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