blob: 33f404d56b6bdc33416239ba254939eface643b8 [file] [log] [blame]
Harry Cutts4fb941a2022-12-14 19:14:04 +00001/*
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 Cuttsedf6ce72023-01-04 12:15:53 +000032#include "ui/Rotation.h"
Harry Cutts4fb941a2022-12-14 19:14:04 +000033
34namespace android {
35
36using testing::AllOf;
37
38class GestureConverterTest : public testing::Test {
39protected:
40 static constexpr int32_t DEVICE_ID = END_RESERVED_ID + 1000;
Harry Cuttsc5748d12022-12-02 17:30:18 +000041 static constexpr int32_t EVENTHUB_ID = 1;
Harry Cutts4fb941a2022-12-14 19:14:04 +000042 static constexpr stime_t ARBITRARY_GESTURE_TIME = 1.2;
Harry Cuttsb1e83552022-12-20 11:02:26 +000043 static constexpr float POINTER_X = 500;
Harry Cutts4fb941a2022-12-14 19:14:04 +000044 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 Cuttsc5748d12022-12-02 17:30:18 +000052 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 Cutts4fb941a2022-12-14 19:14:04 +000055
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 Cuttsc5748d12022-12-02 17:30:18 +000062 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 Cutts4fb941a2022-12-14 19:14:04 +000077 std::shared_ptr<FakeEventHub> mFakeEventHub;
78 sp<FakeInputReaderPolicy> mFakePolicy;
79 std::unique_ptr<TestInputListener> mFakeListener;
80 std::unique_ptr<InstrumentedInputReader> mReader;
Harry Cuttsc5748d12022-12-02 17:30:18 +000081 std::shared_ptr<InputDevice> mDevice;
Harry Cutts4fb941a2022-12-14 19:14:04 +000082 std::shared_ptr<FakePointerController> mFakePointerController;
83};
84
85TEST_F(GestureConverterTest, Move) {
Harry Cuttsc5748d12022-12-02 17:30:18 +000086 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
87 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Harry Cutts4fb941a2022-12-14 19:14:04 +000088
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 Cuttsb1e83552022-12-20 11:02:26 +000099 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(POINTER_X - 5, POINTER_Y + 10));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000100}
101
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000102TEST_F(GestureConverterTest, Move_Rotated) {
Harry Cuttsc5748d12022-12-02 17:30:18 +0000103 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
104 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000105 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 Cuttsb1e83552022-12-20 11:02:26 +0000117 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(POINTER_X + 10, POINTER_Y + 5));
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000118}
119
Harry Cutts4fb941a2022-12-14 19:14:04 +0000120TEST_F(GestureConverterTest, ButtonsChange) {
Harry Cuttsc5748d12022-12-02 17:30:18 +0000121 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
122 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Harry Cutts4fb941a2022-12-14 19:14:04 +0000123
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
186TEST_F(GestureConverterTest, DragWithButton) {
Harry Cuttsc5748d12022-12-02 17:30:18 +0000187 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
188 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
Harry Cutts4fb941a2022-12-14 19:14:04 +0000189
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 Cuttsb1e83552022-12-20 11:02:26 +0000221 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(POINTER_X - 5, POINTER_Y + 10));
Harry Cutts4fb941a2022-12-14 19:14:04 +0000222
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 Cuttsef400b22022-12-16 21:26:24 +0000242TEST_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 Cuttsa546ba82023-01-13 17:21:00 +0000247 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
Harry Cuttsef400b22022-12-16 21:26:24 +0000248 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),
Harry Cutts24492e62023-03-10 15:17:43 +0000255 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDownTime(downTime),
256 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE)));
Harry Cuttsef400b22022-12-16 21:26:24 +0000257 args.pop_front();
258 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
259 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
260 WithCoords(POINTER_X, POINTER_Y - 10),
261 WithGestureScrollDistance(0, 10, EPSILON),
262 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
Harry Cutts24492e62023-03-10 15:17:43 +0000263 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER),
264 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE)));
Harry Cuttsef400b22022-12-16 21:26:24 +0000265
Harry Cuttsa546ba82023-01-13 17:21:00 +0000266 Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
Harry Cuttsef400b22022-12-16 21:26:24 +0000267 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
268 ASSERT_EQ(1u, args.size());
269 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
270 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
271 WithCoords(POINTER_X, POINTER_Y - 15),
272 WithGestureScrollDistance(0, 5, EPSILON),
273 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
Harry Cutts24492e62023-03-10 15:17:43 +0000274 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER),
275 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE)));
Harry Cuttsef400b22022-12-16 21:26:24 +0000276
277 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
278 GESTURES_FLING_START);
279 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
280 ASSERT_EQ(1u, args.size());
281 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
282 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
283 WithCoords(POINTER_X, POINTER_Y - 15),
284 WithGestureScrollDistance(0, 0, EPSILON),
285 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
Harry Cutts24492e62023-03-10 15:17:43 +0000286 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER),
287 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE)));
Harry Cuttsef400b22022-12-16 21:26:24 +0000288}
289
290TEST_F(GestureConverterTest, Scroll_Rotated) {
291 const nsecs_t downTime = 12345;
292 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
293 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
294 converter.setOrientation(ui::ROTATION_90);
295
Harry Cuttsa546ba82023-01-13 17:21:00 +0000296 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
Harry Cuttsef400b22022-12-16 21:26:24 +0000297 std::list<NotifyArgs> args = converter.handleGesture(downTime, READ_TIME, startGesture);
298 ASSERT_EQ(2u, args.size());
299
300 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
301 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithCoords(POINTER_X, POINTER_Y),
302 WithGestureScrollDistance(0, 0, EPSILON),
303 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
304 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDownTime(downTime)));
305 args.pop_front();
306 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
307 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
308 WithCoords(POINTER_X - 10, POINTER_Y),
309 WithGestureScrollDistance(0, 10, EPSILON),
310 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
311 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
312
Harry Cuttsa546ba82023-01-13 17:21:00 +0000313 Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
Harry Cuttsef400b22022-12-16 21:26:24 +0000314 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
315 ASSERT_EQ(1u, args.size());
316 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
317 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
318 WithCoords(POINTER_X - 15, POINTER_Y),
319 WithGestureScrollDistance(0, 5, EPSILON),
320 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
321 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
322
323 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
324 GESTURES_FLING_START);
325 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
326 ASSERT_EQ(1u, args.size());
327 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
328 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
329 WithCoords(POINTER_X - 15, POINTER_Y),
330 WithGestureScrollDistance(0, 0, EPSILON),
331 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
332 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
333}
334
335TEST_F(GestureConverterTest, Scroll_ClearsClassificationAndOffsetsAfterGesture) {
336 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
337 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
338
Harry Cuttsa546ba82023-01-13 17:21:00 +0000339 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
Harry Cuttsef400b22022-12-16 21:26:24 +0000340 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
341
Harry Cuttsa546ba82023-01-13 17:21:00 +0000342 Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
Harry Cuttsef400b22022-12-16 21:26:24 +0000343 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
344
345 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
346 GESTURES_FLING_START);
347 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
348
349 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
350 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture);
351 ASSERT_EQ(1u, args.size());
352 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
353 AllOf(WithMotionClassification(MotionClassification::NONE),
354 WithGestureScrollDistance(0, 0, EPSILON)));
355}
356
Harry Cuttsc5748d12022-12-02 17:30:18 +0000357TEST_F(GestureConverterTest, ThreeFingerSwipe_ClearsClassificationAndOffsetsAfterGesture) {
358 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
359 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
360
361 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ 0,
362 /* dy= */ 0);
363 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
364
365 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
366 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
367
368 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ -5,
369 /* dy= */ 10);
370 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture);
371 ASSERT_EQ(1u, args.size());
372 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
373 AllOf(WithMotionClassification(MotionClassification::NONE),
374 WithGestureOffset(0, 0, EPSILON)));
375}
376
377TEST_F(GestureConverterTest, ThreeFingerSwipe_Vertical) {
378 // The gestures library will "lock" a swipe into the dimension it starts in. For example, if you
379 // start swiping up and then start moving left or right, it'll return gesture events with only Y
380 // deltas until you lift your fingers and start swiping again. That's why each of these tests
381 // only checks movement in one dimension.
382 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
383 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
384
385 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ 0,
386 /* dy= */ 10);
387 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
388 ASSERT_EQ(4u, args.size());
389
390 // Three fake fingers should be created. We don't actually care where they are, so long as they
391 // move appropriately.
392 NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
393 ASSERT_THAT(arg,
394 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
395 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
396 WithPointerCount(1u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
397 PointerCoords finger0Start = arg.pointerCoords[0];
398 args.pop_front();
399 arg = std::get<NotifyMotionArgs>(args.front());
400 ASSERT_THAT(arg,
401 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
402 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
403 WithGestureOffset(0, 0, EPSILON),
404 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
405 WithPointerCount(2u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
406 PointerCoords finger1Start = arg.pointerCoords[1];
407 args.pop_front();
408 arg = std::get<NotifyMotionArgs>(args.front());
409 ASSERT_THAT(arg,
410 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
411 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
412 WithGestureOffset(0, 0, EPSILON),
413 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
414 WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
415 PointerCoords finger2Start = arg.pointerCoords[2];
416 args.pop_front();
417
418 arg = std::get<NotifyMotionArgs>(args.front());
419 ASSERT_THAT(arg,
420 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
421 WithGestureOffset(0, -0.01, EPSILON),
422 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
423 WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
424 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX());
425 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX());
426 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX());
427 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY() - 10);
428 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY() - 10);
429 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY() - 10);
430
431 Gesture continueGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
432 /* dx= */ 0, /* dy= */ 5);
433 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
434 ASSERT_EQ(1u, args.size());
435 arg = std::get<NotifyMotionArgs>(args.front());
436 ASSERT_THAT(arg,
437 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
438 WithGestureOffset(0, -0.005, EPSILON),
439 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
440 WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
441 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX());
442 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX());
443 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX());
444 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY() - 15);
445 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY() - 15);
446 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY() - 15);
447
448 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
449 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
450 ASSERT_EQ(3u, args.size());
451 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
452 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
453 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
454 WithGestureOffset(0, 0, EPSILON),
455 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
456 WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
457 args.pop_front();
458 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
459 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
460 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
461 WithGestureOffset(0, 0, EPSILON),
462 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
463 WithPointerCount(2u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
464 args.pop_front();
465 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
466 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON),
467 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
468 WithPointerCount(1u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
469}
470
Harry Cutts94f5bd52023-01-06 18:02:18 +0000471TEST_F(GestureConverterTest, ThreeFingerSwipe_Rotated) {
472 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
473 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
474 converter.setOrientation(ui::ROTATION_90);
475
476 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ 0,
477 /* dy= */ 10);
478 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
479 ASSERT_EQ(4u, args.size());
480
481 // Three fake fingers should be created. We don't actually care where they are, so long as they
482 // move appropriately.
483 NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
484 ASSERT_THAT(arg,
485 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
486 WithPointerCount(1u)));
487 PointerCoords finger0Start = arg.pointerCoords[0];
488 args.pop_front();
489 arg = std::get<NotifyMotionArgs>(args.front());
490 ASSERT_THAT(arg,
491 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
492 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
493 WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u)));
494 PointerCoords finger1Start = arg.pointerCoords[1];
495 args.pop_front();
496 arg = std::get<NotifyMotionArgs>(args.front());
497 ASSERT_THAT(arg,
498 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
499 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
500 WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u)));
501 PointerCoords finger2Start = arg.pointerCoords[2];
502 args.pop_front();
503
504 arg = std::get<NotifyMotionArgs>(args.front());
505 ASSERT_THAT(arg,
506 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
507 WithGestureOffset(0, -0.01, EPSILON), WithPointerCount(3u)));
508 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() - 10);
509 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() - 10);
510 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() - 10);
511 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
512 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
513 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
514
515 Gesture continueGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
516 /* dx= */ 0, /* dy= */ 5);
517 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
518 ASSERT_EQ(1u, args.size());
519 arg = std::get<NotifyMotionArgs>(args.front());
520 ASSERT_THAT(arg,
521 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
522 WithGestureOffset(0, -0.005, EPSILON), WithPointerCount(3u)));
523 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() - 15);
524 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() - 15);
525 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() - 15);
526 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
527 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
528 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
529
530 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
531 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
532 ASSERT_EQ(3u, args.size());
533 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
534 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
535 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
536 WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u)));
537 args.pop_front();
538 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
539 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
540 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
541 WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u)));
542 args.pop_front();
543 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
544 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON),
545 WithPointerCount(1u)));
546}
547
Harry Cuttsc5748d12022-12-02 17:30:18 +0000548TEST_F(GestureConverterTest, FourFingerSwipe_Horizontal) {
549 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
550 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
551
552 Gesture startGesture(kGestureFourFingerSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
553 /* dx= */ 10, /* dy= */ 0);
554 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
555 ASSERT_EQ(5u, args.size());
556
557 // Four fake fingers should be created. We don't actually care where they are, so long as they
558 // move appropriately.
559 NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
560 ASSERT_THAT(arg,
561 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
562 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
563 WithPointerCount(1u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
564 PointerCoords finger0Start = arg.pointerCoords[0];
565 args.pop_front();
566 arg = std::get<NotifyMotionArgs>(args.front());
567 ASSERT_THAT(arg,
568 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
569 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
570 WithGestureOffset(0, 0, EPSILON),
571 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
572 WithPointerCount(2u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
573 PointerCoords finger1Start = arg.pointerCoords[1];
574 args.pop_front();
575 arg = std::get<NotifyMotionArgs>(args.front());
576 ASSERT_THAT(arg,
577 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
578 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
579 WithGestureOffset(0, 0, EPSILON),
580 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
581 WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
582 PointerCoords finger2Start = arg.pointerCoords[2];
583 args.pop_front();
584 arg = std::get<NotifyMotionArgs>(args.front());
585 ASSERT_THAT(arg,
586 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
587 3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
588 WithGestureOffset(0, 0, EPSILON),
589 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
590 WithPointerCount(4u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
591 PointerCoords finger3Start = arg.pointerCoords[3];
592 args.pop_front();
593
594 arg = std::get<NotifyMotionArgs>(args.front());
595 ASSERT_THAT(arg,
596 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
597 WithGestureOffset(0.01, 0, EPSILON),
598 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
599 WithPointerCount(4u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
600 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 10);
601 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() + 10);
602 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() + 10);
603 EXPECT_EQ(arg.pointerCoords[3].getX(), finger3Start.getX() + 10);
604 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
605 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
606 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
607 EXPECT_EQ(arg.pointerCoords[3].getY(), finger3Start.getY());
608
609 Gesture continueGesture(kGestureFourFingerSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
610 /* dx= */ 5, /* dy= */ 0);
611 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
612 ASSERT_EQ(1u, args.size());
613 arg = std::get<NotifyMotionArgs>(args.front());
614 ASSERT_THAT(arg,
615 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
616 WithGestureOffset(0.005, 0, EPSILON),
617 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
618 WithPointerCount(4u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
619 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 15);
620 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() + 15);
621 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() + 15);
622 EXPECT_EQ(arg.pointerCoords[3].getX(), finger3Start.getX() + 15);
623 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
624 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
625 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
626 EXPECT_EQ(arg.pointerCoords[3].getY(), finger3Start.getY());
627
628 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
629 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
630 ASSERT_EQ(4u, args.size());
631 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
632 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
633 3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
634 WithGestureOffset(0, 0, EPSILON),
635 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
636 WithPointerCount(4u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
637 args.pop_front();
638 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
639 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
640 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
641 WithGestureOffset(0, 0, EPSILON),
642 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
643 WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
644 args.pop_front();
645 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
646 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
647 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
648 WithGestureOffset(0, 0, EPSILON),
649 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
650 WithPointerCount(2u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
651 args.pop_front();
652 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
653 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON),
654 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
655 WithPointerCount(1u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
656}
657
Harry Cuttsb1e83552022-12-20 11:02:26 +0000658TEST_F(GestureConverterTest, Pinch_Inwards) {
659 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
660 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
661
662 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
663 GESTURES_ZOOM_START);
664 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
665 ASSERT_EQ(2u, args.size());
666 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
667 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
668 WithMotionClassification(MotionClassification::PINCH),
669 WithGesturePinchScaleFactor(1.0f, EPSILON),
670 WithCoords(POINTER_X - 100, POINTER_Y), WithPointerCount(1u),
671 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
672 args.pop_front();
673 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
674 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
675 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
676 WithMotionClassification(MotionClassification::PINCH),
677 WithGesturePinchScaleFactor(1.0f, EPSILON),
678 WithPointerCoords(1, POINTER_X + 100, POINTER_Y), WithPointerCount(2u),
679 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
680
681 Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
682 /* dz= */ 0.8, GESTURES_ZOOM_UPDATE);
683 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, updateGesture);
684 ASSERT_EQ(1u, args.size());
685 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
686 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
687 WithMotionClassification(MotionClassification::PINCH),
688 WithGesturePinchScaleFactor(0.8f, EPSILON),
689 WithPointerCoords(0, POINTER_X - 80, POINTER_Y),
690 WithPointerCoords(1, POINTER_X + 80, POINTER_Y), WithPointerCount(2u),
691 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
692
693 Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
694 GESTURES_ZOOM_END);
695 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, endGesture);
696 ASSERT_EQ(2u, args.size());
697 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
698 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
699 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
700 WithMotionClassification(MotionClassification::PINCH),
701 WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(2u),
702 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
703 args.pop_front();
704 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
705 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
706 WithMotionClassification(MotionClassification::PINCH),
707 WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(1u),
708 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
709}
710
711TEST_F(GestureConverterTest, Pinch_Outwards) {
712 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
713 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
714
715 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
716 GESTURES_ZOOM_START);
717 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
718 ASSERT_EQ(2u, args.size());
719 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
720 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
721 WithMotionClassification(MotionClassification::PINCH),
722 WithGesturePinchScaleFactor(1.0f, EPSILON),
723 WithCoords(POINTER_X - 100, POINTER_Y), WithPointerCount(1u),
724 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
725 args.pop_front();
726 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
727 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
728 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
729 WithMotionClassification(MotionClassification::PINCH),
730 WithGesturePinchScaleFactor(1.0f, EPSILON),
731 WithPointerCoords(1, POINTER_X + 100, POINTER_Y), WithPointerCount(2u),
732 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
733
734 Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
735 /* dz= */ 1.2, GESTURES_ZOOM_UPDATE);
736 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, updateGesture);
737 ASSERT_EQ(1u, args.size());
738 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
739 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
740 WithMotionClassification(MotionClassification::PINCH),
741 WithGesturePinchScaleFactor(1.2f, EPSILON),
742 WithPointerCoords(0, POINTER_X - 120, POINTER_Y),
743 WithPointerCoords(1, POINTER_X + 120, POINTER_Y), WithPointerCount(2u),
744 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
745
746 Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
747 GESTURES_ZOOM_END);
748 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, endGesture);
749 ASSERT_EQ(2u, args.size());
750 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
751 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
752 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
753 WithMotionClassification(MotionClassification::PINCH),
754 WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(2u),
755 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
756 args.pop_front();
757 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
758 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
759 WithMotionClassification(MotionClassification::PINCH),
760 WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(1u),
761 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
762}
763
764TEST_F(GestureConverterTest, Pinch_ClearsClassificationAndScaleFactorAfterGesture) {
765 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
766 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
767
768 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
769 GESTURES_ZOOM_START);
770 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
771
772 Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
773 /* dz= */ 1.2, GESTURES_ZOOM_UPDATE);
774 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, updateGesture);
775
776 Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
777 GESTURES_ZOOM_END);
778 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, endGesture);
779
780 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
781 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture);
782 ASSERT_EQ(1u, args.size());
783 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
784 AllOf(WithMotionClassification(MotionClassification::NONE),
785 WithGesturePinchScaleFactor(0, EPSILON)));
786}
787
Harry Cuttse9b71422023-03-14 16:54:44 +0000788TEST_F(GestureConverterTest, ResetWithButtonPressed) {
789 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
790 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
791
792 Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
793 /*down=*/GESTURES_BUTTON_LEFT | GESTURES_BUTTON_RIGHT,
794 /*up=*/GESTURES_BUTTON_NONE, /*is_tap=*/false);
795 (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, downGesture);
796
797 std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
798 ASSERT_EQ(3u, args.size());
799
800 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
801 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
802 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
803 WithButtonState(AMOTION_EVENT_BUTTON_SECONDARY),
804 WithCoords(POINTER_X, POINTER_Y),
805 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
806 args.pop_front();
807 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
808 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
809 WithActionButton(AMOTION_EVENT_BUTTON_SECONDARY), WithButtonState(0),
810 WithCoords(POINTER_X, POINTER_Y),
811 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
812 args.pop_front();
813 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
814 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0),
815 WithCoords(POINTER_X, POINTER_Y),
816 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
817}
818
819TEST_F(GestureConverterTest, ResetDuringScroll) {
820 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
821 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
822
823 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
824 (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
825
826 std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
827 ASSERT_EQ(1u, args.size());
828 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
829 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
830 WithCoords(POINTER_X, POINTER_Y - 10),
831 WithGestureScrollDistance(0, 0, EPSILON),
832 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
833 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER),
834 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE)));
835}
836
837TEST_F(GestureConverterTest, ResetDuringThreeFingerSwipe) {
838 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
839 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
840
841 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/0,
842 /*dy=*/10);
843 (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
844
845 std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
846 ASSERT_EQ(3u, args.size());
847 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
848 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
849 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
850 WithGestureOffset(0, 0, EPSILON),
851 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
852 WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
853 args.pop_front();
854 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
855 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
856 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
857 WithGestureOffset(0, 0, EPSILON),
858 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
859 WithPointerCount(2u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
860 args.pop_front();
861 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
862 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON),
863 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
864 WithPointerCount(1u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
865}
866
867TEST_F(GestureConverterTest, ResetDuringPinch) {
868 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
869 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
870
871 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
872 GESTURES_ZOOM_START);
873 (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
874
875 std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
876 ASSERT_EQ(2u, args.size());
877 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
878 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
879 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
880 WithMotionClassification(MotionClassification::PINCH),
881 WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(2u),
882 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
883 args.pop_front();
884 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
885 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
886 WithMotionClassification(MotionClassification::PINCH),
887 WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(1u),
888 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
889}
890
Harry Cutts4fb941a2022-12-14 19:14:04 +0000891} // namespace android