blob: 3b54de52a9fca8613eef62661ee3e7e8451ecabb [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;
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 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
99 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(95, 210));
100}
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
117 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110, 205));
118}
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
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 Cuttsc5748d12022-12-02 17:30:18 +0000242TEST_F(GestureConverterTest, ThreeFingerSwipe_ClearsClassificationAndOffsetsAfterGesture) {
243 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
244 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
245
246 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ 0,
247 /* dy= */ 0);
248 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
249
250 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
251 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
252
253 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ -5,
254 /* dy= */ 10);
255 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture);
256 ASSERT_EQ(1u, args.size());
257 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
258 AllOf(WithMotionClassification(MotionClassification::NONE),
259 WithGestureOffset(0, 0, EPSILON)));
260}
261
262TEST_F(GestureConverterTest, ThreeFingerSwipe_Vertical) {
263 // The gestures library will "lock" a swipe into the dimension it starts in. For example, if you
264 // start swiping up and then start moving left or right, it'll return gesture events with only Y
265 // deltas until you lift your fingers and start swiping again. That's why each of these tests
266 // only checks movement in one dimension.
267 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
268 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
269
270 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ 0,
271 /* dy= */ 10);
272 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
273 ASSERT_EQ(4u, args.size());
274
275 // Three fake fingers should be created. We don't actually care where they are, so long as they
276 // move appropriately.
277 NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
278 ASSERT_THAT(arg,
279 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
280 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
281 WithPointerCount(1u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
282 PointerCoords finger0Start = arg.pointerCoords[0];
283 args.pop_front();
284 arg = std::get<NotifyMotionArgs>(args.front());
285 ASSERT_THAT(arg,
286 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
287 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
288 WithGestureOffset(0, 0, EPSILON),
289 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
290 WithPointerCount(2u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
291 PointerCoords finger1Start = arg.pointerCoords[1];
292 args.pop_front();
293 arg = std::get<NotifyMotionArgs>(args.front());
294 ASSERT_THAT(arg,
295 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
296 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
297 WithGestureOffset(0, 0, EPSILON),
298 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
299 WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
300 PointerCoords finger2Start = arg.pointerCoords[2];
301 args.pop_front();
302
303 arg = std::get<NotifyMotionArgs>(args.front());
304 ASSERT_THAT(arg,
305 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
306 WithGestureOffset(0, -0.01, EPSILON),
307 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
308 WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
309 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX());
310 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX());
311 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX());
312 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY() - 10);
313 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY() - 10);
314 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY() - 10);
315
316 Gesture continueGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
317 /* dx= */ 0, /* dy= */ 5);
318 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
319 ASSERT_EQ(1u, args.size());
320 arg = std::get<NotifyMotionArgs>(args.front());
321 ASSERT_THAT(arg,
322 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
323 WithGestureOffset(0, -0.005, EPSILON),
324 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
325 WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
326 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX());
327 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX());
328 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX());
329 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY() - 15);
330 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY() - 15);
331 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY() - 15);
332
333 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
334 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
335 ASSERT_EQ(3u, args.size());
336 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
337 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
338 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
339 WithGestureOffset(0, 0, EPSILON),
340 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
341 WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
342 args.pop_front();
343 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
344 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
345 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
346 WithGestureOffset(0, 0, EPSILON),
347 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
348 WithPointerCount(2u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
349 args.pop_front();
350 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
351 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON),
352 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
353 WithPointerCount(1u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
354}
355
Harry Cutts94f5bd52023-01-06 18:02:18 +0000356TEST_F(GestureConverterTest, ThreeFingerSwipe_Rotated) {
357 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
358 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
359 converter.setOrientation(ui::ROTATION_90);
360
361 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ 0,
362 /* dy= */ 10);
363 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
364 ASSERT_EQ(4u, args.size());
365
366 // Three fake fingers should be created. We don't actually care where they are, so long as they
367 // move appropriately.
368 NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
369 ASSERT_THAT(arg,
370 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
371 WithPointerCount(1u)));
372 PointerCoords finger0Start = arg.pointerCoords[0];
373 args.pop_front();
374 arg = std::get<NotifyMotionArgs>(args.front());
375 ASSERT_THAT(arg,
376 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
377 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
378 WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u)));
379 PointerCoords finger1Start = arg.pointerCoords[1];
380 args.pop_front();
381 arg = std::get<NotifyMotionArgs>(args.front());
382 ASSERT_THAT(arg,
383 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
384 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
385 WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u)));
386 PointerCoords finger2Start = arg.pointerCoords[2];
387 args.pop_front();
388
389 arg = std::get<NotifyMotionArgs>(args.front());
390 ASSERT_THAT(arg,
391 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
392 WithGestureOffset(0, -0.01, EPSILON), WithPointerCount(3u)));
393 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() - 10);
394 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() - 10);
395 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() - 10);
396 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
397 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
398 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
399
400 Gesture continueGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
401 /* dx= */ 0, /* dy= */ 5);
402 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
403 ASSERT_EQ(1u, args.size());
404 arg = std::get<NotifyMotionArgs>(args.front());
405 ASSERT_THAT(arg,
406 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
407 WithGestureOffset(0, -0.005, EPSILON), WithPointerCount(3u)));
408 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() - 15);
409 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() - 15);
410 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() - 15);
411 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
412 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
413 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
414
415 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
416 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
417 ASSERT_EQ(3u, args.size());
418 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
419 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
420 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
421 WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u)));
422 args.pop_front();
423 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
424 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
425 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
426 WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u)));
427 args.pop_front();
428 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
429 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON),
430 WithPointerCount(1u)));
431}
432
Harry Cuttsc5748d12022-12-02 17:30:18 +0000433TEST_F(GestureConverterTest, FourFingerSwipe_Horizontal) {
434 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
435 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
436
437 Gesture startGesture(kGestureFourFingerSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
438 /* dx= */ 10, /* dy= */ 0);
439 std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
440 ASSERT_EQ(5u, args.size());
441
442 // Four fake fingers should be created. We don't actually care where they are, so long as they
443 // move appropriately.
444 NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
445 ASSERT_THAT(arg,
446 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
447 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
448 WithPointerCount(1u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
449 PointerCoords finger0Start = arg.pointerCoords[0];
450 args.pop_front();
451 arg = std::get<NotifyMotionArgs>(args.front());
452 ASSERT_THAT(arg,
453 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
454 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
455 WithGestureOffset(0, 0, EPSILON),
456 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
457 WithPointerCount(2u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
458 PointerCoords finger1Start = arg.pointerCoords[1];
459 args.pop_front();
460 arg = std::get<NotifyMotionArgs>(args.front());
461 ASSERT_THAT(arg,
462 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
463 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
464 WithGestureOffset(0, 0, EPSILON),
465 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
466 WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
467 PointerCoords finger2Start = arg.pointerCoords[2];
468 args.pop_front();
469 arg = std::get<NotifyMotionArgs>(args.front());
470 ASSERT_THAT(arg,
471 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
472 3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
473 WithGestureOffset(0, 0, EPSILON),
474 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
475 WithPointerCount(4u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
476 PointerCoords finger3Start = arg.pointerCoords[3];
477 args.pop_front();
478
479 arg = std::get<NotifyMotionArgs>(args.front());
480 ASSERT_THAT(arg,
481 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
482 WithGestureOffset(0.01, 0, EPSILON),
483 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
484 WithPointerCount(4u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
485 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 10);
486 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() + 10);
487 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() + 10);
488 EXPECT_EQ(arg.pointerCoords[3].getX(), finger3Start.getX() + 10);
489 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
490 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
491 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
492 EXPECT_EQ(arg.pointerCoords[3].getY(), finger3Start.getY());
493
494 Gesture continueGesture(kGestureFourFingerSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
495 /* dx= */ 5, /* dy= */ 0);
496 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
497 ASSERT_EQ(1u, args.size());
498 arg = std::get<NotifyMotionArgs>(args.front());
499 ASSERT_THAT(arg,
500 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
501 WithGestureOffset(0.005, 0, EPSILON),
502 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
503 WithPointerCount(4u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
504 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 15);
505 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() + 15);
506 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() + 15);
507 EXPECT_EQ(arg.pointerCoords[3].getX(), finger3Start.getX() + 15);
508 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
509 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
510 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
511 EXPECT_EQ(arg.pointerCoords[3].getY(), finger3Start.getY());
512
513 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
514 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
515 ASSERT_EQ(4u, args.size());
516 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
517 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
518 3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
519 WithGestureOffset(0, 0, EPSILON),
520 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
521 WithPointerCount(4u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
522 args.pop_front();
523 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
524 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
525 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
526 WithGestureOffset(0, 0, EPSILON),
527 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
528 WithPointerCount(3u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
529 args.pop_front();
530 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
531 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
532 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
533 WithGestureOffset(0, 0, EPSILON),
534 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
535 WithPointerCount(2u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
536 args.pop_front();
537 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
538 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON),
539 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
540 WithPointerCount(1u), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER)));
541}
542
Harry Cutts4fb941a2022-12-14 19:14:04 +0000543} // namespace android