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 | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 84 | |
| 85 | def PerformCorrespondingGesture(test, variation_number, robot, device_spec): |
| 86 | variation = test.variations[variation_number] |
| 87 | fn = None |
| 88 | if test.name == tests.NOISE_STATIONARY: |
| 89 | fn = lambda: _PerformStationaryNoiseTest(variation, robot, device_spec) |
Charlie Mooney | 1b70811 | 2015-04-28 14:46:21 -0700 | [diff] [blame] | 90 | elif test.name == tests.ONE_FINGER_TAP: |
| 91 | fn = lambda: _PerformOneFingerTapTest(variation, robot, device_spec) |
Charlie Mooney | 4198adb | 2015-05-04 13:55:20 -0700 | [diff] [blame] | 92 | elif test.name == tests.TWO_FINGER_TAP: |
| 93 | fn = lambda: _PerformTwoFingerTapTest(variation, robot, device_spec) |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 94 | elif test.name in SINGLE_FINGER_LINE_TESTS: |
| 95 | pause = 1 if test.name == tests.ONE_FINGER_TRACKING_FROM_CENTER else 0 |
| 96 | fn = lambda: _PerformOneFingerLineTest(variation, robot, device_spec, pause) |
Charlie Mooney | e9fd656 | 2015-04-30 13:45:19 -0700 | [diff] [blame] | 97 | elif test.name == tests.TWO_FINGER_TRACKING: |
Charlie Mooney | a337d58 | 2015-04-29 10:37:45 -0700 | [diff] [blame] | 98 | fn = lambda: _PerformTwoFingerLineTest(variation, robot, device_spec) |
Charlie Mooney | e9fd656 | 2015-04-30 13:45:19 -0700 | [diff] [blame] | 99 | elif test.name == tests.RESTING_FINGER_PLUS_2ND_FINGER_MOVE: |
| 100 | fn = lambda: _PerformRestingFingerTest(variation, robot, device_spec) |
Charlie Mooney | 5065bcb | 2015-05-04 13:18:09 -0700 | [diff] [blame] | 101 | elif test.name == tests.PINCH_TO_ZOOM: |
| 102 | fn = lambda: _PerformPinchTest(variation, robot, device_spec) |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 103 | |
| 104 | if fn is None: |
| 105 | print color.Fore.RED + 'Error: Robot unable to perform gesture!' |
| 106 | return None |
| 107 | |
| 108 | return Thread(target=fn) |
| 109 | |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 110 | |
Charlie Mooney | 0866191 | 2015-04-16 09:20:34 -0700 | [diff] [blame] | 111 | def _PerformStationaryNoiseTest(variation, robot, device_spec): |
| 112 | frequency, amplitude, waveform, location = variation |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 113 | tap_position = LOCATION_COORDINATES[location] |
Charlie Mooney | 058aa5f | 2015-05-06 12:55:48 -0700 | [diff] [blame^] | 114 | fingertip = robot.fingertips[NOISE_TESTING_FINGERTIP] |
Charlie Mooney | 4198adb | 2015-05-04 13:55:20 -0700 | [diff] [blame] | 115 | robot.Tap(device_spec, [fingertip], tap_position, touch_time_s=4) |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 116 | |
| 117 | |
Charlie Mooney | 1b70811 | 2015-04-28 14:46:21 -0700 | [diff] [blame] | 118 | def _PerformOneFingerTapTest(variation, robot, device_spec): |
| 119 | location, = variation |
| 120 | tap_position = LOCATION_COORDINATES[location] |
| 121 | fingertip = robot.fingertips[STANDARD_FINGERTIP] |
Charlie Mooney | 4198adb | 2015-05-04 13:55:20 -0700 | [diff] [blame] | 122 | robot.Tap(device_spec, [fingertip], tap_position) |
| 123 | |
| 124 | |
| 125 | def _PerformTwoFingerTapTest(variation, robot, device_spec): |
| 126 | angle, = variation |
| 127 | |
| 128 | fingertip1 = robot.fingertips[STANDARD_FINGERTIP] |
| 129 | fingertip2 = robot.fingertips[STANDARD_SECONDARY_FINGERTIP] |
| 130 | fingertips = [fingertip1, fingertip2] |
| 131 | |
| 132 | robot.Tap(device_spec, fingertips, (CENTER, CENTER), angle=ANGLES[angle]) |
Charlie Mooney | 1b70811 | 2015-04-28 14:46:21 -0700 | [diff] [blame] | 133 | |
| 134 | |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 135 | def _PerformOneFingerLineTest(variation, robot, device_spec, pause_time_s): |
| 136 | direction, speed = variation |
| 137 | start, end = LINE_DIRECTION_COORDINATES[direction] |
| 138 | fingertip = robot.fingertips[STANDARD_FINGERTIP] |
| 139 | |
| 140 | robot.PushSpeed(SPEEDS[speed]) |
Charlie Mooney | a337d58 | 2015-04-29 10:37:45 -0700 | [diff] [blame] | 141 | robot.Line(device_spec, [fingertip], start, end, pause_time_s) |
| 142 | robot.PopSpeed() |
| 143 | |
| 144 | |
| 145 | def _PerformTwoFingerLineTest(variation, robot, device_spec): |
| 146 | direction, speed = variation |
| 147 | start, end = LINE_DIRECTION_COORDINATES[direction] |
| 148 | |
| 149 | # Compute the fingertip angle to be perpendicular to the |
| 150 | # movement direction |
| 151 | x1, y1 = start |
| 152 | x2, y2 = end |
| 153 | dy = y2 - y1 |
| 154 | dx = x2 - x1 |
| 155 | angle = -1 * (math.degrees(math.atan2(y2 - y1, x2 - x1)) + 90) |
| 156 | |
| 157 | fingertips = [robot.fingertips[STANDARD_FINGERTIP], |
| 158 | robot.fingertips[STANDARD_SECONDARY_FINGERTIP]] |
| 159 | robot.PushSpeed(SPEEDS[speed]) |
| 160 | robot.Line(device_spec, fingertips, start, end, |
| 161 | fingertip_spacing=5, fingertip_angle=angle) |
Charlie Mooney | 61b4fbb | 2015-04-22 15:22:02 -0700 | [diff] [blame] | 162 | robot.PopSpeed() |
Charlie Mooney | e9fd656 | 2015-04-30 13:45:19 -0700 | [diff] [blame] | 163 | |
| 164 | |
| 165 | def _PerformRestingFingerTest(variation, robot, device_spec): |
| 166 | direction, speed = variation |
| 167 | start, end = LINE_DIRECTION_COORDINATES[direction] |
| 168 | stationary_location = LOCATION_COORDINATES[tests.GV.BL] |
| 169 | |
| 170 | stationary_fingertip = robot.fingertips[STANDARD_FINGERTIP] |
| 171 | moving_fingertip = robot.fingertips[STANDARD_SECONDARY_FINGERTIP] |
| 172 | |
| 173 | robot.PushSpeed(SPEEDS[speed]) |
| 174 | robot.LineWithStationaryFinger(device_spec, stationary_fingertip, |
| 175 | moving_fingertip, start, end, |
| 176 | stationary_location) |
| 177 | robot.PopSpeed() |
Charlie Mooney | 5065bcb | 2015-05-04 13:18:09 -0700 | [diff] [blame] | 178 | |
| 179 | def _PerformPinchTest(variation, robot, device_spec): |
| 180 | direction, angle = variation |
| 181 | |
| 182 | min_spread = 15 |
| 183 | max_spread = min(device_spec.Height(), device_spec.Width(), |
| 184 | robot.MAX_FINGER_DISTANCE) |
| 185 | if direction == tests.GV.ZOOM_OUT: |
| 186 | start_distance, end_distance = max_spread, min_spread |
| 187 | else: |
| 188 | start_distance, end_distance = min_spread, max_spread |
| 189 | |
Charlie Mooney | 058aa5f | 2015-05-06 12:55:48 -0700 | [diff] [blame^] | 190 | fingertips = [robot.fingertips[STANDARD_FINGERTIP], |
| 191 | robot.fingertips[STANDARD_SECONDARY_FINGERTIP]] |
| 192 | |
Charlie Mooney | 5065bcb | 2015-05-04 13:18:09 -0700 | [diff] [blame] | 193 | robot.PushSpeed(SPEEDS[tests.GV.NORMAL]) |
Charlie Mooney | 058aa5f | 2015-05-06 12:55:48 -0700 | [diff] [blame^] | 194 | robot.Pinch(device_spec, fingertips, (CENTER, CENTER), ANGLES[angle], |
Charlie Mooney | 5065bcb | 2015-05-04 13:18:09 -0700 | [diff] [blame] | 195 | start_distance, end_distance) |
| 196 | robot.PopSpeed() |