Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <memory> |
| 18 | |
| 19 | #include <EventHub.h> |
| 20 | #include <gestures/GestureConverter.h> |
| 21 | #include <gtest/gtest.h> |
| 22 | |
| 23 | #include "FakeEventHub.h" |
| 24 | #include "FakeInputReaderPolicy.h" |
| 25 | #include "FakePointerController.h" |
| 26 | #include "InstrumentedInputReader.h" |
| 27 | #include "NotifyArgs.h" |
| 28 | #include "TestConstants.h" |
| 29 | #include "TestInputListener.h" |
| 30 | #include "TestInputListenerMatchers.h" |
| 31 | #include "include/gestures.h" |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 32 | #include "ui/Rotation.h" |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | using testing::AllOf; |
| 37 | |
| 38 | class GestureConverterTest : public testing::Test { |
| 39 | protected: |
| 40 | static constexpr int32_t DEVICE_ID = END_RESERVED_ID + 1000; |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 41 | static constexpr int32_t EVENTHUB_ID = 1; |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 42 | static constexpr stime_t ARBITRARY_GESTURE_TIME = 1.2; |
Harry Cutts | b1e8355 | 2022-12-20 11:02:26 +0000 | [diff] [blame] | 43 | static constexpr float POINTER_X = 500; |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 44 | static constexpr float POINTER_Y = 200; |
| 45 | |
| 46 | void SetUp() { |
| 47 | mFakeEventHub = std::make_unique<FakeEventHub>(); |
| 48 | mFakePolicy = sp<FakeInputReaderPolicy>::make(); |
| 49 | mFakeListener = std::make_unique<TestInputListener>(); |
| 50 | mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy, |
| 51 | *mFakeListener); |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 52 | mDevice = newDevice(); |
| 53 | mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, -500, 500, 0, 0, 20); |
| 54 | mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, -500, 500, 0, 0, 20); |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 55 | |
| 56 | mFakePointerController = std::make_shared<FakePointerController>(); |
| 57 | mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1); |
| 58 | mFakePointerController->setPosition(POINTER_X, POINTER_Y); |
| 59 | mFakePolicy->setPointerController(mFakePointerController); |
| 60 | } |
| 61 | |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 62 | std::shared_ptr<InputDevice> newDevice() { |
| 63 | InputDeviceIdentifier identifier; |
| 64 | identifier.name = "device"; |
| 65 | identifier.location = "USB1"; |
| 66 | identifier.bus = 0; |
| 67 | std::shared_ptr<InputDevice> device = |
| 68 | std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, /* generation= */ 2, |
| 69 | identifier); |
| 70 | mReader->pushNextDevice(device); |
| 71 | mFakeEventHub->addDevice(EVENTHUB_ID, identifier.name, InputDeviceClass::TOUCHPAD, |
| 72 | identifier.bus); |
| 73 | mReader->loopOnce(); |
| 74 | return device; |
| 75 | } |
| 76 | |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 77 | std::shared_ptr<FakeEventHub> mFakeEventHub; |
| 78 | sp<FakeInputReaderPolicy> mFakePolicy; |
| 79 | std::unique_ptr<TestInputListener> mFakeListener; |
| 80 | std::unique_ptr<InstrumentedInputReader> mReader; |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 81 | std::shared_ptr<InputDevice> mDevice; |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 82 | std::shared_ptr<FakePointerController> mFakePointerController; |
| 83 | }; |
| 84 | |
| 85 | TEST_F(GestureConverterTest, Move) { |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 86 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 87 | GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID); |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 88 | |
| 89 | Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10); |
| 90 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture); |
| 91 | ASSERT_EQ(1u, args.size()); |
| 92 | |
| 93 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 94 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 95 | WithCoords(POINTER_X - 5, POINTER_Y + 10), WithRelativeMotion(-5, 10), |
| 96 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithButtonState(0), |
| 97 | WithPressure(0.0f))); |
| 98 | |
Harry Cutts | b1e8355 | 2022-12-20 11:02:26 +0000 | [diff] [blame] | 99 | ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(POINTER_X - 5, POINTER_Y + 10)); |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 102 | TEST_F(GestureConverterTest, Move_Rotated) { |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 103 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 104 | GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID); |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 105 | converter.setOrientation(ui::ROTATION_90); |
| 106 | |
| 107 | Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10); |
| 108 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture); |
| 109 | ASSERT_EQ(1u, args.size()); |
| 110 | |
| 111 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 112 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 113 | WithCoords(POINTER_X + 10, POINTER_Y + 5), WithRelativeMotion(10, 5), |
| 114 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithButtonState(0), |
| 115 | WithPressure(0.0f))); |
| 116 | |
Harry Cutts | b1e8355 | 2022-12-20 11:02:26 +0000 | [diff] [blame] | 117 | ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(POINTER_X + 10, POINTER_Y + 5)); |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 120 | TEST_F(GestureConverterTest, ButtonsChange) { |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 121 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 122 | GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID); |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 123 | |
| 124 | // Press left and right buttons at once |
| 125 | Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 126 | /* down= */ GESTURES_BUTTON_LEFT | GESTURES_BUTTON_RIGHT, |
| 127 | /* up= */ GESTURES_BUTTON_NONE, /* is_tap= */ false); |
| 128 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, downGesture); |
| 129 | ASSERT_EQ(3u, args.size()); |
| 130 | |
| 131 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 132 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 133 | WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY | |
| 134 | AMOTION_EVENT_BUTTON_SECONDARY), |
| 135 | WithCoords(POINTER_X, POINTER_Y), |
| 136 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 137 | args.pop_front(); |
| 138 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 139 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS), |
| 140 | WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), |
| 141 | WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY), |
| 142 | WithCoords(POINTER_X, POINTER_Y), |
| 143 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 144 | args.pop_front(); |
| 145 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 146 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS), |
| 147 | WithActionButton(AMOTION_EVENT_BUTTON_SECONDARY), |
| 148 | WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY | |
| 149 | AMOTION_EVENT_BUTTON_SECONDARY), |
| 150 | WithCoords(POINTER_X, POINTER_Y), |
| 151 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 152 | |
| 153 | // Then release the left button |
| 154 | Gesture leftUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 155 | /* down= */ GESTURES_BUTTON_NONE, /* up= */ GESTURES_BUTTON_LEFT, |
| 156 | /* is_tap= */ false); |
| 157 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, leftUpGesture); |
| 158 | ASSERT_EQ(1u, args.size()); |
| 159 | |
| 160 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 161 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), |
| 162 | WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), |
| 163 | WithButtonState(AMOTION_EVENT_BUTTON_SECONDARY), |
| 164 | WithCoords(POINTER_X, POINTER_Y), |
| 165 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 166 | |
| 167 | // Finally release the right button |
| 168 | Gesture rightUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 169 | /* down= */ GESTURES_BUTTON_NONE, /* up= */ GESTURES_BUTTON_RIGHT, |
| 170 | /* is_tap= */ false); |
| 171 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, rightUpGesture); |
| 172 | ASSERT_EQ(2u, args.size()); |
| 173 | |
| 174 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 175 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), |
| 176 | WithActionButton(AMOTION_EVENT_BUTTON_SECONDARY), WithButtonState(0), |
| 177 | WithCoords(POINTER_X, POINTER_Y), |
| 178 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 179 | args.pop_front(); |
| 180 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 181 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0), |
| 182 | WithCoords(POINTER_X, POINTER_Y), |
| 183 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 184 | } |
| 185 | |
| 186 | TEST_F(GestureConverterTest, DragWithButton) { |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 187 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 188 | GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID); |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 189 | |
| 190 | // Press the button |
| 191 | Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 192 | /* down= */ GESTURES_BUTTON_LEFT, /* up= */ GESTURES_BUTTON_NONE, |
| 193 | /* is_tap= */ false); |
| 194 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, downGesture); |
| 195 | ASSERT_EQ(2u, args.size()); |
| 196 | |
| 197 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 198 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 199 | WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY), |
| 200 | WithCoords(POINTER_X, POINTER_Y), |
| 201 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 202 | args.pop_front(); |
| 203 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 204 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS), |
| 205 | WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), |
| 206 | WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY), |
| 207 | WithCoords(POINTER_X, POINTER_Y), |
| 208 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 209 | |
| 210 | // Move |
| 211 | Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10); |
| 212 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture); |
| 213 | ASSERT_EQ(1u, args.size()); |
| 214 | |
| 215 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 216 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 217 | WithCoords(POINTER_X - 5, POINTER_Y + 10), WithRelativeMotion(-5, 10), |
| 218 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), |
| 219 | WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY), WithPressure(1.0f))); |
| 220 | |
Harry Cutts | b1e8355 | 2022-12-20 11:02:26 +0000 | [diff] [blame] | 221 | ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(POINTER_X - 5, POINTER_Y + 10)); |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 222 | |
| 223 | // Release the button |
| 224 | Gesture upGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 225 | /* down= */ GESTURES_BUTTON_NONE, /* up= */ GESTURES_BUTTON_LEFT, |
| 226 | /* is_tap= */ false); |
| 227 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, upGesture); |
| 228 | ASSERT_EQ(2u, args.size()); |
| 229 | |
| 230 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 231 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), |
| 232 | WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), WithButtonState(0), |
| 233 | WithCoords(POINTER_X - 5, POINTER_Y + 10), |
| 234 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 235 | args.pop_front(); |
| 236 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 237 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0), |
| 238 | WithCoords(POINTER_X - 5, POINTER_Y + 10), |
| 239 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 240 | } |
| 241 | |
Harry Cutts | ef400b2 | 2022-12-16 21:26:24 +0000 | [diff] [blame] | 242 | TEST_F(GestureConverterTest, Scroll) { |
| 243 | const nsecs_t downTime = 12345; |
| 244 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 245 | GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID); |
| 246 | |
Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame^] | 247 | Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10); |
Harry Cutts | ef400b2 | 2022-12-16 21:26:24 +0000 | [diff] [blame] | 248 | std::list<NotifyArgs> args = converter.handleGesture(downTime, READ_TIME, startGesture); |
| 249 | ASSERT_EQ(2u, args.size()); |
| 250 | |
| 251 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 252 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithCoords(POINTER_X, POINTER_Y), |
| 253 | WithGestureScrollDistance(0, 0, EPSILON), |
| 254 | WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE), |
| 255 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDownTime(downTime))); |
| 256 | args.pop_front(); |
| 257 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 258 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 259 | WithCoords(POINTER_X, POINTER_Y - 10), |
| 260 | WithGestureScrollDistance(0, 10, EPSILON), |
| 261 | WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE), |
| 262 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 263 | |
Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame^] | 264 | Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5); |
Harry Cutts | ef400b2 | 2022-12-16 21:26:24 +0000 | [diff] [blame] | 265 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture); |
| 266 | ASSERT_EQ(1u, args.size()); |
| 267 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 268 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 269 | WithCoords(POINTER_X, POINTER_Y - 15), |
| 270 | WithGestureScrollDistance(0, 5, EPSILON), |
| 271 | WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE), |
| 272 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 273 | |
| 274 | Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1, |
| 275 | GESTURES_FLING_START); |
| 276 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture); |
| 277 | ASSERT_EQ(1u, args.size()); |
| 278 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 279 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 280 | WithCoords(POINTER_X, POINTER_Y - 15), |
| 281 | WithGestureScrollDistance(0, 0, EPSILON), |
| 282 | WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE), |
| 283 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 284 | } |
| 285 | |
| 286 | TEST_F(GestureConverterTest, Scroll_Rotated) { |
| 287 | const nsecs_t downTime = 12345; |
| 288 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 289 | GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID); |
| 290 | converter.setOrientation(ui::ROTATION_90); |
| 291 | |
Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame^] | 292 | Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10); |
Harry Cutts | ef400b2 | 2022-12-16 21:26:24 +0000 | [diff] [blame] | 293 | std::list<NotifyArgs> args = converter.handleGesture(downTime, READ_TIME, startGesture); |
| 294 | ASSERT_EQ(2u, args.size()); |
| 295 | |
| 296 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 297 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithCoords(POINTER_X, POINTER_Y), |
| 298 | WithGestureScrollDistance(0, 0, EPSILON), |
| 299 | WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE), |
| 300 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDownTime(downTime))); |
| 301 | args.pop_front(); |
| 302 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 303 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 304 | WithCoords(POINTER_X - 10, POINTER_Y), |
| 305 | WithGestureScrollDistance(0, 10, EPSILON), |
| 306 | WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE), |
| 307 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 308 | |
Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame^] | 309 | Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5); |
Harry Cutts | ef400b2 | 2022-12-16 21:26:24 +0000 | [diff] [blame] | 310 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture); |
| 311 | ASSERT_EQ(1u, args.size()); |
| 312 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 313 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 314 | WithCoords(POINTER_X - 15, POINTER_Y), |
| 315 | WithGestureScrollDistance(0, 5, EPSILON), |
| 316 | WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE), |
| 317 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 318 | |
| 319 | Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1, |
| 320 | GESTURES_FLING_START); |
| 321 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture); |
| 322 | ASSERT_EQ(1u, args.size()); |
| 323 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 324 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 325 | WithCoords(POINTER_X - 15, POINTER_Y), |
| 326 | WithGestureScrollDistance(0, 0, EPSILON), |
| 327 | WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE), |
| 328 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 329 | } |
| 330 | |
| 331 | TEST_F(GestureConverterTest, Scroll_ClearsClassificationAndOffsetsAfterGesture) { |
| 332 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 333 | GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID); |
| 334 | |
Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame^] | 335 | Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10); |
Harry Cutts | ef400b2 | 2022-12-16 21:26:24 +0000 | [diff] [blame] | 336 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture); |
| 337 | |
Harry Cutts | a546ba8 | 2023-01-13 17:21:00 +0000 | [diff] [blame^] | 338 | Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5); |
Harry Cutts | ef400b2 | 2022-12-16 21:26:24 +0000 | [diff] [blame] | 339 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture); |
| 340 | |
| 341 | Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1, |
| 342 | GESTURES_FLING_START); |
| 343 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture); |
| 344 | |
| 345 | Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10); |
| 346 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture); |
| 347 | ASSERT_EQ(1u, args.size()); |
| 348 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 349 | AllOf(WithMotionClassification(MotionClassification::NONE), |
| 350 | WithGestureScrollDistance(0, 0, EPSILON))); |
| 351 | } |
| 352 | |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 353 | TEST_F(GestureConverterTest, ThreeFingerSwipe_ClearsClassificationAndOffsetsAfterGesture) { |
| 354 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 355 | GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID); |
| 356 | |
| 357 | Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ 0, |
| 358 | /* dy= */ 0); |
| 359 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture); |
| 360 | |
| 361 | Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME); |
| 362 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture); |
| 363 | |
| 364 | Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ -5, |
| 365 | /* dy= */ 10); |
| 366 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture); |
| 367 | ASSERT_EQ(1u, args.size()); |
| 368 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 369 | AllOf(WithMotionClassification(MotionClassification::NONE), |
| 370 | WithGestureOffset(0, 0, EPSILON))); |
| 371 | } |
| 372 | |
| 373 | TEST_F(GestureConverterTest, ThreeFingerSwipe_Vertical) { |
| 374 | // The gestures library will "lock" a swipe into the dimension it starts in. For example, if you |
| 375 | // start swiping up and then start moving left or right, it'll return gesture events with only Y |
| 376 | // deltas until you lift your fingers and start swiping again. That's why each of these tests |
| 377 | // only checks movement in one dimension. |
| 378 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 379 | GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID); |
| 380 | |
| 381 | Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ 0, |
| 382 | /* dy= */ 10); |
| 383 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture); |
| 384 | ASSERT_EQ(4u, args.size()); |
| 385 | |
| 386 | // Three fake fingers should be created. We don't actually care where they are, so long as they |
| 387 | // move appropriately. |
| 388 | NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front()); |
| 389 | ASSERT_THAT(arg, |
| 390 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON), |
| 391 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 392 | WithPointerCount(1u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 393 | PointerCoords finger0Start = arg.pointerCoords[0]; |
| 394 | args.pop_front(); |
| 395 | arg = std::get<NotifyMotionArgs>(args.front()); |
| 396 | ASSERT_THAT(arg, |
| 397 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 398 | 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 399 | WithGestureOffset(0, 0, EPSILON), |
| 400 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 401 | WithPointerCount(2u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 402 | PointerCoords finger1Start = arg.pointerCoords[1]; |
| 403 | args.pop_front(); |
| 404 | arg = std::get<NotifyMotionArgs>(args.front()); |
| 405 | ASSERT_THAT(arg, |
| 406 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 407 | 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 408 | WithGestureOffset(0, 0, EPSILON), |
| 409 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 410 | WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 411 | PointerCoords finger2Start = arg.pointerCoords[2]; |
| 412 | args.pop_front(); |
| 413 | |
| 414 | arg = std::get<NotifyMotionArgs>(args.front()); |
| 415 | ASSERT_THAT(arg, |
| 416 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 417 | WithGestureOffset(0, -0.01, EPSILON), |
| 418 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 419 | WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 420 | EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX()); |
| 421 | EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX()); |
| 422 | EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX()); |
| 423 | EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY() - 10); |
| 424 | EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY() - 10); |
| 425 | EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY() - 10); |
| 426 | |
| 427 | Gesture continueGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 428 | /* dx= */ 0, /* dy= */ 5); |
| 429 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture); |
| 430 | ASSERT_EQ(1u, args.size()); |
| 431 | arg = std::get<NotifyMotionArgs>(args.front()); |
| 432 | ASSERT_THAT(arg, |
| 433 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 434 | WithGestureOffset(0, -0.005, EPSILON), |
| 435 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 436 | WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 437 | EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX()); |
| 438 | EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX()); |
| 439 | EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX()); |
| 440 | EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY() - 15); |
| 441 | EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY() - 15); |
| 442 | EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY() - 15); |
| 443 | |
| 444 | Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME); |
| 445 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture); |
| 446 | ASSERT_EQ(3u, args.size()); |
| 447 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 448 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP | |
| 449 | 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 450 | WithGestureOffset(0, 0, EPSILON), |
| 451 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 452 | WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 453 | args.pop_front(); |
| 454 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 455 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP | |
| 456 | 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 457 | WithGestureOffset(0, 0, EPSILON), |
| 458 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 459 | WithPointerCount(2u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 460 | args.pop_front(); |
| 461 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 462 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON), |
| 463 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 464 | WithPointerCount(1u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 465 | } |
| 466 | |
Harry Cutts | 94f5bd5 | 2023-01-06 18:02:18 +0000 | [diff] [blame] | 467 | TEST_F(GestureConverterTest, ThreeFingerSwipe_Rotated) { |
| 468 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 469 | GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID); |
| 470 | converter.setOrientation(ui::ROTATION_90); |
| 471 | |
| 472 | Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ 0, |
| 473 | /* dy= */ 10); |
| 474 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture); |
| 475 | ASSERT_EQ(4u, args.size()); |
| 476 | |
| 477 | // Three fake fingers should be created. We don't actually care where they are, so long as they |
| 478 | // move appropriately. |
| 479 | NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front()); |
| 480 | ASSERT_THAT(arg, |
| 481 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON), |
| 482 | WithPointerCount(1u))); |
| 483 | PointerCoords finger0Start = arg.pointerCoords[0]; |
| 484 | args.pop_front(); |
| 485 | arg = std::get<NotifyMotionArgs>(args.front()); |
| 486 | ASSERT_THAT(arg, |
| 487 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 488 | 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 489 | WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u))); |
| 490 | PointerCoords finger1Start = arg.pointerCoords[1]; |
| 491 | args.pop_front(); |
| 492 | arg = std::get<NotifyMotionArgs>(args.front()); |
| 493 | ASSERT_THAT(arg, |
| 494 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 495 | 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 496 | WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u))); |
| 497 | PointerCoords finger2Start = arg.pointerCoords[2]; |
| 498 | args.pop_front(); |
| 499 | |
| 500 | arg = std::get<NotifyMotionArgs>(args.front()); |
| 501 | ASSERT_THAT(arg, |
| 502 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 503 | WithGestureOffset(0, -0.01, EPSILON), WithPointerCount(3u))); |
| 504 | EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() - 10); |
| 505 | EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() - 10); |
| 506 | EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() - 10); |
| 507 | EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY()); |
| 508 | EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY()); |
| 509 | EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY()); |
| 510 | |
| 511 | Gesture continueGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 512 | /* dx= */ 0, /* dy= */ 5); |
| 513 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture); |
| 514 | ASSERT_EQ(1u, args.size()); |
| 515 | arg = std::get<NotifyMotionArgs>(args.front()); |
| 516 | ASSERT_THAT(arg, |
| 517 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 518 | WithGestureOffset(0, -0.005, EPSILON), WithPointerCount(3u))); |
| 519 | EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() - 15); |
| 520 | EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() - 15); |
| 521 | EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() - 15); |
| 522 | EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY()); |
| 523 | EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY()); |
| 524 | EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY()); |
| 525 | |
| 526 | Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME); |
| 527 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture); |
| 528 | ASSERT_EQ(3u, args.size()); |
| 529 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 530 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP | |
| 531 | 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 532 | WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u))); |
| 533 | args.pop_front(); |
| 534 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 535 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP | |
| 536 | 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 537 | WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u))); |
| 538 | args.pop_front(); |
| 539 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 540 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON), |
| 541 | WithPointerCount(1u))); |
| 542 | } |
| 543 | |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 544 | TEST_F(GestureConverterTest, FourFingerSwipe_Horizontal) { |
| 545 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 546 | GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID); |
| 547 | |
| 548 | Gesture startGesture(kGestureFourFingerSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 549 | /* dx= */ 10, /* dy= */ 0); |
| 550 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture); |
| 551 | ASSERT_EQ(5u, args.size()); |
| 552 | |
| 553 | // Four fake fingers should be created. We don't actually care where they are, so long as they |
| 554 | // move appropriately. |
| 555 | NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front()); |
| 556 | ASSERT_THAT(arg, |
| 557 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON), |
| 558 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 559 | WithPointerCount(1u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 560 | PointerCoords finger0Start = arg.pointerCoords[0]; |
| 561 | args.pop_front(); |
| 562 | arg = std::get<NotifyMotionArgs>(args.front()); |
| 563 | ASSERT_THAT(arg, |
| 564 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 565 | 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 566 | WithGestureOffset(0, 0, EPSILON), |
| 567 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 568 | WithPointerCount(2u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 569 | PointerCoords finger1Start = arg.pointerCoords[1]; |
| 570 | args.pop_front(); |
| 571 | arg = std::get<NotifyMotionArgs>(args.front()); |
| 572 | ASSERT_THAT(arg, |
| 573 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 574 | 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 575 | WithGestureOffset(0, 0, EPSILON), |
| 576 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 577 | WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 578 | PointerCoords finger2Start = arg.pointerCoords[2]; |
| 579 | args.pop_front(); |
| 580 | arg = std::get<NotifyMotionArgs>(args.front()); |
| 581 | ASSERT_THAT(arg, |
| 582 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 583 | 3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 584 | WithGestureOffset(0, 0, EPSILON), |
| 585 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 586 | WithPointerCount(4u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 587 | PointerCoords finger3Start = arg.pointerCoords[3]; |
| 588 | args.pop_front(); |
| 589 | |
| 590 | arg = std::get<NotifyMotionArgs>(args.front()); |
| 591 | ASSERT_THAT(arg, |
| 592 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 593 | WithGestureOffset(0.01, 0, EPSILON), |
| 594 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 595 | WithPointerCount(4u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 596 | EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 10); |
| 597 | EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() + 10); |
| 598 | EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() + 10); |
| 599 | EXPECT_EQ(arg.pointerCoords[3].getX(), finger3Start.getX() + 10); |
| 600 | EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY()); |
| 601 | EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY()); |
| 602 | EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY()); |
| 603 | EXPECT_EQ(arg.pointerCoords[3].getY(), finger3Start.getY()); |
| 604 | |
| 605 | Gesture continueGesture(kGestureFourFingerSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 606 | /* dx= */ 5, /* dy= */ 0); |
| 607 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture); |
| 608 | ASSERT_EQ(1u, args.size()); |
| 609 | arg = std::get<NotifyMotionArgs>(args.front()); |
| 610 | ASSERT_THAT(arg, |
| 611 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 612 | WithGestureOffset(0.005, 0, EPSILON), |
| 613 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 614 | WithPointerCount(4u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 615 | EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 15); |
| 616 | EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() + 15); |
| 617 | EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() + 15); |
| 618 | EXPECT_EQ(arg.pointerCoords[3].getX(), finger3Start.getX() + 15); |
| 619 | EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY()); |
| 620 | EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY()); |
| 621 | EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY()); |
| 622 | EXPECT_EQ(arg.pointerCoords[3].getY(), finger3Start.getY()); |
| 623 | |
| 624 | Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME); |
| 625 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture); |
| 626 | ASSERT_EQ(4u, args.size()); |
| 627 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 628 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP | |
| 629 | 3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 630 | WithGestureOffset(0, 0, EPSILON), |
| 631 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 632 | WithPointerCount(4u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 633 | args.pop_front(); |
| 634 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 635 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP | |
| 636 | 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 637 | WithGestureOffset(0, 0, EPSILON), |
| 638 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 639 | WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 640 | args.pop_front(); |
| 641 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 642 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP | |
| 643 | 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 644 | WithGestureOffset(0, 0, EPSILON), |
| 645 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 646 | WithPointerCount(2u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 647 | args.pop_front(); |
| 648 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 649 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON), |
| 650 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 651 | WithPointerCount(1u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 652 | } |
| 653 | |
Harry Cutts | b1e8355 | 2022-12-20 11:02:26 +0000 | [diff] [blame] | 654 | TEST_F(GestureConverterTest, Pinch_Inwards) { |
| 655 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 656 | GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID); |
| 657 | |
| 658 | Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1, |
| 659 | GESTURES_ZOOM_START); |
| 660 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture); |
| 661 | ASSERT_EQ(2u, args.size()); |
| 662 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 663 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 664 | WithMotionClassification(MotionClassification::PINCH), |
| 665 | WithGesturePinchScaleFactor(1.0f, EPSILON), |
| 666 | WithCoords(POINTER_X - 100, POINTER_Y), WithPointerCount(1u), |
| 667 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 668 | args.pop_front(); |
| 669 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 670 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 671 | 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 672 | WithMotionClassification(MotionClassification::PINCH), |
| 673 | WithGesturePinchScaleFactor(1.0f, EPSILON), |
| 674 | WithPointerCoords(1, POINTER_X + 100, POINTER_Y), WithPointerCount(2u), |
| 675 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 676 | |
| 677 | Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 678 | /* dz= */ 0.8, GESTURES_ZOOM_UPDATE); |
| 679 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, updateGesture); |
| 680 | ASSERT_EQ(1u, args.size()); |
| 681 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 682 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 683 | WithMotionClassification(MotionClassification::PINCH), |
| 684 | WithGesturePinchScaleFactor(0.8f, EPSILON), |
| 685 | WithPointerCoords(0, POINTER_X - 80, POINTER_Y), |
| 686 | WithPointerCoords(1, POINTER_X + 80, POINTER_Y), WithPointerCount(2u), |
| 687 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 688 | |
| 689 | Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1, |
| 690 | GESTURES_ZOOM_END); |
| 691 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, endGesture); |
| 692 | ASSERT_EQ(2u, args.size()); |
| 693 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 694 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP | |
| 695 | 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 696 | WithMotionClassification(MotionClassification::PINCH), |
| 697 | WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(2u), |
| 698 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 699 | args.pop_front(); |
| 700 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 701 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 702 | WithMotionClassification(MotionClassification::PINCH), |
| 703 | WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(1u), |
| 704 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 705 | } |
| 706 | |
| 707 | TEST_F(GestureConverterTest, Pinch_Outwards) { |
| 708 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 709 | GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID); |
| 710 | |
| 711 | Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1, |
| 712 | GESTURES_ZOOM_START); |
| 713 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture); |
| 714 | ASSERT_EQ(2u, args.size()); |
| 715 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 716 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 717 | WithMotionClassification(MotionClassification::PINCH), |
| 718 | WithGesturePinchScaleFactor(1.0f, EPSILON), |
| 719 | WithCoords(POINTER_X - 100, POINTER_Y), WithPointerCount(1u), |
| 720 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 721 | args.pop_front(); |
| 722 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 723 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 724 | 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 725 | WithMotionClassification(MotionClassification::PINCH), |
| 726 | WithGesturePinchScaleFactor(1.0f, EPSILON), |
| 727 | WithPointerCoords(1, POINTER_X + 100, POINTER_Y), WithPointerCount(2u), |
| 728 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 729 | |
| 730 | Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 731 | /* dz= */ 1.2, GESTURES_ZOOM_UPDATE); |
| 732 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, updateGesture); |
| 733 | ASSERT_EQ(1u, args.size()); |
| 734 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 735 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 736 | WithMotionClassification(MotionClassification::PINCH), |
| 737 | WithGesturePinchScaleFactor(1.2f, EPSILON), |
| 738 | WithPointerCoords(0, POINTER_X - 120, POINTER_Y), |
| 739 | WithPointerCoords(1, POINTER_X + 120, POINTER_Y), WithPointerCount(2u), |
| 740 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 741 | |
| 742 | Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1, |
| 743 | GESTURES_ZOOM_END); |
| 744 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, endGesture); |
| 745 | ASSERT_EQ(2u, args.size()); |
| 746 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 747 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP | |
| 748 | 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 749 | WithMotionClassification(MotionClassification::PINCH), |
| 750 | WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(2u), |
| 751 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 752 | args.pop_front(); |
| 753 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 754 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 755 | WithMotionClassification(MotionClassification::PINCH), |
| 756 | WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(1u), |
| 757 | WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 758 | } |
| 759 | |
| 760 | TEST_F(GestureConverterTest, Pinch_ClearsClassificationAndScaleFactorAfterGesture) { |
| 761 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 762 | GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID); |
| 763 | |
| 764 | Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1, |
| 765 | GESTURES_ZOOM_START); |
| 766 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture); |
| 767 | |
| 768 | Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 769 | /* dz= */ 1.2, GESTURES_ZOOM_UPDATE); |
| 770 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, updateGesture); |
| 771 | |
| 772 | Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1, |
| 773 | GESTURES_ZOOM_END); |
| 774 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, endGesture); |
| 775 | |
| 776 | Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10); |
| 777 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture); |
| 778 | ASSERT_EQ(1u, args.size()); |
| 779 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 780 | AllOf(WithMotionClassification(MotionClassification::NONE), |
| 781 | WithGesturePinchScaleFactor(0, EPSILON))); |
| 782 | } |
| 783 | |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 784 | } // namespace android |