• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
32 #include "ui/Rotation.h"
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;
41     static constexpr int32_t EVENTHUB_ID = 1;
42     static constexpr stime_t ARBITRARY_GESTURE_TIME = 1.2;
43     static constexpr float POINTER_X = 500;
44     static constexpr float POINTER_Y = 200;
45 
SetUp()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);
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);
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 
newDevice()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 
77     std::shared_ptr<FakeEventHub> mFakeEventHub;
78     sp<FakeInputReaderPolicy> mFakePolicy;
79     std::unique_ptr<TestInputListener> mFakeListener;
80     std::unique_ptr<InstrumentedInputReader> mReader;
81     std::shared_ptr<InputDevice> mDevice;
82     std::shared_ptr<FakePointerController> mFakePointerController;
83 };
84 
TEST_F(GestureConverterTest,Move)85 TEST_F(GestureConverterTest, Move) {
86     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
87     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
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(ToolType::FINGER), WithButtonState(0),
97                       WithPressure(0.0f)));
98 
99     ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(POINTER_X - 5, POINTER_Y + 10));
100 }
101 
TEST_F(GestureConverterTest,Move_Rotated)102 TEST_F(GestureConverterTest, Move_Rotated) {
103     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
104     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
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(ToolType::FINGER), WithButtonState(0),
115                       WithPressure(0.0f)));
116 
117     ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(POINTER_X + 10, POINTER_Y + 5));
118 }
119 
TEST_F(GestureConverterTest,ButtonsChange)120 TEST_F(GestureConverterTest, ButtonsChange) {
121     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
122     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
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(ToolType::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(ToolType::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(ToolType::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(ToolType::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(3u, 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(ToolType::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(ToolType::FINGER)));
184     args.pop_front();
185     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
186                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithButtonState(0),
187                       WithCoords(POINTER_X, POINTER_Y), WithToolType(ToolType::FINGER)));
188 }
189 
TEST_F(GestureConverterTest,DragWithButton)190 TEST_F(GestureConverterTest, DragWithButton) {
191     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
192     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
193 
194     // Press the button
195     Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
196                         /* down= */ GESTURES_BUTTON_LEFT, /* up= */ GESTURES_BUTTON_NONE,
197                         /* is_tap= */ false);
198     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, downGesture);
199     ASSERT_EQ(2u, args.size());
200 
201     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
202                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
203                       WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
204                       WithCoords(POINTER_X, POINTER_Y),
205                       WithToolType(ToolType::FINGER)));
206     args.pop_front();
207     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
208                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
209                       WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
210                       WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
211                       WithCoords(POINTER_X, POINTER_Y),
212                       WithToolType(ToolType::FINGER)));
213 
214     // Move
215     Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
216     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture);
217     ASSERT_EQ(1u, args.size());
218 
219     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
220                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
221                       WithCoords(POINTER_X - 5, POINTER_Y + 10), WithRelativeMotion(-5, 10),
222                       WithToolType(ToolType::FINGER),
223                       WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY), WithPressure(1.0f)));
224 
225     ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(POINTER_X - 5, POINTER_Y + 10));
226 
227     // Release the button
228     Gesture upGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
229                       /* down= */ GESTURES_BUTTON_NONE, /* up= */ GESTURES_BUTTON_LEFT,
230                       /* is_tap= */ false);
231     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, upGesture);
232     ASSERT_EQ(3u, args.size());
233 
234     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
235                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
236                       WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), WithButtonState(0),
237                       WithCoords(POINTER_X - 5, POINTER_Y + 10),
238                       WithToolType(ToolType::FINGER)));
239     args.pop_front();
240     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
241                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0),
242                       WithCoords(POINTER_X - 5, POINTER_Y + 10),
243                       WithToolType(ToolType::FINGER)));
244     args.pop_front();
245     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
246                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithButtonState(0),
247                       WithCoords(POINTER_X - 5, POINTER_Y + 10), WithToolType(ToolType::FINGER)));
248 }
249 
TEST_F(GestureConverterTest,Scroll)250 TEST_F(GestureConverterTest, Scroll) {
251     const nsecs_t downTime = 12345;
252     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
253     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
254 
255     Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
256     std::list<NotifyArgs> args = converter.handleGesture(downTime, READ_TIME, startGesture);
257     ASSERT_EQ(2u, args.size());
258 
259     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
260                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithCoords(POINTER_X, POINTER_Y),
261                       WithGestureScrollDistance(0, 0, EPSILON),
262                       WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
263                       WithToolType(ToolType::FINGER), WithDownTime(downTime),
264                       WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE)));
265     args.pop_front();
266     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
267                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
268                       WithCoords(POINTER_X, POINTER_Y - 10),
269                       WithGestureScrollDistance(0, 10, EPSILON),
270                       WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
271                       WithToolType(ToolType::FINGER),
272                       WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE)));
273 
274     Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
275     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
276     ASSERT_EQ(1u, args.size());
277     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
278                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
279                       WithCoords(POINTER_X, POINTER_Y - 15),
280                       WithGestureScrollDistance(0, 5, EPSILON),
281                       WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
282                       WithToolType(ToolType::FINGER),
283                       WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE)));
284 
285     Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
286                          GESTURES_FLING_START);
287     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
288     ASSERT_EQ(1u, args.size());
289     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
290                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
291                       WithCoords(POINTER_X, POINTER_Y - 15),
292                       WithGestureScrollDistance(0, 0, EPSILON),
293                       WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
294                       WithToolType(ToolType::FINGER),
295                       WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE)));
296 }
297 
TEST_F(GestureConverterTest,Scroll_Rotated)298 TEST_F(GestureConverterTest, Scroll_Rotated) {
299     const nsecs_t downTime = 12345;
300     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
301     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
302     converter.setOrientation(ui::ROTATION_90);
303 
304     Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
305     std::list<NotifyArgs> args = converter.handleGesture(downTime, READ_TIME, startGesture);
306     ASSERT_EQ(2u, args.size());
307 
308     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
309                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithCoords(POINTER_X, POINTER_Y),
310                       WithGestureScrollDistance(0, 0, EPSILON),
311                       WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
312                       WithToolType(ToolType::FINGER), WithDownTime(downTime)));
313     args.pop_front();
314     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
315                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
316                       WithCoords(POINTER_X - 10, POINTER_Y),
317                       WithGestureScrollDistance(0, 10, EPSILON),
318                       WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
319                       WithToolType(ToolType::FINGER)));
320 
321     Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
322     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
323     ASSERT_EQ(1u, args.size());
324     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
325                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
326                       WithCoords(POINTER_X - 15, POINTER_Y),
327                       WithGestureScrollDistance(0, 5, EPSILON),
328                       WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
329                       WithToolType(ToolType::FINGER)));
330 
331     Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
332                          GESTURES_FLING_START);
333     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
334     ASSERT_EQ(1u, args.size());
335     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
336                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
337                       WithCoords(POINTER_X - 15, POINTER_Y),
338                       WithGestureScrollDistance(0, 0, EPSILON),
339                       WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
340                       WithToolType(ToolType::FINGER)));
341 }
342 
TEST_F(GestureConverterTest,Scroll_ClearsClassificationAfterGesture)343 TEST_F(GestureConverterTest, Scroll_ClearsClassificationAfterGesture) {
344     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
345     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
346 
347     Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
348     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
349 
350     Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
351     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
352 
353     Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
354                          GESTURES_FLING_START);
355     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
356 
357     Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
358     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture);
359     ASSERT_EQ(1u, args.size());
360     EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
361                 WithMotionClassification(MotionClassification::NONE));
362 }
363 
TEST_F(GestureConverterTest,Scroll_ClearsScrollDistanceAfterGesture)364 TEST_F(GestureConverterTest, Scroll_ClearsScrollDistanceAfterGesture) {
365     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
366     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
367 
368     Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
369     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
370 
371     Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
372     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
373 
374     Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
375                          GESTURES_FLING_START);
376     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
377 
378     // Move gestures don't use the fake finger array, so to test that gesture axes are cleared we
379     // need to use another gesture type, like pinch.
380     Gesture pinchGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
381                          GESTURES_ZOOM_START);
382     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, pinchGesture);
383     ASSERT_FALSE(args.empty());
384     EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()), WithGestureScrollDistance(0, 0, EPSILON));
385 }
386 
TEST_F(GestureConverterTest,ThreeFingerSwipe_ClearsClassificationAfterGesture)387 TEST_F(GestureConverterTest, ThreeFingerSwipe_ClearsClassificationAfterGesture) {
388     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
389     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
390 
391     Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/0,
392                          /*dy=*/0);
393     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
394 
395     Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
396     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
397 
398     Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/-5,
399                         /*dy=*/10);
400     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture);
401     ASSERT_EQ(1u, args.size());
402     EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
403                 WithMotionClassification(MotionClassification::NONE));
404 }
405 
TEST_F(GestureConverterTest,ThreeFingerSwipe_ClearsGestureAxesAfterGesture)406 TEST_F(GestureConverterTest, ThreeFingerSwipe_ClearsGestureAxesAfterGesture) {
407     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
408     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
409 
410     Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/5,
411                          /*dy=*/5);
412     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
413 
414     Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
415     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
416 
417     // Move gestures don't use the fake finger array, so to test that gesture axes are cleared we
418     // need to use another gesture type, like pinch.
419     Gesture pinchGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
420                          GESTURES_ZOOM_START);
421     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, pinchGesture);
422     ASSERT_FALSE(args.empty());
423     EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
424                 AllOf(WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(0)));
425 }
426 
TEST_F(GestureConverterTest,ThreeFingerSwipe_Vertical)427 TEST_F(GestureConverterTest, ThreeFingerSwipe_Vertical) {
428     // The gestures library will "lock" a swipe into the dimension it starts in. For example, if you
429     // start swiping up and then start moving left or right, it'll return gesture events with only Y
430     // deltas until you lift your fingers and start swiping again. That's why each of these tests
431     // only checks movement in one dimension.
432     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
433     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
434 
435     Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ 0,
436                          /* dy= */ 10);
437     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
438     ASSERT_EQ(4u, args.size());
439 
440     // Three fake fingers should be created. We don't actually care where they are, so long as they
441     // move appropriately.
442     NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
443     ASSERT_THAT(arg,
444                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
445                       WithGestureSwipeFingerCount(3),
446                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
447                       WithPointerCount(1u), WithToolType(ToolType::FINGER)));
448     PointerCoords finger0Start = arg.pointerCoords[0];
449     args.pop_front();
450     arg = std::get<NotifyMotionArgs>(args.front());
451     ASSERT_THAT(arg,
452                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
453                                        1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
454                       WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(3),
455                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
456                       WithPointerCount(2u), WithToolType(ToolType::FINGER)));
457     PointerCoords finger1Start = arg.pointerCoords[1];
458     args.pop_front();
459     arg = std::get<NotifyMotionArgs>(args.front());
460     ASSERT_THAT(arg,
461                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
462                                        2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
463                       WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(3),
464                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
465                       WithPointerCount(3u), WithToolType(ToolType::FINGER)));
466     PointerCoords finger2Start = arg.pointerCoords[2];
467     args.pop_front();
468 
469     arg = std::get<NotifyMotionArgs>(args.front());
470     ASSERT_THAT(arg,
471                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
472                       WithGestureOffset(0, -0.01, EPSILON), WithGestureSwipeFingerCount(3),
473                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
474                       WithPointerCount(3u), WithToolType(ToolType::FINGER)));
475     EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX());
476     EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX());
477     EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX());
478     EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY() - 10);
479     EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY() - 10);
480     EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY() - 10);
481 
482     Gesture continueGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
483                             /* dx= */ 0, /* dy= */ 5);
484     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
485     ASSERT_EQ(1u, args.size());
486     arg = std::get<NotifyMotionArgs>(args.front());
487     ASSERT_THAT(arg,
488                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
489                       WithGestureOffset(0, -0.005, EPSILON), WithGestureSwipeFingerCount(3),
490                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
491                       WithPointerCount(3u), WithToolType(ToolType::FINGER)));
492     EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX());
493     EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX());
494     EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX());
495     EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY() - 15);
496     EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY() - 15);
497     EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY() - 15);
498 
499     Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
500     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
501     ASSERT_EQ(3u, args.size());
502     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
503                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
504                                        2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
505                       WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(3),
506                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
507                       WithPointerCount(3u), WithToolType(ToolType::FINGER)));
508     args.pop_front();
509     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
510                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
511                                        1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
512                       WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(3),
513                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
514                       WithPointerCount(2u), WithToolType(ToolType::FINGER)));
515     args.pop_front();
516     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
517                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON),
518                       WithGestureSwipeFingerCount(3),
519                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
520                       WithPointerCount(1u), WithToolType(ToolType::FINGER)));
521 }
522 
TEST_F(GestureConverterTest,ThreeFingerSwipe_Rotated)523 TEST_F(GestureConverterTest, ThreeFingerSwipe_Rotated) {
524     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
525     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
526     converter.setOrientation(ui::ROTATION_90);
527 
528     Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ 0,
529                          /* dy= */ 10);
530     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
531     ASSERT_EQ(4u, args.size());
532 
533     // Three fake fingers should be created. We don't actually care where they are, so long as they
534     // move appropriately.
535     NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
536     ASSERT_THAT(arg,
537                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
538                       WithPointerCount(1u)));
539     PointerCoords finger0Start = arg.pointerCoords[0];
540     args.pop_front();
541     arg = std::get<NotifyMotionArgs>(args.front());
542     ASSERT_THAT(arg,
543                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
544                                        1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
545                       WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u)));
546     PointerCoords finger1Start = arg.pointerCoords[1];
547     args.pop_front();
548     arg = std::get<NotifyMotionArgs>(args.front());
549     ASSERT_THAT(arg,
550                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
551                                        2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
552                       WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u)));
553     PointerCoords finger2Start = arg.pointerCoords[2];
554     args.pop_front();
555 
556     arg = std::get<NotifyMotionArgs>(args.front());
557     ASSERT_THAT(arg,
558                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
559                       WithGestureOffset(0, -0.01, EPSILON), WithPointerCount(3u)));
560     EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() - 10);
561     EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() - 10);
562     EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() - 10);
563     EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
564     EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
565     EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
566 
567     Gesture continueGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
568                             /* dx= */ 0, /* dy= */ 5);
569     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
570     ASSERT_EQ(1u, args.size());
571     arg = std::get<NotifyMotionArgs>(args.front());
572     ASSERT_THAT(arg,
573                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
574                       WithGestureOffset(0, -0.005, EPSILON), WithPointerCount(3u)));
575     EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() - 15);
576     EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() - 15);
577     EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() - 15);
578     EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
579     EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
580     EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
581 
582     Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
583     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
584     ASSERT_EQ(3u, args.size());
585     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
586                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
587                                        2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
588                       WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u)));
589     args.pop_front();
590     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
591                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
592                                        1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
593                       WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u)));
594     args.pop_front();
595     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
596                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON),
597                       WithPointerCount(1u)));
598 }
599 
TEST_F(GestureConverterTest,FourFingerSwipe_Horizontal)600 TEST_F(GestureConverterTest, FourFingerSwipe_Horizontal) {
601     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
602     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
603 
604     Gesture startGesture(kGestureFourFingerSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
605                          /* dx= */ 10, /* dy= */ 0);
606     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
607     ASSERT_EQ(5u, args.size());
608 
609     // Four fake fingers should be created. We don't actually care where they are, so long as they
610     // move appropriately.
611     NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
612     ASSERT_THAT(arg,
613                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
614                       WithGestureSwipeFingerCount(4),
615                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
616                       WithPointerCount(1u), WithToolType(ToolType::FINGER)));
617     PointerCoords finger0Start = arg.pointerCoords[0];
618     args.pop_front();
619     arg = std::get<NotifyMotionArgs>(args.front());
620     ASSERT_THAT(arg,
621                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
622                                        1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
623                       WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
624                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
625                       WithPointerCount(2u), WithToolType(ToolType::FINGER)));
626     PointerCoords finger1Start = arg.pointerCoords[1];
627     args.pop_front();
628     arg = std::get<NotifyMotionArgs>(args.front());
629     ASSERT_THAT(arg,
630                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
631                                        2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
632                       WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
633                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
634                       WithPointerCount(3u), WithToolType(ToolType::FINGER)));
635     PointerCoords finger2Start = arg.pointerCoords[2];
636     args.pop_front();
637     arg = std::get<NotifyMotionArgs>(args.front());
638     ASSERT_THAT(arg,
639                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
640                                        3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
641                       WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
642                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
643                       WithPointerCount(4u), WithToolType(ToolType::FINGER)));
644     PointerCoords finger3Start = arg.pointerCoords[3];
645     args.pop_front();
646 
647     arg = std::get<NotifyMotionArgs>(args.front());
648     ASSERT_THAT(arg,
649                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
650                       WithGestureOffset(0.01, 0, EPSILON), WithGestureSwipeFingerCount(4),
651                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
652                       WithPointerCount(4u), WithToolType(ToolType::FINGER)));
653     EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 10);
654     EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() + 10);
655     EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() + 10);
656     EXPECT_EQ(arg.pointerCoords[3].getX(), finger3Start.getX() + 10);
657     EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
658     EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
659     EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
660     EXPECT_EQ(arg.pointerCoords[3].getY(), finger3Start.getY());
661 
662     Gesture continueGesture(kGestureFourFingerSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
663                             /* dx= */ 5, /* dy= */ 0);
664     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, continueGesture);
665     ASSERT_EQ(1u, args.size());
666     arg = std::get<NotifyMotionArgs>(args.front());
667     ASSERT_THAT(arg,
668                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
669                       WithGestureOffset(0.005, 0, EPSILON), WithGestureSwipeFingerCount(4),
670                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
671                       WithPointerCount(4u), WithToolType(ToolType::FINGER)));
672     EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 15);
673     EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() + 15);
674     EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() + 15);
675     EXPECT_EQ(arg.pointerCoords[3].getX(), finger3Start.getX() + 15);
676     EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
677     EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
678     EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
679     EXPECT_EQ(arg.pointerCoords[3].getY(), finger3Start.getY());
680 
681     Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
682     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, liftGesture);
683     ASSERT_EQ(4u, args.size());
684     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
685                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
686                                        3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
687                       WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
688                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
689                       WithPointerCount(4u), WithToolType(ToolType::FINGER)));
690     args.pop_front();
691     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
692                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
693                                        2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
694                       WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
695                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
696                       WithPointerCount(3u), WithToolType(ToolType::FINGER)));
697     args.pop_front();
698     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
699                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
700                                        1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
701                       WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(4),
702                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
703                       WithPointerCount(2u), WithToolType(ToolType::FINGER)));
704     args.pop_front();
705     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
706                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON),
707                       WithGestureSwipeFingerCount(4),
708                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
709                       WithPointerCount(1u), WithToolType(ToolType::FINGER)));
710 }
711 
TEST_F(GestureConverterTest,Pinch_Inwards)712 TEST_F(GestureConverterTest, Pinch_Inwards) {
713     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
714     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
715 
716     Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
717                          GESTURES_ZOOM_START);
718     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
719     ASSERT_EQ(2u, args.size());
720     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
721                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
722                       WithMotionClassification(MotionClassification::PINCH),
723                       WithGesturePinchScaleFactor(1.0f, EPSILON),
724                       WithCoords(POINTER_X - 100, POINTER_Y), WithPointerCount(1u),
725                       WithToolType(ToolType::FINGER)));
726     args.pop_front();
727     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
728                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
729                                        1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
730                       WithMotionClassification(MotionClassification::PINCH),
731                       WithGesturePinchScaleFactor(1.0f, EPSILON),
732                       WithPointerCoords(1, POINTER_X + 100, POINTER_Y), WithPointerCount(2u),
733                       WithToolType(ToolType::FINGER)));
734 
735     Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
736                           /* dz= */ 0.8, GESTURES_ZOOM_UPDATE);
737     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, updateGesture);
738     ASSERT_EQ(1u, args.size());
739     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
740                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
741                       WithMotionClassification(MotionClassification::PINCH),
742                       WithGesturePinchScaleFactor(0.8f, EPSILON),
743                       WithPointerCoords(0, POINTER_X - 80, POINTER_Y),
744                       WithPointerCoords(1, POINTER_X + 80, POINTER_Y), WithPointerCount(2u),
745                       WithToolType(ToolType::FINGER)));
746 
747     Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
748                        GESTURES_ZOOM_END);
749     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, endGesture);
750     ASSERT_EQ(2u, args.size());
751     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
752                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
753                                        1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
754                       WithMotionClassification(MotionClassification::PINCH),
755                       WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(2u),
756                       WithToolType(ToolType::FINGER)));
757     args.pop_front();
758     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
759                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
760                       WithMotionClassification(MotionClassification::PINCH),
761                       WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(1u),
762                       WithToolType(ToolType::FINGER)));
763 }
764 
TEST_F(GestureConverterTest,Pinch_Outwards)765 TEST_F(GestureConverterTest, Pinch_Outwards) {
766     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
767     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
768 
769     Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
770                          GESTURES_ZOOM_START);
771     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
772     ASSERT_EQ(2u, args.size());
773     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
774                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
775                       WithMotionClassification(MotionClassification::PINCH),
776                       WithGesturePinchScaleFactor(1.0f, EPSILON),
777                       WithCoords(POINTER_X - 100, POINTER_Y), WithPointerCount(1u),
778                       WithToolType(ToolType::FINGER)));
779     args.pop_front();
780     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
781                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
782                                        1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
783                       WithMotionClassification(MotionClassification::PINCH),
784                       WithGesturePinchScaleFactor(1.0f, EPSILON),
785                       WithPointerCoords(1, POINTER_X + 100, POINTER_Y), WithPointerCount(2u),
786                       WithToolType(ToolType::FINGER)));
787 
788     Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
789                           /* dz= */ 1.2, GESTURES_ZOOM_UPDATE);
790     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, updateGesture);
791     ASSERT_EQ(1u, args.size());
792     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
793                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
794                       WithMotionClassification(MotionClassification::PINCH),
795                       WithGesturePinchScaleFactor(1.2f, EPSILON),
796                       WithPointerCoords(0, POINTER_X - 120, POINTER_Y),
797                       WithPointerCoords(1, POINTER_X + 120, POINTER_Y), WithPointerCount(2u),
798                       WithToolType(ToolType::FINGER)));
799 
800     Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
801                        GESTURES_ZOOM_END);
802     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, endGesture);
803     ASSERT_EQ(2u, args.size());
804     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
805                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
806                                        1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
807                       WithMotionClassification(MotionClassification::PINCH),
808                       WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(2u),
809                       WithToolType(ToolType::FINGER)));
810     args.pop_front();
811     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
812                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
813                       WithMotionClassification(MotionClassification::PINCH),
814                       WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(1u),
815                       WithToolType(ToolType::FINGER)));
816 }
817 
TEST_F(GestureConverterTest,Pinch_ClearsClassificationAfterGesture)818 TEST_F(GestureConverterTest, Pinch_ClearsClassificationAfterGesture) {
819     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
820     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
821 
822     Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
823                          GESTURES_ZOOM_START);
824     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
825 
826     Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
827                           /*dz=*/1.2, GESTURES_ZOOM_UPDATE);
828     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, updateGesture);
829 
830     Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
831                        GESTURES_ZOOM_END);
832     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, endGesture);
833 
834     Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
835     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture);
836     ASSERT_EQ(1u, args.size());
837     EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
838                 WithMotionClassification(MotionClassification::NONE));
839 }
840 
TEST_F(GestureConverterTest,Pinch_ClearsScaleFactorAfterGesture)841 TEST_F(GestureConverterTest, Pinch_ClearsScaleFactorAfterGesture) {
842     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
843     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
844 
845     Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
846                          GESTURES_ZOOM_START);
847     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
848 
849     Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
850                           /*dz=*/1.2, GESTURES_ZOOM_UPDATE);
851     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, updateGesture);
852 
853     Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
854                        GESTURES_ZOOM_END);
855     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, endGesture);
856 
857     // Move gestures don't use the fake finger array, so to test that gesture axes are cleared we
858     // need to use another gesture type, like scroll.
859     Gesture scrollGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/1,
860                           /*dy=*/0);
861     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, scrollGesture);
862     ASSERT_FALSE(args.empty());
863     EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()), WithGesturePinchScaleFactor(0, EPSILON));
864 }
865 
TEST_F(GestureConverterTest,ResetWithButtonPressed)866 TEST_F(GestureConverterTest, ResetWithButtonPressed) {
867     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
868     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
869 
870     Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
871                         /*down=*/GESTURES_BUTTON_LEFT | GESTURES_BUTTON_RIGHT,
872                         /*up=*/GESTURES_BUTTON_NONE, /*is_tap=*/false);
873     (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, downGesture);
874 
875     std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
876     ASSERT_EQ(3u, args.size());
877 
878     EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
879                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
880                       WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
881                       WithButtonState(AMOTION_EVENT_BUTTON_SECONDARY),
882                       WithCoords(POINTER_X, POINTER_Y),
883                       WithToolType(ToolType::FINGER)));
884     args.pop_front();
885     EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
886                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
887                       WithActionButton(AMOTION_EVENT_BUTTON_SECONDARY), WithButtonState(0),
888                       WithCoords(POINTER_X, POINTER_Y),
889                       WithToolType(ToolType::FINGER)));
890     args.pop_front();
891     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
892                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0),
893                       WithCoords(POINTER_X, POINTER_Y),
894                       WithToolType(ToolType::FINGER)));
895 }
896 
TEST_F(GestureConverterTest,ResetDuringScroll)897 TEST_F(GestureConverterTest, ResetDuringScroll) {
898     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
899     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
900 
901     Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
902     (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
903 
904     std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
905     ASSERT_EQ(1u, args.size());
906     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
907                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
908                       WithCoords(POINTER_X, POINTER_Y - 10),
909                       WithGestureScrollDistance(0, 0, EPSILON),
910                       WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
911                       WithToolType(ToolType::FINGER),
912                       WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE)));
913 }
914 
TEST_F(GestureConverterTest,ResetDuringThreeFingerSwipe)915 TEST_F(GestureConverterTest, ResetDuringThreeFingerSwipe) {
916     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
917     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
918 
919     Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/0,
920                          /*dy=*/10);
921     (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
922 
923     std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
924     ASSERT_EQ(3u, args.size());
925     EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
926                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
927                                        2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
928                       WithGestureOffset(0, 0, EPSILON),
929                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
930                       WithPointerCount(3u), WithToolType(ToolType::FINGER)));
931     args.pop_front();
932     EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
933                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
934                                        1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
935                       WithGestureOffset(0, 0, EPSILON),
936                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
937                       WithPointerCount(2u), WithToolType(ToolType::FINGER)));
938     args.pop_front();
939     EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
940                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithGestureOffset(0, 0, EPSILON),
941                       WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
942                       WithPointerCount(1u), WithToolType(ToolType::FINGER)));
943 }
944 
TEST_F(GestureConverterTest,ResetDuringPinch)945 TEST_F(GestureConverterTest, ResetDuringPinch) {
946     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
947     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
948 
949     Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
950                          GESTURES_ZOOM_START);
951     (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, startGesture);
952 
953     std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
954     ASSERT_EQ(2u, args.size());
955     EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
956                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_UP |
957                                        1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
958                       WithMotionClassification(MotionClassification::PINCH),
959                       WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(2u),
960                       WithToolType(ToolType::FINGER)));
961     args.pop_front();
962     EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
963                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
964                       WithMotionClassification(MotionClassification::PINCH),
965                       WithGesturePinchScaleFactor(1.0f, EPSILON), WithPointerCount(1u),
966                       WithToolType(ToolType::FINGER)));
967 }
968 
TEST_F(GestureConverterTest,FlingTapDown)969 TEST_F(GestureConverterTest, FlingTapDown) {
970     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
971     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
972 
973     Gesture tapDownGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
974                            /*vx=*/0.f, /*vy=*/0.f, GESTURES_FLING_TAP_DOWN);
975     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, tapDownGesture);
976 
977     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
978                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
979                       WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0.f, 0.f),
980                       WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f)));
981 
982     ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(POINTER_X, POINTER_Y));
983     ASSERT_TRUE(mFakePointerController->isPointerShown());
984 }
985 
TEST_F(GestureConverterTest,Tap)986 TEST_F(GestureConverterTest, Tap) {
987     // Tap should produce button press/release events
988     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
989     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
990 
991     Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* vx= */ 0,
992                          /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
993     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
994 
995     ASSERT_EQ(1u, args.size());
996     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
997                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
998                       WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0, 0),
999                       WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f)));
1000 
1001     Gesture tapGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1002                        /* down= */ GESTURES_BUTTON_LEFT,
1003                        /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ true);
1004     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, tapGesture);
1005 
1006     ASSERT_EQ(5u, args.size());
1007     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1008                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithCoords(POINTER_X, POINTER_Y),
1009                       WithRelativeMotion(0.f, 0.f), WithToolType(ToolType::FINGER),
1010                       WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY), WithPressure(1.0f)));
1011     args.pop_front();
1012     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1013                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1014                       WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1015                       WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
1016                       WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0.f, 0.f),
1017                       WithToolType(ToolType::FINGER), WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
1018                       WithPressure(1.0f)));
1019     args.pop_front();
1020     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1021                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1022                       WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), WithButtonState(0),
1023                       WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0.f, 0.f),
1024                       WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(1.0f)));
1025     args.pop_front();
1026     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1027                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithCoords(POINTER_X, POINTER_Y),
1028                       WithRelativeMotion(0.f, 0.f), WithToolType(ToolType::FINGER),
1029                       WithButtonState(0), WithPressure(0.0f)));
1030     args.pop_front();
1031     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1032                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1033                       WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0, 0),
1034                       WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f)));
1035 }
1036 
TEST_F(GestureConverterTest,Click)1037 TEST_F(GestureConverterTest, Click) {
1038     // Click should produce button press/release events
1039     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1040     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1041 
1042     Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* vx= */ 0,
1043                          /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
1044     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
1045 
1046     ASSERT_EQ(1u, args.size());
1047     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1048                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1049                       WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0, 0),
1050                       WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f)));
1051 
1052     Gesture buttonDownGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1053                               /* down= */ GESTURES_BUTTON_LEFT,
1054                               /* up= */ GESTURES_BUTTON_NONE, /* is_tap= */ false);
1055     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, buttonDownGesture);
1056 
1057     ASSERT_EQ(2u, args.size());
1058     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1059                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithCoords(POINTER_X, POINTER_Y),
1060                       WithRelativeMotion(0.f, 0.f), WithToolType(ToolType::FINGER),
1061                       WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY), WithPressure(1.0f)));
1062     args.pop_front();
1063     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1064                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1065                       WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1066                       WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
1067                       WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0.f, 0.f),
1068                       WithToolType(ToolType::FINGER), WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
1069                       WithPressure(1.0f)));
1070 
1071     Gesture buttonUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1072                             /* down= */ GESTURES_BUTTON_NONE,
1073                             /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ false);
1074     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, buttonUpGesture);
1075 
1076     ASSERT_EQ(3u, args.size());
1077     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1078                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1079                       WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), WithButtonState(0),
1080                       WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0.f, 0.f),
1081                       WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(1.0f)));
1082     args.pop_front();
1083     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1084                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithCoords(POINTER_X, POINTER_Y),
1085                       WithRelativeMotion(0.f, 0.f), WithToolType(ToolType::FINGER),
1086                       WithButtonState(0), WithPressure(0.0f)));
1087     args.pop_front();
1088     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1089                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1090                       WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0, 0),
1091                       WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f)));
1092 }
1093 
TEST_F(GestureConverterTest,TapWithTapToClickDisabled)1094 TEST_F(GestureConverterTest, TapWithTapToClickDisabled) {
1095     // Tap should be ignored when disabled
1096     mReader->getContext()->setPreventingTouchpadTaps(true);
1097 
1098     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1099     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1100 
1101     Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* vx= */ 0,
1102                          /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
1103     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
1104 
1105     ASSERT_EQ(1u, args.size());
1106     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1107                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1108                       WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0, 0),
1109                       WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f)));
1110     args.pop_front();
1111 
1112     Gesture tapGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1113                        /* down= */ GESTURES_BUTTON_LEFT,
1114                        /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ true);
1115     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, tapGesture);
1116 
1117     // no events should be generated
1118     ASSERT_EQ(0u, args.size());
1119 
1120     // Future taps should be re-enabled
1121     ASSERT_FALSE(mReader->getContext()->isPreventingTouchpadTaps());
1122 }
1123 
TEST_F(GestureConverterTest,ClickWithTapToClickDisabled)1124 TEST_F(GestureConverterTest, ClickWithTapToClickDisabled) {
1125     // Click should still produce button press/release events
1126     mReader->getContext()->setPreventingTouchpadTaps(true);
1127 
1128     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1129     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1130 
1131     Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* vx= */ 0,
1132                          /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
1133     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, flingGesture);
1134 
1135     ASSERT_EQ(1u, args.size());
1136     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1137                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1138                       WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0, 0),
1139                       WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f)));
1140 
1141     Gesture buttonDownGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1142                               /* down= */ GESTURES_BUTTON_LEFT,
1143                               /* up= */ GESTURES_BUTTON_NONE, /* is_tap= */ false);
1144     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, buttonDownGesture);
1145     ASSERT_EQ(2u, args.size());
1146 
1147     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1148                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithCoords(POINTER_X, POINTER_Y),
1149                       WithRelativeMotion(0.f, 0.f), WithToolType(ToolType::FINGER),
1150                       WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY), WithPressure(1.0f)));
1151     args.pop_front();
1152     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1153                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1154                       WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1155                       WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
1156                       WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0.f, 0.f),
1157                       WithToolType(ToolType::FINGER), WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
1158                       WithPressure(1.0f)));
1159 
1160     Gesture buttonUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1161                             /* down= */ GESTURES_BUTTON_NONE,
1162                             /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ false);
1163     args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, buttonUpGesture);
1164 
1165     ASSERT_EQ(3u, args.size());
1166     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1167                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1168                       WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY), WithButtonState(0),
1169                       WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0.f, 0.f),
1170                       WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(1.0f)));
1171     args.pop_front();
1172     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1173                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithCoords(POINTER_X, POINTER_Y),
1174                       WithRelativeMotion(0.f, 0.f), WithToolType(ToolType::FINGER),
1175                       WithButtonState(0), WithPressure(0.0f)));
1176     args.pop_front();
1177     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1178                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1179                       WithCoords(POINTER_X, POINTER_Y), WithRelativeMotion(0, 0),
1180                       WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f)));
1181 
1182     // Future taps should be re-enabled
1183     ASSERT_FALSE(mReader->getContext()->isPreventingTouchpadTaps());
1184 }
1185 
TEST_F(GestureConverterTest,MoveEnablesTapToClick)1186 TEST_F(GestureConverterTest, MoveEnablesTapToClick) {
1187     // initially disable tap-to-click
1188     mReader->getContext()->setPreventingTouchpadTaps(true);
1189 
1190     InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1191     GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1192 
1193     Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
1194     std::list<NotifyArgs> args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, moveGesture);
1195     ASSERT_EQ(1u, args.size());
1196 
1197     ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1198                 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1199                       WithCoords(POINTER_X - 5, POINTER_Y + 10), WithRelativeMotion(-5, 10),
1200                       WithToolType(ToolType::FINGER), WithButtonState(0), WithPressure(0.0f)));
1201 
1202     ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(POINTER_X - 5, POINTER_Y + 10));
1203 
1204     // Future taps should be re-enabled
1205     ASSERT_FALSE(mReader->getContext()->isPreventingTouchpadTaps());
1206 }
1207 
1208 } // namespace android
1209