Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 1 | # Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """ This is a module containing functions that allow the test suite to |
| 6 | determine the specifics of the gesture and execute them on the touchbot. |
| 7 | In essence, you use this class to go from a Test object to the robot |
| 8 | actually performing the correct gesture on the pad. |
| 9 | """ |
| 10 | |
Charlie Mooney | a337d58 | 2015-04-29 10:37:45 -0700 | [diff] [blame] | 11 | import math |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 12 | from threading import Thread |
| 13 | |
| 14 | import colorama as color |
| 15 | |
| 16 | import tests |
| 17 | from touchbot import Touchbot |
| 18 | |
| 19 | |
Charlie Mooney | 058aa5f | 2015-05-06 12:55:48 -0700 | [diff] [blame] | 20 | NOISE_TESTING_FINGERTIP = '1round_12mm' |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 21 | STANDARD_FINGERTIP = '1round_9mm' |
Charlie Mooney | a337d58 | 2015-04-29 10:37:45 -0700 | [diff] [blame] | 22 | STANDARD_SECONDARY_FINGERTIP = '2round_9mm' |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 23 | |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 24 | BUFFER_SIZE = 0.1 |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 25 | OVERSHOOT_DISTANCE = 0.05 |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 26 | LEFT = BUFFER_SIZE |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 27 | OVER_LEFT = -OVERSHOOT_DISTANCE |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 28 | RIGHT = 1.0 - BUFFER_SIZE |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 29 | OVER_RIGHT = 1.0 + OVERSHOOT_DISTANCE |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 30 | TOP = BUFFER_SIZE |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 31 | OVER_TOP = -OVERSHOOT_DISTANCE |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 32 | BOTTOM = 1.0 - BUFFER_SIZE |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 33 | OVER_BOTTOM = 1.0 + OVERSHOOT_DISTANCE |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 34 | CENTER = 0.5 |
| 35 | |
| 36 | LOCATION_COORDINATES = { |
| 37 | tests.GV.TL: (LEFT, TOP), |
| 38 | tests.GV.TR: (RIGHT, TOP), |
| 39 | tests.GV.BL: (LEFT, BOTTOM), |
| 40 | tests.GV.BR: (RIGHT, BOTTOM), |
| 41 | tests.GV.TS: (CENTER, TOP), |
| 42 | tests.GV.BS: (CENTER, BOTTOM), |
| 43 | tests.GV.LS: (LEFT, CENTER), |
| 44 | tests.GV.RS: (RIGHT, CENTER), |
| 45 | tests.GV.CENTER: (CENTER, CENTER), |
| 46 | } |
| 47 | |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 48 | LINE_DIRECTION_COORDINATES = { |
| 49 | tests.GV.LR: ((LEFT, CENTER), (RIGHT, CENTER)), |
| 50 | tests.GV.RL: ((RIGHT, CENTER), (LEFT, CENTER)), |
| 51 | tests.GV.TB: ((CENTER, TOP), (CENTER, BOTTOM)), |
| 52 | tests.GV.BT: ((CENTER, BOTTOM), (CENTER, TOP)), |
| 53 | tests.GV.BLTR: ((LEFT, BOTTOM), (RIGHT, TOP)), |
| 54 | tests.GV.BRTL: ((RIGHT, BOTTOM), (LEFT, TOP)), |
| 55 | tests.GV.TRBL: ((RIGHT, TOP), (LEFT, BOTTOM)), |
| 56 | tests.GV.TLBR: ((LEFT, TOP), (RIGHT, BOTTOM)), |
| 57 | tests.GV.CL: ((CENTER, CENTER), (OVER_LEFT, CENTER)), |
| 58 | tests.GV.CR: ((CENTER, CENTER), (OVER_RIGHT, CENTER)), |
| 59 | tests.GV.CT: ((CENTER, CENTER), (CENTER, OVER_TOP)), |
| 60 | tests.GV.CB: ((CENTER, CENTER), (CENTER, OVER_BOTTOM)), |
| 61 | tests.GV.CUL: ((CENTER, CENTER), (OVER_LEFT, OVER_TOP)), |
| 62 | tests.GV.CLL: ((CENTER, CENTER), (OVER_LEFT, OVER_BOTTOM)), |
| 63 | tests.GV.CLR: ((CENTER, CENTER), (OVER_RIGHT, OVER_BOTTOM)), |
| 64 | } |
| 65 | |
| 66 | SPEEDS = { |
| 67 | tests.GV.NORMAL: Touchbot.SPEED_MEDIUM, |
| 68 | tests.GV.SLOW: Touchbot.SPEED_SLOW, |
| 69 | } |
| 70 | |
Charlie Mooney | 4198adb | 2015-05-04 13:55:20 -0700 | [diff] [blame] | 71 | ANGLES = { |
Charlie Mooney | 5065bcb | 2015-05-04 13:18:09 -0700 | [diff] [blame] | 72 | tests.GV.HORIZONTAL: 0, |
| 73 | tests.GV.VERTICAL: 90, |
| 74 | tests.GV.DIAGONAL: 45, |
| 75 | } |
| 76 | |
| 77 | |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 78 | SINGLE_FINGER_LINE_TESTS = [ |
| 79 | tests.ONE_FINGER_TO_EDGE, |
| 80 | tests.ONE_FINGER_TRACKING, |
| 81 | tests.ONE_FINGER_TRACKING_FROM_CENTER, |
| 82 | ] |
| 83 | |
Charlie Mooney | cfb4a64 | 2015-05-07 15:33:37 -0700 | [diff] [blame^] | 84 | TWO_FINGER_LINE_TESTS = [ |
| 85 | tests.TWO_FINGER_TRACKING, |
| 86 | tests.TWO_CLOSE_FINGERS_TRACKING, |
| 87 | ] |
| 88 | |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 89 | |
Charlie Mooney | c56c860 | 2015-05-07 15:15:40 -0700 | [diff] [blame] | 90 | def _ComputePerpendicularAngle(start, end): |
| 91 | """ Compute the fingertip angle to be perpendicular to the |
| 92 | movement direction. |
| 93 | |
| 94 | The robot's X/Y axes are not in the same direction as the touch device's, |
| 95 | they are flipped. |
| 96 | |
| 97 | DUT x ----> Robot y ----> |
| 98 | y x |
| 99 | | | |
| 100 | | | |
| 101 | \/ \/ |
| 102 | |
| 103 | As a result the angle is computed in the DUT space, but then must have |
| 104 | its sign flipped before being used in any commands to the robot or |
| 105 | everything will be wrong since it's a clockwise angle instead of counter- |
| 106 | clockwise. |
| 107 | """ |
| 108 | x1, y1 = start |
| 109 | x2, y2 = end |
| 110 | dy = y2 - y1 |
| 111 | dx = x2 - x1 |
| 112 | return -1 * (math.degrees(math.atan2(y2 - y1, x2 - x1)) + 90) |
| 113 | |
| 114 | |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 115 | def PerformCorrespondingGesture(test, variation_number, robot, device_spec): |
| 116 | variation = test.variations[variation_number] |
| 117 | fn = None |
| 118 | if test.name == tests.NOISE_STATIONARY: |
| 119 | fn = lambda: _PerformStationaryNoiseTest(variation, robot, device_spec) |
Charlie Mooney | 1b70811 | 2015-04-28 14:46:21 -0700 | [diff] [blame] | 120 | elif test.name == tests.ONE_FINGER_TAP: |
| 121 | fn = lambda: _PerformOneFingerTapTest(variation, robot, device_spec) |
Charlie Mooney | 4198adb | 2015-05-04 13:55:20 -0700 | [diff] [blame] | 122 | elif test.name == tests.TWO_FINGER_TAP: |
| 123 | fn = lambda: _PerformTwoFingerTapTest(variation, robot, device_spec) |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 124 | elif test.name in SINGLE_FINGER_LINE_TESTS: |
| 125 | pause = 1 if test.name == tests.ONE_FINGER_TRACKING_FROM_CENTER else 0 |
| 126 | fn = lambda: _PerformOneFingerLineTest(variation, robot, device_spec, pause) |
Charlie Mooney | cfb4a64 | 2015-05-07 15:33:37 -0700 | [diff] [blame^] | 127 | elif test.name in TWO_FINGER_LINE_TESTS: |
| 128 | spacing = 0 if test.name == tests.TWO_CLOSE_FINGERS_TRACKING else 5 |
| 129 | fn = lambda: _PerformTwoFingerLineTest(variation, robot, device_spec, |
| 130 | spacing_mm=spacing) |
Charlie Mooney | e9fd656 | 2015-04-30 13:45:19 -0700 | [diff] [blame] | 131 | elif test.name == tests.RESTING_FINGER_PLUS_2ND_FINGER_MOVE: |
| 132 | fn = lambda: _PerformRestingFingerTest(variation, robot, device_spec) |
Charlie Mooney | 5065bcb | 2015-05-04 13:18:09 -0700 | [diff] [blame] | 133 | elif test.name == tests.PINCH_TO_ZOOM: |
| 134 | fn = lambda: _PerformPinchTest(variation, robot, device_spec) |
Charlie Mooney | c56c860 | 2015-05-07 15:15:40 -0700 | [diff] [blame] | 135 | elif test.name == tests.DRAG_THUMB_EDGE: |
| 136 | fn = lambda: _PerformThumbEdgeTest(variation, robot, device_spec) |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 137 | |
| 138 | if fn is None: |
| 139 | print color.Fore.RED + 'Error: Robot unable to perform gesture!' |
| 140 | return None |
| 141 | |
| 142 | return Thread(target=fn) |
| 143 | |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 144 | |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 145 | def _PerformStationaryNoiseTest(variation, robot, device_spec): |
| 146 | frequency, amplitude, waveform, location = variation |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 147 | tap_position = LOCATION_COORDINATES[location] |
Charlie Mooney | 058aa5f | 2015-05-06 12:55:48 -0700 | [diff] [blame] | 148 | fingertip = robot.fingertips[NOISE_TESTING_FINGERTIP] |
Charlie Mooney | 4198adb | 2015-05-04 13:55:20 -0700 | [diff] [blame] | 149 | robot.Tap(device_spec, [fingertip], tap_position, touch_time_s=4) |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 150 | |
| 151 | |
Charlie Mooney | 1b70811 | 2015-04-28 14:46:21 -0700 | [diff] [blame] | 152 | def _PerformOneFingerTapTest(variation, robot, device_spec): |
| 153 | location, = variation |
| 154 | tap_position = LOCATION_COORDINATES[location] |
| 155 | fingertip = robot.fingertips[STANDARD_FINGERTIP] |
Charlie Mooney | 4198adb | 2015-05-04 13:55:20 -0700 | [diff] [blame] | 156 | robot.Tap(device_spec, [fingertip], tap_position) |
| 157 | |
| 158 | |
| 159 | def _PerformTwoFingerTapTest(variation, robot, device_spec): |
| 160 | angle, = variation |
| 161 | |
| 162 | fingertip1 = robot.fingertips[STANDARD_FINGERTIP] |
| 163 | fingertip2 = robot.fingertips[STANDARD_SECONDARY_FINGERTIP] |
| 164 | fingertips = [fingertip1, fingertip2] |
| 165 | |
| 166 | robot.Tap(device_spec, fingertips, (CENTER, CENTER), angle=ANGLES[angle]) |
Charlie Mooney | 1b70811 | 2015-04-28 14:46:21 -0700 | [diff] [blame] | 167 | |
| 168 | |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 169 | def _PerformOneFingerLineTest(variation, robot, device_spec, pause_time_s): |
| 170 | direction, speed = variation |
| 171 | start, end = LINE_DIRECTION_COORDINATES[direction] |
| 172 | fingertip = robot.fingertips[STANDARD_FINGERTIP] |
| 173 | |
| 174 | robot.PushSpeed(SPEEDS[speed]) |
Charlie Mooney | a337d58 | 2015-04-29 10:37:45 -0700 | [diff] [blame] | 175 | robot.Line(device_spec, [fingertip], start, end, pause_time_s) |
| 176 | robot.PopSpeed() |
| 177 | |
| 178 | |
Charlie Mooney | cfb4a64 | 2015-05-07 15:33:37 -0700 | [diff] [blame^] | 179 | def _PerformTwoFingerLineTest(variation, robot, device_spec, spacing_mm): |
Charlie Mooney | a337d58 | 2015-04-29 10:37:45 -0700 | [diff] [blame] | 180 | direction, speed = variation |
| 181 | start, end = LINE_DIRECTION_COORDINATES[direction] |
Charlie Mooney | c56c860 | 2015-05-07 15:15:40 -0700 | [diff] [blame] | 182 | angle = _ComputePerpendicularAngle(start, end) |
Charlie Mooney | a337d58 | 2015-04-29 10:37:45 -0700 | [diff] [blame] | 183 | |
| 184 | fingertips = [robot.fingertips[STANDARD_FINGERTIP], |
| 185 | robot.fingertips[STANDARD_SECONDARY_FINGERTIP]] |
Charlie Mooney | cfb4a64 | 2015-05-07 15:33:37 -0700 | [diff] [blame^] | 186 | |
Charlie Mooney | a337d58 | 2015-04-29 10:37:45 -0700 | [diff] [blame] | 187 | robot.PushSpeed(SPEEDS[speed]) |
| 188 | robot.Line(device_spec, fingertips, start, end, |
Charlie Mooney | cfb4a64 | 2015-05-07 15:33:37 -0700 | [diff] [blame^] | 189 | fingertip_spacing=spacing_mm, fingertip_angle=angle) |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 190 | robot.PopSpeed() |
Charlie Mooney | e9fd656 | 2015-04-30 13:45:19 -0700 | [diff] [blame] | 191 | |
| 192 | |
| 193 | def _PerformRestingFingerTest(variation, robot, device_spec): |
| 194 | direction, speed = variation |
| 195 | start, end = LINE_DIRECTION_COORDINATES[direction] |
| 196 | stationary_location = LOCATION_COORDINATES[tests.GV.BL] |
| 197 | |
| 198 | stationary_fingertip = robot.fingertips[STANDARD_FINGERTIP] |
| 199 | moving_fingertip = robot.fingertips[STANDARD_SECONDARY_FINGERTIP] |
| 200 | |
| 201 | robot.PushSpeed(SPEEDS[speed]) |
| 202 | robot.LineWithStationaryFinger(device_spec, stationary_fingertip, |
| 203 | moving_fingertip, start, end, |
| 204 | stationary_location) |
| 205 | robot.PopSpeed() |
Charlie Mooney | 5065bcb | 2015-05-04 13:18:09 -0700 | [diff] [blame] | 206 | |
| 207 | def _PerformPinchTest(variation, robot, device_spec): |
| 208 | direction, angle = variation |
| 209 | |
| 210 | min_spread = 15 |
| 211 | max_spread = min(device_spec.Height(), device_spec.Width(), |
| 212 | robot.MAX_FINGER_DISTANCE) |
| 213 | if direction == tests.GV.ZOOM_OUT: |
| 214 | start_distance, end_distance = max_spread, min_spread |
| 215 | else: |
| 216 | start_distance, end_distance = min_spread, max_spread |
| 217 | |
Charlie Mooney | 058aa5f | 2015-05-06 12:55:48 -0700 | [diff] [blame] | 218 | fingertips = [robot.fingertips[STANDARD_FINGERTIP], |
| 219 | robot.fingertips[STANDARD_SECONDARY_FINGERTIP]] |
| 220 | |
Charlie Mooney | 5065bcb | 2015-05-04 13:18:09 -0700 | [diff] [blame] | 221 | robot.PushSpeed(SPEEDS[tests.GV.NORMAL]) |
Charlie Mooney | 058aa5f | 2015-05-06 12:55:48 -0700 | [diff] [blame] | 222 | robot.Pinch(device_spec, fingertips, (CENTER, CENTER), ANGLES[angle], |
Charlie Mooney | 5065bcb | 2015-05-04 13:18:09 -0700 | [diff] [blame] | 223 | start_distance, end_distance) |
| 224 | robot.PopSpeed() |
Charlie Mooney | c56c860 | 2015-05-07 15:15:40 -0700 | [diff] [blame] | 225 | |
| 226 | def _PerformThumbEdgeTest(variation, robot, device_spec): |
| 227 | fingertip_type, direction = variation |
| 228 | start, end = LINE_DIRECTION_COORDINATES[direction] |
| 229 | angle = _ComputePerpendicularAngle(start, end) |
| 230 | |
| 231 | fingertip = robot.fingertips[fingertip_type] |
| 232 | |
| 233 | robot.PushSpeed(SPEEDS[tests.GV.NORMAL]) |
| 234 | robot.Line(device_spec, [fingertip], start, end, fingertip_angle=angle) |
| 235 | robot.PopSpeed() |