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; |
| 43 | static constexpr float POINTER_X = 100; |
| 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 | |
| 99 | ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(95, 210)); |
| 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 | |
| 117 | ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110, 205)); |
| 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 | |
| 221 | ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(95, 210)); |
| 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 | |
| 247 | Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, 10); |
| 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 | |
| 264 | Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, 5); |
| 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 | |
| 292 | Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, 10); |
| 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 | |
| 309 | Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, 5); |
| 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 | |
| 335 | Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, 10); |
| 336 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture); |
| 337 | |
| 338 | Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, 5); |
| 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 | |
| 467 | TEST_F(GestureConverterTest, FourFingerSwipe_Horizontal) { |
| 468 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 469 | GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID); |
| 470 | |
| 471 | Gesture startGesture(kGestureFourFingerSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 472 | /* dx= */ 10, /* dy= */ 0); |
| 473 | std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture); |
| 474 | ASSERT_EQ(5u, args.size()); |
| 475 | |
| 476 | // Four fake fingers should be created. We don't actually care where they are, so long as they |
| 477 | // move appropriately. |
| 478 | NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front()); |
| 479 | ASSERT_THAT(arg, |
| 480 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON), |
| 481 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 482 | WithPointerCount(1u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 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), |
| 490 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 491 | WithPointerCount(2u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 492 | PointerCoords finger1Start = arg.pointerCoords[1]; |
| 493 | args.pop_front(); |
| 494 | arg = std::get<NotifyMotionArgs>(args.front()); |
| 495 | ASSERT_THAT(arg, |
| 496 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 497 | 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 498 | WithGestureOffset(0, 0, EPSILON), |
| 499 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 500 | WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 501 | PointerCoords finger2Start = arg.pointerCoords[2]; |
| 502 | args.pop_front(); |
| 503 | arg = std::get<NotifyMotionArgs>(args.front()); |
| 504 | ASSERT_THAT(arg, |
| 505 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 506 | 3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 507 | WithGestureOffset(0, 0, EPSILON), |
| 508 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 509 | WithPointerCount(4u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 510 | PointerCoords finger3Start = arg.pointerCoords[3]; |
| 511 | args.pop_front(); |
| 512 | |
| 513 | arg = std::get<NotifyMotionArgs>(args.front()); |
| 514 | ASSERT_THAT(arg, |
| 515 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 516 | WithGestureOffset(0.01, 0, EPSILON), |
| 517 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 518 | WithPointerCount(4u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 519 | EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 10); |
| 520 | EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() + 10); |
| 521 | EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() + 10); |
| 522 | EXPECT_EQ(arg.pointerCoords[3].getX(), finger3Start.getX() + 10); |
| 523 | EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY()); |
| 524 | EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY()); |
| 525 | EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY()); |
| 526 | EXPECT_EQ(arg.pointerCoords[3].getY(), finger3Start.getY()); |
| 527 | |
| 528 | Gesture continueGesture(kGestureFourFingerSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, |
| 529 | /* dx= */ 5, /* dy= */ 0); |
| 530 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture); |
| 531 | ASSERT_EQ(1u, args.size()); |
| 532 | arg = std::get<NotifyMotionArgs>(args.front()); |
| 533 | ASSERT_THAT(arg, |
| 534 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 535 | WithGestureOffset(0.005, 0, EPSILON), |
| 536 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 537 | WithPointerCount(4u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 538 | EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 15); |
| 539 | EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() + 15); |
| 540 | EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() + 15); |
| 541 | EXPECT_EQ(arg.pointerCoords[3].getX(), finger3Start.getX() + 15); |
| 542 | EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY()); |
| 543 | EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY()); |
| 544 | EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY()); |
| 545 | EXPECT_EQ(arg.pointerCoords[3].getY(), finger3Start.getY()); |
| 546 | |
| 547 | Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME); |
| 548 | args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture); |
| 549 | ASSERT_EQ(4u, args.size()); |
| 550 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 551 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP | |
| 552 | 3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 553 | WithGestureOffset(0, 0, EPSILON), |
| 554 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 555 | WithPointerCount(4u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 556 | args.pop_front(); |
| 557 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 558 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP | |
| 559 | 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 560 | WithGestureOffset(0, 0, EPSILON), |
| 561 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 562 | WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 563 | args.pop_front(); |
| 564 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 565 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP | |
| 566 | 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 567 | WithGestureOffset(0, 0, EPSILON), |
| 568 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 569 | WithPointerCount(2u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 570 | args.pop_front(); |
| 571 | ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()), |
| 572 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON), |
| 573 | WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE), |
| 574 | WithPointerCount(1u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))); |
| 575 | } |
| 576 | |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 577 | } // namespace android |