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 <com_android_input_flags.h>
20 #include <flag_macros.h>
21 #include <gestures/GestureConverter.h>
22 #include <gtest/gtest.h>
23
24 #include "FakeEventHub.h"
25 #include "FakeInputReaderPolicy.h"
26 #include "FakePointerController.h"
27 #include "InstrumentedInputReader.h"
28 #include "NotifyArgs.h"
29 #include "TestConstants.h"
30 #include "TestEventMatchers.h"
31 #include "TestInputListener.h"
32 #include "include/gestures.h"
33 #include "ui/Rotation.h"
34
35 namespace android {
36
37 namespace input_flags = com::android::input::flags;
38
39 namespace {
40
41 const auto TOUCHPAD_PALM_REJECTION =
42 ACONFIG_FLAG(input_flags, enable_touchpad_typing_palm_rejection);
43 const auto TOUCHPAD_PALM_REJECTION_V2 =
44 ACONFIG_FLAG(input_flags, enable_v2_touchpad_typing_palm_rejection);
45
46 } // namespace
47
48 using testing::AllOf;
49 using testing::Each;
50 using testing::ElementsAre;
51 using testing::VariantWith;
52
53 class GestureConverterTest : public testing::Test {
54 protected:
55 static constexpr int32_t DEVICE_ID = END_RESERVED_ID + 1000;
56 static constexpr int32_t EVENTHUB_ID = 1;
57 static constexpr stime_t ARBITRARY_GESTURE_TIME = 1.2;
58
SetUp()59 void SetUp() {
60 mFakeEventHub = std::make_unique<FakeEventHub>();
61 mFakePolicy = sp<FakeInputReaderPolicy>::make();
62 mFakeListener = std::make_unique<TestInputListener>();
63 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
64 *mFakeListener);
65 mDevice = newDevice();
66 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, -500, 500, 0, 0, 20);
67 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, -500, 500, 0, 0, 20);
68 }
69
newDevice()70 std::shared_ptr<InputDevice> newDevice() {
71 InputDeviceIdentifier identifier;
72 identifier.name = "device";
73 identifier.location = "USB1";
74 identifier.bus = 0;
75 std::shared_ptr<InputDevice> device =
76 std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, /* generation= */ 2,
77 identifier);
78 mReader->pushNextDevice(device);
79 mFakeEventHub->addDevice(EVENTHUB_ID, identifier.name, InputDeviceClass::TOUCHPAD,
80 identifier.bus);
81 mReader->loopOnce();
82 return device;
83 }
84
85 std::shared_ptr<FakeEventHub> mFakeEventHub;
86 sp<FakeInputReaderPolicy> mFakePolicy;
87 std::unique_ptr<TestInputListener> mFakeListener;
88 std::unique_ptr<InstrumentedInputReader> mReader;
89 std::shared_ptr<InputDevice> mDevice;
90 };
91
TEST_F(GestureConverterTest,Move)92 TEST_F(GestureConverterTest, Move) {
93 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
94 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
95 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
96
97 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
98 std::list<NotifyArgs> args =
99 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
100 ASSERT_THAT(args,
101 ElementsAre(VariantWith<NotifyMotionArgs>(
102 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
103 WithRelativeMotion(0, 0))),
104 VariantWith<NotifyMotionArgs>(
105 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
106 WithRelativeMotion(-5, 10), WithButtonState(0),
107 WithPressure(0.0f)))));
108 ASSERT_THAT(args,
109 Each(VariantWith<NotifyMotionArgs>(
110 AllOf(WithCoords(0, 0), WithToolType(ToolType::FINGER),
111 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
112
113 // The same gesture again should only repeat the HOVER_MOVE, not the HOVER_ENTER.
114 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
115 ASSERT_THAT(args,
116 ElementsAre(VariantWith<NotifyMotionArgs>(
117 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithCoords(0, 0),
118 WithRelativeMotion(-5, 10), WithToolType(ToolType::FINGER),
119 WithButtonState(0), WithPressure(0.0f),
120 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
121 }
122
TEST_F(GestureConverterTest,Move_Rotated)123 TEST_F(GestureConverterTest, Move_Rotated) {
124 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
125 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
126 converter.setOrientation(ui::ROTATION_90);
127 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
128
129 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
130 std::list<NotifyArgs> args =
131 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
132 ASSERT_THAT(args,
133 ElementsAre(VariantWith<NotifyMotionArgs>(
134 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
135 WithRelativeMotion(0, 0))),
136 VariantWith<NotifyMotionArgs>(
137 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
138 WithRelativeMotion(10, 5), WithButtonState(0),
139 WithPressure(0.0f)))));
140 ASSERT_THAT(args,
141 Each(VariantWith<NotifyMotionArgs>(
142 AllOf(WithCoords(0, 0), WithToolType(ToolType::FINGER),
143 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
144 }
145
TEST_F(GestureConverterTest,ButtonsChange)146 TEST_F(GestureConverterTest, ButtonsChange) {
147 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
148 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
149 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
150
151 // Press left and right buttons at once
152 Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
153 /* down= */ GESTURES_BUTTON_LEFT | GESTURES_BUTTON_RIGHT,
154 /* up= */ GESTURES_BUTTON_NONE, /* is_tap= */ false);
155 std::list<NotifyArgs> args =
156 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, downGesture);
157 ASSERT_THAT(args,
158 ElementsAre(VariantWith<NotifyMotionArgs>(
159 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
160 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY |
161 AMOTION_EVENT_BUTTON_SECONDARY))),
162 VariantWith<NotifyMotionArgs>(
163 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
164 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
165 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))),
166 VariantWith<NotifyMotionArgs>(
167 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
168 WithActionButton(AMOTION_EVENT_BUTTON_SECONDARY),
169 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY |
170 AMOTION_EVENT_BUTTON_SECONDARY)))));
171 ASSERT_THAT(args,
172 Each(VariantWith<NotifyMotionArgs>(
173 AllOf(WithCoords(0, 0), WithToolType(ToolType::FINGER),
174 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
175
176 // Then release the left button
177 Gesture leftUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
178 /* down= */ GESTURES_BUTTON_NONE, /* up= */ GESTURES_BUTTON_LEFT,
179 /* is_tap= */ false);
180 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, leftUpGesture);
181 ASSERT_THAT(args,
182 ElementsAre(VariantWith<NotifyMotionArgs>(
183 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
184 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
185 WithButtonState(AMOTION_EVENT_BUTTON_SECONDARY), WithCoords(0, 0),
186 WithToolType(ToolType::FINGER),
187 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
188
189 // Finally release the right button
190 Gesture rightUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
191 /* down= */ GESTURES_BUTTON_NONE, /* up= */ GESTURES_BUTTON_RIGHT,
192 /* is_tap= */ false);
193 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, rightUpGesture);
194 ASSERT_THAT(args,
195 ElementsAre(VariantWith<NotifyMotionArgs>(
196 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
197 WithActionButton(AMOTION_EVENT_BUTTON_SECONDARY))),
198 VariantWith<NotifyMotionArgs>(
199 WithMotionAction(AMOTION_EVENT_ACTION_UP)),
200 VariantWith<NotifyMotionArgs>(
201 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER))));
202 ASSERT_THAT(args,
203 Each(VariantWith<NotifyMotionArgs>(
204 AllOf(WithButtonState(0), WithCoords(0, 0), WithToolType(ToolType::FINGER),
205 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
206 }
207
TEST_F(GestureConverterTest,ButtonDownAfterMoveExitsHover)208 TEST_F(GestureConverterTest, ButtonDownAfterMoveExitsHover) {
209 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
210 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
211 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
212
213 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
214 std::list<NotifyArgs> args =
215 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
216
217 Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
218 /*down=*/GESTURES_BUTTON_LEFT, /*up=*/GESTURES_BUTTON_NONE,
219 /*is_tap=*/false);
220 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, downGesture);
221 ASSERT_THAT(args.front(),
222 VariantWith<NotifyMotionArgs>(
223 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), WithButtonState(0),
224 WithCoords(0, 0), WithToolType(ToolType::FINGER),
225 WithDisplayId(ui::LogicalDisplayId::DEFAULT))));
226 }
227
TEST_F(GestureConverterTest,DragWithButton)228 TEST_F(GestureConverterTest, DragWithButton) {
229 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
230 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
231 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
232
233 // Press the button
234 Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
235 /* down= */ GESTURES_BUTTON_LEFT, /* up= */ GESTURES_BUTTON_NONE,
236 /* is_tap= */ false);
237 std::list<NotifyArgs> args =
238 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, downGesture);
239 ASSERT_THAT(args,
240 ElementsAre(VariantWith<NotifyMotionArgs>(
241 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
242 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))),
243 VariantWith<NotifyMotionArgs>(
244 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
245 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
246 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY)))));
247 ASSERT_THAT(args,
248 Each(VariantWith<NotifyMotionArgs>(
249 AllOf(WithCoords(0, 0), WithToolType(ToolType::FINGER),
250 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
251
252 // Move
253 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
254 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
255 ASSERT_THAT(args,
256 ElementsAre(VariantWith<NotifyMotionArgs>(
257 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithCoords(0, 0),
258 WithRelativeMotion(-5, 10), WithToolType(ToolType::FINGER),
259 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY), WithPressure(1.0f),
260 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
261
262 // Release the button
263 Gesture upGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
264 /* down= */ GESTURES_BUTTON_NONE, /* up= */ GESTURES_BUTTON_LEFT,
265 /* is_tap= */ false);
266 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, upGesture);
267 ASSERT_THAT(args,
268 ElementsAre(VariantWith<NotifyMotionArgs>(
269 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
270 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY))),
271 VariantWith<NotifyMotionArgs>(
272 WithMotionAction(AMOTION_EVENT_ACTION_UP)),
273 VariantWith<NotifyMotionArgs>(
274 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER))));
275 ASSERT_THAT(args,
276 Each(VariantWith<NotifyMotionArgs>(
277 AllOf(WithButtonState(0), WithCoords(0, 0), WithToolType(ToolType::FINGER),
278 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
279 }
280
TEST_F(GestureConverterTest,Scroll)281 TEST_F(GestureConverterTest, Scroll) {
282 const nsecs_t downTime = 12345;
283 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
284 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
285 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
286
287 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
288 std::list<NotifyArgs> args =
289 converter.handleGesture(downTime, READ_TIME, ARBITRARY_TIME, startGesture);
290 ASSERT_THAT(args,
291 ElementsAre(VariantWith<NotifyMotionArgs>(
292 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
293 WithCoords(0, 0),
294 WithGestureScrollDistance(0, 0, EPSILON),
295 WithDownTime(downTime))),
296 VariantWith<NotifyMotionArgs>(
297 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
298 WithCoords(0, -10),
299 WithGestureScrollDistance(0, 10, EPSILON)))));
300 ASSERT_THAT(args,
301 Each(VariantWith<NotifyMotionArgs>(
302 AllOf(WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
303 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE),
304 WithToolType(ToolType::FINGER),
305 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
306
307 Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
308 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, continueGesture);
309 ASSERT_THAT(args,
310 ElementsAre(VariantWith<NotifyMotionArgs>(
311 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithCoords(0, -15),
312 WithGestureScrollDistance(0, 5, EPSILON),
313 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
314 WithToolType(ToolType::FINGER),
315 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE),
316 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
317
318 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
319 GESTURES_FLING_START);
320 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
321 ASSERT_THAT(args,
322 ElementsAre(VariantWith<NotifyMotionArgs>(
323 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
324 WithCoords(0, -15),
325 WithGestureScrollDistance(0, 0, EPSILON),
326 WithMotionClassification(
327 MotionClassification::TWO_FINGER_SWIPE),
328 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE))),
329 VariantWith<NotifyMotionArgs>(
330 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
331 WithCoords(0, 0),
332 WithMotionClassification(MotionClassification::NONE)))));
333 ASSERT_THAT(args,
334 Each(VariantWith<NotifyMotionArgs>(
335 AllOf(WithToolType(ToolType::FINGER),
336 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
337 }
338
TEST_F(GestureConverterTest,Scroll_Rotated)339 TEST_F(GestureConverterTest, Scroll_Rotated) {
340 const nsecs_t downTime = 12345;
341 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
342 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
343 converter.setOrientation(ui::ROTATION_90);
344 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
345
346 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
347 std::list<NotifyArgs> args =
348 converter.handleGesture(downTime, READ_TIME, ARBITRARY_TIME, startGesture);
349 ASSERT_THAT(args,
350 ElementsAre(VariantWith<NotifyMotionArgs>(
351 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
352 WithCoords(0, 0),
353 WithGestureScrollDistance(0, 0, EPSILON),
354 WithDownTime(downTime))),
355 VariantWith<NotifyMotionArgs>(
356 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
357 WithCoords(-10, 0),
358 WithGestureScrollDistance(0, 10, EPSILON)))));
359 ASSERT_THAT(args,
360 Each(VariantWith<NotifyMotionArgs>(
361 AllOf(WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
362 WithToolType(ToolType::FINGER),
363 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
364
365 Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
366 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, continueGesture);
367 ASSERT_THAT(args,
368 ElementsAre(VariantWith<NotifyMotionArgs>(
369 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithCoords(-15, 0),
370 WithGestureScrollDistance(0, 5, EPSILON),
371 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE),
372 WithToolType(ToolType::FINGER),
373 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
374
375 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
376 GESTURES_FLING_START);
377 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
378 ASSERT_THAT(args,
379 ElementsAre(VariantWith<NotifyMotionArgs>(
380 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
381 WithCoords(-15, 0),
382 WithGestureScrollDistance(0, 0, EPSILON),
383 WithMotionClassification(
384 MotionClassification::TWO_FINGER_SWIPE))),
385 VariantWith<NotifyMotionArgs>(
386 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
387 WithCoords(0, 0),
388 WithMotionClassification(MotionClassification::NONE)))));
389 ASSERT_THAT(args,
390 Each(VariantWith<NotifyMotionArgs>(
391 AllOf(WithToolType(ToolType::FINGER),
392 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
393 }
394
TEST_F(GestureConverterTest,Scroll_ClearsClassificationAfterGesture)395 TEST_F(GestureConverterTest, Scroll_ClearsClassificationAfterGesture) {
396 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
397 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
398 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
399
400 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
401 std::list<NotifyArgs> args =
402 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
403
404 Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
405 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, continueGesture);
406
407 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
408 GESTURES_FLING_START);
409 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
410
411 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
412 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
413 ASSERT_THAT(args,
414 ElementsAre(VariantWith<NotifyMotionArgs>(
415 AllOf(WithMotionClassification(MotionClassification::NONE),
416 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
417 }
418
TEST_F(GestureConverterTest,Scroll_ClearsScrollDistanceAfterGesture)419 TEST_F(GestureConverterTest, Scroll_ClearsScrollDistanceAfterGesture) {
420 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
421 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
422 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
423
424 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
425 std::list<NotifyArgs> args =
426 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
427
428 Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -5);
429 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, continueGesture);
430
431 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
432 GESTURES_FLING_START);
433 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
434
435 // Move gestures don't use the fake finger array, so to test that gesture axes are cleared we
436 // need to use another gesture type, like pinch.
437 Gesture pinchGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
438 GESTURES_ZOOM_START);
439 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, pinchGesture);
440 ASSERT_FALSE(args.empty());
441 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()), WithGestureScrollDistance(0, 0, EPSILON));
442 }
443
TEST_F(GestureConverterTest,Scroll_ClearsFakeFingerPositionOnSubsequentScrollGestures)444 TEST_F(GestureConverterTest, Scroll_ClearsFakeFingerPositionOnSubsequentScrollGestures) {
445 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
446 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
447 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
448
449 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 15, -10);
450 std::list<NotifyArgs> args =
451 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
452
453 Gesture continueGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -2, -5);
454 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, continueGesture);
455
456 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
457 GESTURES_FLING_START);
458 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
459 Gesture flingGestureEnd(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, 0,
460 GESTURES_FLING_TAP_DOWN);
461 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGestureEnd);
462
463 // Start a second scoll gesture, and ensure the fake finger is reset to (0, 0), instead of
464 // continuing from the position where the last scroll gesture's fake finger ended.
465 Gesture secondScrollStart(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 2,
466 14);
467 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, secondScrollStart);
468 ASSERT_THAT(args,
469 ElementsAre(VariantWith<NotifyMotionArgs>(
470 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)),
471 VariantWith<NotifyMotionArgs>(
472 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
473 WithCoords(0, 0),
474 WithGestureScrollDistance(0, 0, EPSILON))),
475 VariantWith<NotifyMotionArgs>(
476 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
477 WithCoords(2, 14),
478 WithGestureScrollDistance(-2, -14, EPSILON)))));
479 }
480
TEST_F(GestureConverterTest,ThreeFingerSwipe_ClearsClassificationAfterGesture)481 TEST_F(GestureConverterTest, ThreeFingerSwipe_ClearsClassificationAfterGesture) {
482 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
483 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
484 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
485
486 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/0,
487 /*dy=*/0);
488 std::list<NotifyArgs> args =
489 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
490
491 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
492 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, liftGesture);
493
494 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/-5,
495 /*dy=*/10);
496 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
497 ASSERT_THAT(args,
498 ElementsAre(VariantWith<NotifyMotionArgs>(
499 WithMotionClassification(MotionClassification::NONE))));
500 }
501
TEST_F(GestureConverterTest,ThreeFingerSwipe_ClearsGestureAxesAfterGesture)502 TEST_F(GestureConverterTest, ThreeFingerSwipe_ClearsGestureAxesAfterGesture) {
503 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
504 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
505 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
506
507 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/5,
508 /*dy=*/5);
509 std::list<NotifyArgs> args =
510 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
511
512 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
513 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, liftGesture);
514
515 // Move gestures don't use the fake finger array, so to test that gesture axes are cleared we
516 // need to use another gesture type, like pinch.
517 Gesture pinchGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
518 GESTURES_ZOOM_START);
519 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, pinchGesture);
520 ASSERT_FALSE(args.empty());
521 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()),
522 AllOf(WithGestureOffset(0, 0, EPSILON), WithGestureSwipeFingerCount(0)));
523 }
524
TEST_F(GestureConverterTest,ThreeFingerSwipe_Vertical)525 TEST_F(GestureConverterTest, ThreeFingerSwipe_Vertical) {
526 // The gestures library will "lock" a swipe into the dimension it starts in. For example, if you
527 // start swiping up and then start moving left or right, it'll return gesture events with only Y
528 // deltas until you lift your fingers and start swiping again. That's why each of these tests
529 // only checks movement in one dimension.
530 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
531 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
532 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
533
534 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ 0,
535 /* dy= */ 10);
536 std::list<NotifyArgs> args =
537 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
538 ASSERT_EQ(4u, args.size());
539 ASSERT_THAT(args,
540 Each(VariantWith<NotifyMotionArgs>(
541 AllOf(WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
542 WithGestureSwipeFingerCount(3), WithToolType(ToolType::FINGER),
543 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
544
545 // Three fake fingers should be created. We don't actually care where they are, so long as they
546 // move appropriately.
547 NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
548 ASSERT_THAT(arg,
549 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
550 WithPointerCount(1u)));
551 PointerCoords finger0Start = arg.pointerCoords[0];
552 args.pop_front();
553 arg = std::get<NotifyMotionArgs>(args.front());
554 ASSERT_THAT(arg,
555 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
556 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
557 WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u)));
558 PointerCoords finger1Start = arg.pointerCoords[1];
559 args.pop_front();
560 arg = std::get<NotifyMotionArgs>(args.front());
561 ASSERT_THAT(arg,
562 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
563 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
564 WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u)));
565 PointerCoords finger2Start = arg.pointerCoords[2];
566 args.pop_front();
567
568 arg = std::get<NotifyMotionArgs>(args.front());
569 ASSERT_THAT(arg,
570 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
571 WithGestureOffset(0, -0.01, EPSILON), WithPointerCount(3u)));
572 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX());
573 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX());
574 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX());
575 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY() - 10);
576 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY() - 10);
577 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY() - 10);
578
579 Gesture continueGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
580 /* dx= */ 0, /* dy= */ 5);
581 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, continueGesture);
582 ASSERT_EQ(1u, args.size());
583 arg = std::get<NotifyMotionArgs>(args.front());
584 ASSERT_THAT(arg,
585 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
586 WithGestureOffset(0, -0.005, EPSILON), WithGestureSwipeFingerCount(3),
587 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
588 WithPointerCount(3u), WithToolType(ToolType::FINGER),
589 WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
590 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX());
591 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX());
592 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX());
593 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY() - 15);
594 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY() - 15);
595 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY() - 15);
596
597 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
598 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, liftGesture);
599 ASSERT_THAT(args,
600 ElementsAre(VariantWith<NotifyMotionArgs>(
601 AllOf(WithMotionAction(
602 AMOTION_EVENT_ACTION_POINTER_UP |
603 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
604 WithGestureOffset(0, 0, EPSILON),
605 WithGestureSwipeFingerCount(3),
606 WithMotionClassification(
607 MotionClassification::MULTI_FINGER_SWIPE),
608 WithPointerCount(3u))),
609 VariantWith<NotifyMotionArgs>(
610 AllOf(WithMotionAction(
611 AMOTION_EVENT_ACTION_POINTER_UP |
612 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
613 WithGestureOffset(0, 0, EPSILON),
614 WithGestureSwipeFingerCount(3),
615 WithMotionClassification(
616 MotionClassification::MULTI_FINGER_SWIPE),
617 WithPointerCount(2u))),
618 VariantWith<NotifyMotionArgs>(
619 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
620 WithGestureOffset(0, 0, EPSILON),
621 WithGestureSwipeFingerCount(3),
622 WithMotionClassification(
623 MotionClassification::MULTI_FINGER_SWIPE),
624 WithPointerCount(1u))),
625 VariantWith<NotifyMotionArgs>(
626 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
627 WithCoords(0, 0),
628 WithMotionClassification(MotionClassification::NONE)))));
629 ASSERT_THAT(args,
630 Each(VariantWith<NotifyMotionArgs>(
631 AllOf(WithToolType(ToolType::FINGER),
632 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
633 }
634
TEST_F(GestureConverterTest,ThreeFingerSwipe_Rotated)635 TEST_F(GestureConverterTest, ThreeFingerSwipe_Rotated) {
636 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
637 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
638 converter.setOrientation(ui::ROTATION_90);
639 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
640
641 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dx= */ 0,
642 /* dy= */ 10);
643 std::list<NotifyArgs> args =
644 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
645 ASSERT_EQ(4u, args.size());
646 ASSERT_THAT(args,
647 Each(VariantWith<NotifyMotionArgs>(WithDisplayId(ui::LogicalDisplayId::DEFAULT))));
648
649 // Three fake fingers should be created. We don't actually care where they are, so long as they
650 // move appropriately.
651 NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
652 ASSERT_THAT(arg,
653 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
654 WithPointerCount(1u)));
655 PointerCoords finger0Start = arg.pointerCoords[0];
656 args.pop_front();
657 arg = std::get<NotifyMotionArgs>(args.front());
658 ASSERT_THAT(arg,
659 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
660 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
661 WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u)));
662 PointerCoords finger1Start = arg.pointerCoords[1];
663 args.pop_front();
664 arg = std::get<NotifyMotionArgs>(args.front());
665 ASSERT_THAT(arg,
666 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
667 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
668 WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u)));
669 PointerCoords finger2Start = arg.pointerCoords[2];
670 args.pop_front();
671
672 arg = std::get<NotifyMotionArgs>(args.front());
673 ASSERT_THAT(arg,
674 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
675 WithGestureOffset(0, -0.01, EPSILON), WithPointerCount(3u)));
676 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() - 10);
677 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() - 10);
678 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() - 10);
679 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
680 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
681 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
682
683 Gesture continueGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
684 /* dx= */ 0, /* dy= */ 5);
685 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, continueGesture);
686 ASSERT_EQ(1u, args.size());
687 arg = std::get<NotifyMotionArgs>(args.front());
688 ASSERT_THAT(arg,
689 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
690 WithGestureOffset(0, -0.005, EPSILON), WithPointerCount(3u),
691 WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
692 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() - 15);
693 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() - 15);
694 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() - 15);
695 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
696 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
697 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
698
699 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
700 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, liftGesture);
701 ASSERT_THAT(args,
702 ElementsAre(VariantWith<NotifyMotionArgs>(
703 AllOf(WithMotionAction(
704 AMOTION_EVENT_ACTION_POINTER_UP |
705 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
706 WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u))),
707 VariantWith<NotifyMotionArgs>(
708 AllOf(WithMotionAction(
709 AMOTION_EVENT_ACTION_POINTER_UP |
710 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
711 WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u))),
712 VariantWith<NotifyMotionArgs>(
713 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
714 WithGestureOffset(0, 0, EPSILON), WithPointerCount(1u))),
715 VariantWith<NotifyMotionArgs>(
716 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)))));
717 ASSERT_THAT(args,
718 Each(VariantWith<NotifyMotionArgs>(WithDisplayId(ui::LogicalDisplayId::DEFAULT))));
719 }
720
TEST_F(GestureConverterTest,FourFingerSwipe_Horizontal)721 TEST_F(GestureConverterTest, FourFingerSwipe_Horizontal) {
722 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
723 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
724 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
725
726 Gesture startGesture(kGestureFourFingerSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
727 /* dx= */ 10, /* dy= */ 0);
728 std::list<NotifyArgs> args =
729 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
730 ASSERT_EQ(5u, args.size());
731 ASSERT_THAT(args,
732 Each(VariantWith<NotifyMotionArgs>(
733 AllOf(WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
734 WithGestureSwipeFingerCount(4), WithToolType(ToolType::FINGER),
735 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
736
737 // Four fake fingers should be created. We don't actually care where they are, so long as they
738 // move appropriately.
739 NotifyMotionArgs arg = std::get<NotifyMotionArgs>(args.front());
740 ASSERT_THAT(arg,
741 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithGestureOffset(0, 0, EPSILON),
742 WithPointerCount(1u)));
743 PointerCoords finger0Start = arg.pointerCoords[0];
744 args.pop_front();
745 arg = std::get<NotifyMotionArgs>(args.front());
746 ASSERT_THAT(arg,
747 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
748 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
749 WithGestureOffset(0, 0, EPSILON), WithPointerCount(2u)));
750 PointerCoords finger1Start = arg.pointerCoords[1];
751 args.pop_front();
752 arg = std::get<NotifyMotionArgs>(args.front());
753 ASSERT_THAT(arg,
754 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
755 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
756 WithGestureOffset(0, 0, EPSILON), WithPointerCount(3u)));
757 PointerCoords finger2Start = arg.pointerCoords[2];
758 args.pop_front();
759 arg = std::get<NotifyMotionArgs>(args.front());
760 ASSERT_THAT(arg,
761 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_POINTER_DOWN |
762 3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
763 WithGestureOffset(0, 0, EPSILON), WithPointerCount(4u)));
764 PointerCoords finger3Start = arg.pointerCoords[3];
765 args.pop_front();
766
767 arg = std::get<NotifyMotionArgs>(args.front());
768 ASSERT_THAT(arg,
769 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
770 WithGestureOffset(0.01, 0, EPSILON), WithPointerCount(4u)));
771 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 10);
772 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() + 10);
773 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() + 10);
774 EXPECT_EQ(arg.pointerCoords[3].getX(), finger3Start.getX() + 10);
775 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
776 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
777 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
778 EXPECT_EQ(arg.pointerCoords[3].getY(), finger3Start.getY());
779
780 Gesture continueGesture(kGestureFourFingerSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
781 /* dx= */ 5, /* dy= */ 0);
782 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, continueGesture);
783 ASSERT_EQ(1u, args.size());
784 arg = std::get<NotifyMotionArgs>(args.front());
785 ASSERT_THAT(arg,
786 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
787 WithGestureOffset(0.005, 0, EPSILON), WithGestureSwipeFingerCount(4),
788 WithMotionClassification(MotionClassification::MULTI_FINGER_SWIPE),
789 WithPointerCount(4u), WithToolType(ToolType::FINGER),
790 WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
791 EXPECT_EQ(arg.pointerCoords[0].getX(), finger0Start.getX() + 15);
792 EXPECT_EQ(arg.pointerCoords[1].getX(), finger1Start.getX() + 15);
793 EXPECT_EQ(arg.pointerCoords[2].getX(), finger2Start.getX() + 15);
794 EXPECT_EQ(arg.pointerCoords[3].getX(), finger3Start.getX() + 15);
795 EXPECT_EQ(arg.pointerCoords[0].getY(), finger0Start.getY());
796 EXPECT_EQ(arg.pointerCoords[1].getY(), finger1Start.getY());
797 EXPECT_EQ(arg.pointerCoords[2].getY(), finger2Start.getY());
798 EXPECT_EQ(arg.pointerCoords[3].getY(), finger3Start.getY());
799
800 Gesture liftGesture(kGestureSwipeLift, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME);
801 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, liftGesture);
802 ASSERT_THAT(args,
803 ElementsAre(VariantWith<NotifyMotionArgs>(
804 AllOf(WithMotionAction(
805 AMOTION_EVENT_ACTION_POINTER_UP |
806 3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
807 WithGestureOffset(0, 0, EPSILON),
808 WithGestureSwipeFingerCount(4),
809 WithMotionClassification(
810 MotionClassification::MULTI_FINGER_SWIPE),
811 WithPointerCount(4u))),
812 VariantWith<NotifyMotionArgs>(
813 AllOf(WithMotionAction(
814 AMOTION_EVENT_ACTION_POINTER_UP |
815 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
816 WithGestureOffset(0, 0, EPSILON),
817 WithGestureSwipeFingerCount(4),
818 WithMotionClassification(
819 MotionClassification::MULTI_FINGER_SWIPE),
820 WithPointerCount(3u))),
821 VariantWith<NotifyMotionArgs>(
822 AllOf(WithMotionAction(
823 AMOTION_EVENT_ACTION_POINTER_UP |
824 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
825 WithGestureOffset(0, 0, EPSILON),
826 WithGestureSwipeFingerCount(4),
827 WithMotionClassification(
828 MotionClassification::MULTI_FINGER_SWIPE),
829 WithPointerCount(2u))),
830 VariantWith<NotifyMotionArgs>(
831 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
832 WithGestureOffset(0, 0, EPSILON),
833 WithGestureSwipeFingerCount(4),
834 WithMotionClassification(
835 MotionClassification::MULTI_FINGER_SWIPE),
836 WithPointerCount(1u))),
837 VariantWith<NotifyMotionArgs>(
838 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
839 WithCoords(0, 0),
840 WithMotionClassification(MotionClassification::NONE)))));
841 ASSERT_THAT(args,
842 Each(VariantWith<NotifyMotionArgs>(
843 AllOf(WithToolType(ToolType::FINGER),
844 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
845 }
846
TEST_F(GestureConverterTest,Pinch_Inwards)847 TEST_F(GestureConverterTest, Pinch_Inwards) {
848 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
849 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
850 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
851
852 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
853 GESTURES_ZOOM_START);
854 std::list<NotifyArgs> args =
855 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
856 ASSERT_THAT(args,
857 ElementsAre(VariantWith<NotifyMotionArgs>(
858 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
859 WithCoords(-100, 0), WithPointerCount(1u))),
860 VariantWith<NotifyMotionArgs>(
861 AllOf(WithMotionAction(
862 AMOTION_EVENT_ACTION_POINTER_DOWN |
863 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
864 WithPointerCoords(1, 100, 0), WithPointerCount(2u)))));
865 ASSERT_THAT(args,
866 Each(VariantWith<NotifyMotionArgs>(
867 AllOf(WithMotionClassification(MotionClassification::PINCH),
868 WithGesturePinchScaleFactor(1.0f, EPSILON),
869 WithToolType(ToolType::FINGER),
870 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
871
872 Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
873 /* dz= */ 0.8, GESTURES_ZOOM_UPDATE);
874 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, updateGesture);
875 ASSERT_THAT(args,
876 ElementsAre(VariantWith<NotifyMotionArgs>(
877 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
878 WithMotionClassification(MotionClassification::PINCH),
879 WithGesturePinchScaleFactor(0.8f, EPSILON),
880 WithPointerCoords(0, -80, 0), WithPointerCoords(1, 80, 0),
881 WithPointerCount(2u), WithToolType(ToolType::FINGER),
882 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
883
884 Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
885 GESTURES_ZOOM_END);
886 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, endGesture);
887 ASSERT_THAT(args,
888 ElementsAre(VariantWith<NotifyMotionArgs>(
889 AllOf(WithMotionAction(
890 AMOTION_EVENT_ACTION_POINTER_UP |
891 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
892 WithMotionClassification(MotionClassification::PINCH),
893 WithGesturePinchScaleFactor(1.0f, EPSILON),
894 WithPointerCount(2u))),
895 VariantWith<NotifyMotionArgs>(
896 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
897 WithMotionClassification(MotionClassification::PINCH),
898 WithGesturePinchScaleFactor(1.0f, EPSILON),
899 WithPointerCount(1u))),
900 VariantWith<NotifyMotionArgs>(
901 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
902 WithCoords(0, 0),
903 WithMotionClassification(MotionClassification::NONE)))));
904 ASSERT_THAT(args,
905 Each(VariantWith<NotifyMotionArgs>(
906 AllOf(WithToolType(ToolType::FINGER),
907 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
908 }
909
TEST_F(GestureConverterTest,Pinch_Outwards)910 TEST_F(GestureConverterTest, Pinch_Outwards) {
911 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
912 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
913 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
914
915 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
916 GESTURES_ZOOM_START);
917 std::list<NotifyArgs> args =
918 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
919 ASSERT_THAT(args,
920 ElementsAre(VariantWith<NotifyMotionArgs>(
921 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
922 WithCoords(-100, 0), WithPointerCount(1u))),
923 VariantWith<NotifyMotionArgs>(
924 AllOf(WithMotionAction(
925 AMOTION_EVENT_ACTION_POINTER_DOWN |
926 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
927 WithPointerCoords(1, 100, 0), WithPointerCount(2u)))));
928 ASSERT_THAT(args,
929 Each(VariantWith<NotifyMotionArgs>(
930 AllOf(WithMotionClassification(MotionClassification::PINCH),
931 WithGesturePinchScaleFactor(1.0f, EPSILON),
932 WithToolType(ToolType::FINGER),
933 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
934
935 Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
936 /* dz= */ 1.1, GESTURES_ZOOM_UPDATE);
937 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, updateGesture);
938 ASSERT_THAT(args,
939 ElementsAre(VariantWith<NotifyMotionArgs>(
940 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
941 WithMotionClassification(MotionClassification::PINCH),
942 WithGesturePinchScaleFactor(1.1f, EPSILON),
943 WithPointerCoords(0, -110, 0), WithPointerCoords(1, 110, 0),
944 WithPointerCount(2u), WithToolType(ToolType::FINGER),
945 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
946
947 Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* dz= */ 1,
948 GESTURES_ZOOM_END);
949 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, endGesture);
950 ASSERT_THAT(args,
951 ElementsAre(VariantWith<NotifyMotionArgs>(
952 AllOf(WithMotionAction(
953 AMOTION_EVENT_ACTION_POINTER_UP |
954 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
955 WithMotionClassification(MotionClassification::PINCH),
956 WithGesturePinchScaleFactor(1.0f, EPSILON),
957 WithPointerCount(2u))),
958 VariantWith<NotifyMotionArgs>(
959 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
960 WithMotionClassification(MotionClassification::PINCH),
961 WithGesturePinchScaleFactor(1.0f, EPSILON),
962 WithPointerCount(1u))),
963 VariantWith<NotifyMotionArgs>(
964 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
965 WithCoords(0, 0),
966 WithMotionClassification(MotionClassification::NONE)))));
967 ASSERT_THAT(args,
968 Each(VariantWith<NotifyMotionArgs>(
969 AllOf(WithToolType(ToolType::FINGER),
970 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
971 }
972
TEST_F(GestureConverterTest,Pinch_ClearsClassificationAfterGesture)973 TEST_F(GestureConverterTest, Pinch_ClearsClassificationAfterGesture) {
974 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
975 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
976 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
977
978 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
979 GESTURES_ZOOM_START);
980 std::list<NotifyArgs> args =
981 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
982
983 Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
984 /*dz=*/1.2, GESTURES_ZOOM_UPDATE);
985 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, updateGesture);
986
987 Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
988 GESTURES_ZOOM_END);
989 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, endGesture);
990
991 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
992 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
993 ASSERT_THAT(args,
994 ElementsAre(VariantWith<NotifyMotionArgs>(
995 WithMotionClassification(MotionClassification::NONE))));
996 }
997
TEST_F(GestureConverterTest,Pinch_ClearsScaleFactorAfterGesture)998 TEST_F(GestureConverterTest, Pinch_ClearsScaleFactorAfterGesture) {
999 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1000 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1001 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
1002
1003 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
1004 GESTURES_ZOOM_START);
1005 std::list<NotifyArgs> args =
1006 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
1007
1008 Gesture updateGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1009 /*dz=*/1.2, GESTURES_ZOOM_UPDATE);
1010 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, updateGesture);
1011
1012 Gesture endGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
1013 GESTURES_ZOOM_END);
1014 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, endGesture);
1015
1016 // Move gestures don't use the fake finger array, so to test that gesture axes are cleared we
1017 // need to use another gesture type, like scroll.
1018 Gesture scrollGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/1,
1019 /*dy=*/0);
1020 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, scrollGesture);
1021 ASSERT_FALSE(args.empty());
1022 EXPECT_THAT(std::get<NotifyMotionArgs>(args.front()), WithGesturePinchScaleFactor(0, EPSILON));
1023 }
1024
TEST_F(GestureConverterTest,ResetWithButtonPressed)1025 TEST_F(GestureConverterTest, ResetWithButtonPressed) {
1026 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1027 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1028 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
1029
1030 Gesture downGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1031 /*down=*/GESTURES_BUTTON_LEFT | GESTURES_BUTTON_RIGHT,
1032 /*up=*/GESTURES_BUTTON_NONE, /*is_tap=*/false);
1033 (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, downGesture);
1034
1035 std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
1036 ASSERT_THAT(args,
1037 ElementsAre(VariantWith<NotifyMotionArgs>(
1038 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1039 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1040 WithButtonState(AMOTION_EVENT_BUTTON_SECONDARY))),
1041 VariantWith<NotifyMotionArgs>(
1042 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1043 WithActionButton(AMOTION_EVENT_BUTTON_SECONDARY),
1044 WithButtonState(0))),
1045 VariantWith<NotifyMotionArgs>(
1046 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
1047 WithButtonState(0))),
1048 VariantWith<NotifyMotionArgs>(
1049 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1050 WithButtonState(0)))));
1051 ASSERT_THAT(args,
1052 Each(VariantWith<NotifyMotionArgs>(
1053 AllOf(WithCoords(0, 0), WithToolType(ToolType::FINGER),
1054 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
1055 }
1056
TEST_F(GestureConverterTest,ResetDuringScroll)1057 TEST_F(GestureConverterTest, ResetDuringScroll) {
1058 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1059 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1060 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
1061
1062 Gesture startGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
1063 (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
1064
1065 std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
1066 ASSERT_THAT(args,
1067 ElementsAre(VariantWith<NotifyMotionArgs>(
1068 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
1069 WithCoords(0, -10),
1070 WithGestureScrollDistance(0, 0, EPSILON),
1071 WithMotionClassification(
1072 MotionClassification::TWO_FINGER_SWIPE),
1073 WithFlags(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE))),
1074 VariantWith<NotifyMotionArgs>(
1075 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1076 WithCoords(0, 0),
1077 WithMotionClassification(MotionClassification::NONE)))));
1078 ASSERT_THAT(args,
1079 Each(VariantWith<NotifyMotionArgs>(
1080 AllOf(WithToolType(ToolType::FINGER),
1081 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
1082 }
1083
TEST_F(GestureConverterTest,ResetDuringThreeFingerSwipe)1084 TEST_F(GestureConverterTest, ResetDuringThreeFingerSwipe) {
1085 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1086 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1087 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
1088
1089 Gesture startGesture(kGestureSwipe, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dx=*/0,
1090 /*dy=*/10);
1091 (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
1092
1093 std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
1094 ASSERT_THAT(args,
1095 ElementsAre(VariantWith<NotifyMotionArgs>(
1096 AllOf(WithMotionAction(
1097 AMOTION_EVENT_ACTION_POINTER_UP |
1098 2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1099 WithGestureOffset(0, 0, EPSILON),
1100 WithMotionClassification(
1101 MotionClassification::MULTI_FINGER_SWIPE),
1102 WithPointerCount(3u))),
1103 VariantWith<NotifyMotionArgs>(
1104 AllOf(WithMotionAction(
1105 AMOTION_EVENT_ACTION_POINTER_UP |
1106 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1107 WithGestureOffset(0, 0, EPSILON),
1108 WithMotionClassification(
1109 MotionClassification::MULTI_FINGER_SWIPE),
1110 WithPointerCount(2u))),
1111 VariantWith<NotifyMotionArgs>(
1112 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
1113 WithGestureOffset(0, 0, EPSILON),
1114 WithMotionClassification(
1115 MotionClassification::MULTI_FINGER_SWIPE),
1116 WithPointerCount(1u))),
1117 VariantWith<NotifyMotionArgs>(
1118 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1119 WithMotionClassification(MotionClassification::NONE)))));
1120 ASSERT_THAT(args,
1121 Each(VariantWith<NotifyMotionArgs>(
1122 AllOf(WithToolType(ToolType::FINGER),
1123 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
1124 }
1125
TEST_F(GestureConverterTest,ResetDuringPinch)1126 TEST_F(GestureConverterTest, ResetDuringPinch) {
1127 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1128 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1129 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
1130
1131 Gesture startGesture(kGesturePinch, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /*dz=*/1,
1132 GESTURES_ZOOM_START);
1133 (void)converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, startGesture);
1134
1135 std::list<NotifyArgs> args = converter.reset(ARBITRARY_TIME);
1136 ASSERT_THAT(args,
1137 ElementsAre(VariantWith<NotifyMotionArgs>(
1138 AllOf(WithMotionAction(
1139 AMOTION_EVENT_ACTION_POINTER_UP |
1140 1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1141 WithMotionClassification(MotionClassification::PINCH),
1142 WithGesturePinchScaleFactor(1.0f, EPSILON),
1143 WithPointerCount(2u))),
1144 VariantWith<NotifyMotionArgs>(
1145 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
1146 WithMotionClassification(MotionClassification::PINCH),
1147 WithGesturePinchScaleFactor(1.0f, EPSILON),
1148 WithPointerCount(1u))),
1149 VariantWith<NotifyMotionArgs>(
1150 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1151 WithCoords(0, 0),
1152 WithMotionClassification(MotionClassification::NONE)))));
1153 ASSERT_THAT(args,
1154 Each(VariantWith<NotifyMotionArgs>(
1155 AllOf(WithToolType(ToolType::FINGER),
1156 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
1157 }
1158
TEST_F(GestureConverterTest,FlingTapDown)1159 TEST_F(GestureConverterTest, FlingTapDown) {
1160 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1161 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1162 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
1163
1164 Gesture tapDownGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1165 /*vx=*/0.f, /*vy=*/0.f, GESTURES_FLING_TAP_DOWN);
1166 std::list<NotifyArgs> args =
1167 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, tapDownGesture);
1168
1169 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1170 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), WithCoords(0, 0),
1171 WithRelativeMotion(0.f, 0.f), WithToolType(ToolType::FINGER),
1172 WithButtonState(0), WithPressure(0.0f),
1173 WithDisplayId(ui::LogicalDisplayId::DEFAULT)));
1174 }
1175
TEST_F(GestureConverterTest,FlingTapDownAfterScrollStopsFling)1176 TEST_F(GestureConverterTest, FlingTapDownAfterScrollStopsFling) {
1177 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1178 input_flags::enable_touchpad_fling_stop(true);
1179 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1180 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
1181
1182 Gesture scrollGesture(kGestureScroll, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 0, -10);
1183 std::list<NotifyArgs> args =
1184 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, scrollGesture);
1185 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, 1, 1,
1186 GESTURES_FLING_START);
1187 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
1188
1189 Gesture tapDownGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1190 /*vx=*/0.f, /*vy=*/0.f, GESTURES_FLING_TAP_DOWN);
1191 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, tapDownGesture);
1192 ASSERT_THAT(args,
1193 ElementsAre(VariantWith<NotifyMotionArgs>(
1194 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)),
1195 VariantWith<NotifyMotionArgs>(
1196 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)),
1197 VariantWith<NotifyMotionArgs>(
1198 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)),
1199 VariantWith<NotifyMotionArgs>(
1200 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER))));
1201 ASSERT_THAT(args,
1202 Each(VariantWith<NotifyMotionArgs>(
1203 AllOf(WithCoords(0, 0), WithToolType(ToolType::FINGER),
1204 WithDisplayId(ui::LogicalDisplayId::DEFAULT),
1205 WithMotionClassification(MotionClassification::TWO_FINGER_SWIPE)))));
1206 }
1207
TEST_F(GestureConverterTest,Tap)1208 TEST_F(GestureConverterTest, Tap) {
1209 // Tap should produce button press/release events
1210 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1211 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1212 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
1213
1214 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* vx= */ 0,
1215 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
1216 std::list<NotifyArgs> args =
1217 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
1218 // We don't need to check args here, since it's covered by the FlingTapDown test.
1219
1220 Gesture tapGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1221 /* down= */ GESTURES_BUTTON_LEFT,
1222 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ true);
1223 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, tapGesture);
1224
1225 ASSERT_THAT(args,
1226 ElementsAre(VariantWith<NotifyMotionArgs>(
1227 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1228 WithButtonState(0), WithPressure(0.0f))),
1229 VariantWith<NotifyMotionArgs>(
1230 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1231 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
1232 WithPressure(1.0f))),
1233 VariantWith<NotifyMotionArgs>(
1234 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1235 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1236 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
1237 WithPressure(1.0f))),
1238 VariantWith<NotifyMotionArgs>(
1239 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1240 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1241 WithButtonState(0), WithPressure(1.0f))),
1242 VariantWith<NotifyMotionArgs>(
1243 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
1244 WithButtonState(0), WithPressure(0.0f))),
1245 VariantWith<NotifyMotionArgs>(
1246 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1247 WithButtonState(0), WithPressure(0.0f)))));
1248 ASSERT_THAT(args,
1249 Each(VariantWith<NotifyMotionArgs>(
1250 AllOf(WithCoords(0, 0), WithRelativeMotion(0.f, 0.f),
1251 WithToolType(ToolType::FINGER),
1252 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
1253 }
1254
TEST_F(GestureConverterTest,Click)1255 TEST_F(GestureConverterTest, Click) {
1256 // Click should produce button press/release events
1257 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1258 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1259 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
1260
1261 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* vx= */ 0,
1262 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
1263 std::list<NotifyArgs> args =
1264 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
1265 // We don't need to check args here, since it's covered by the FlingTapDown test.
1266
1267 Gesture buttonDownGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1268 /* down= */ GESTURES_BUTTON_LEFT,
1269 /* up= */ GESTURES_BUTTON_NONE, /* is_tap= */ false);
1270 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, buttonDownGesture);
1271
1272 ASSERT_THAT(args,
1273 ElementsAre(VariantWith<NotifyMotionArgs>(
1274 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1275 WithButtonState(0), WithPressure(0.0f))),
1276 VariantWith<NotifyMotionArgs>(
1277 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1278 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
1279 WithPressure(1.0f))),
1280 VariantWith<NotifyMotionArgs>(
1281 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1282 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1283 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
1284 WithPressure(1.0f)))));
1285 ASSERT_THAT(args,
1286 Each(VariantWith<NotifyMotionArgs>(
1287 AllOf(WithCoords(0, 0), WithRelativeMotion(0.f, 0.f),
1288 WithToolType(ToolType::FINGER),
1289 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
1290
1291 Gesture buttonUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1292 /* down= */ GESTURES_BUTTON_NONE,
1293 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ false);
1294 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, buttonUpGesture);
1295
1296 ASSERT_THAT(args,
1297 ElementsAre(VariantWith<NotifyMotionArgs>(
1298 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1299 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1300 WithPressure(1.0f))),
1301 VariantWith<NotifyMotionArgs>(
1302 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
1303 WithPressure(0.0f))),
1304 VariantWith<NotifyMotionArgs>(
1305 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1306 WithPressure(0.0f)))));
1307 ASSERT_THAT(args,
1308 Each(VariantWith<NotifyMotionArgs>(
1309 AllOf(WithButtonState(0), WithCoords(0, 0), WithRelativeMotion(0.f, 0.f),
1310 WithToolType(ToolType::FINGER),
1311 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
1312 }
1313
TEST_F_WITH_FLAGS(GestureConverterTest,TapWithTapToClickDisabled,REQUIRES_FLAGS_ENABLED (TOUCHPAD_PALM_REJECTION),REQUIRES_FLAGS_DISABLED (TOUCHPAD_PALM_REJECTION_V2))1314 TEST_F_WITH_FLAGS(GestureConverterTest, TapWithTapToClickDisabled,
1315 REQUIRES_FLAGS_ENABLED(TOUCHPAD_PALM_REJECTION),
1316 REQUIRES_FLAGS_DISABLED(TOUCHPAD_PALM_REJECTION_V2)) {
1317 nsecs_t currentTime = ARBITRARY_GESTURE_TIME;
1318
1319 // Tap should be ignored when disabled
1320 mReader->getContext()->setPreventingTouchpadTaps(true);
1321
1322 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1323 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1324 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
1325
1326 Gesture flingGesture(kGestureFling, currentTime, currentTime, /* vx= */ 0,
1327 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
1328 std::list<NotifyArgs> args =
1329 converter.handleGesture(currentTime, currentTime, currentTime, flingGesture);
1330 // We don't need to check args here, since it's covered by the FlingTapDown test.
1331
1332 Gesture tapGesture(kGestureButtonsChange, currentTime, currentTime,
1333 /* down= */ GESTURES_BUTTON_LEFT,
1334 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ true);
1335 args = converter.handleGesture(currentTime, currentTime, currentTime, tapGesture);
1336
1337 // no events should be generated
1338 ASSERT_EQ(0u, args.size());
1339
1340 // Future taps should be re-enabled
1341 ASSERT_FALSE(mReader->getContext()->isPreventingTouchpadTaps());
1342 }
1343
TEST_F_WITH_FLAGS(GestureConverterTest,TapWithTapToClickDisabledWithDelay,REQUIRES_FLAGS_ENABLED (TOUCHPAD_PALM_REJECTION_V2))1344 TEST_F_WITH_FLAGS(GestureConverterTest, TapWithTapToClickDisabledWithDelay,
1345 REQUIRES_FLAGS_ENABLED(TOUCHPAD_PALM_REJECTION_V2)) {
1346 nsecs_t currentTime = ARBITRARY_GESTURE_TIME;
1347
1348 // Tap should be ignored when disabled
1349 mReader->getContext()->setPreventingTouchpadTaps(true);
1350
1351 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1352 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1353 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
1354
1355 Gesture flingGesture(kGestureFling, currentTime, currentTime, /* vx= */ 0,
1356 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
1357 std::list<NotifyArgs> args =
1358 converter.handleGesture(currentTime, currentTime, currentTime, flingGesture);
1359 // We don't need to check args here, since it's covered by the FlingTapDown test.
1360
1361 Gesture tapGesture(kGestureButtonsChange, currentTime, currentTime,
1362 /* down= */ GESTURES_BUTTON_LEFT,
1363 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ true);
1364 args = converter.handleGesture(currentTime, currentTime, currentTime, tapGesture);
1365
1366 // no events should be generated
1367 ASSERT_EQ(0u, args.size());
1368
1369 // Future taps should be re-enabled
1370 ASSERT_FALSE(mReader->getContext()->isPreventingTouchpadTaps());
1371
1372 // taps before the threshold should still be ignored
1373 currentTime += TAP_ENABLE_DELAY_NANOS.count();
1374 flingGesture = Gesture(kGestureFling, currentTime, currentTime, /* vx= */ 0,
1375 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
1376 args = converter.handleGesture(currentTime, currentTime, currentTime, flingGesture);
1377
1378 ASSERT_EQ(1u, args.size());
1379 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1380 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithRelativeMotion(0, 0)));
1381
1382 tapGesture = Gesture(kGestureButtonsChange, currentTime, currentTime,
1383 /* down= */ GESTURES_BUTTON_LEFT,
1384 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ true);
1385 args = converter.handleGesture(currentTime, currentTime, currentTime, tapGesture);
1386
1387 // no events should be generated
1388 ASSERT_EQ(0u, args.size());
1389
1390 // taps after the threshold should be recognised
1391 currentTime += 1;
1392 flingGesture = Gesture(kGestureFling, currentTime, currentTime, /* vx= */ 0,
1393 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
1394 args = converter.handleGesture(currentTime, currentTime, currentTime, flingGesture);
1395
1396 ASSERT_EQ(1u, args.size());
1397 ASSERT_THAT(std::get<NotifyMotionArgs>(args.front()),
1398 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithRelativeMotion(0, 0)));
1399
1400 tapGesture = Gesture(kGestureButtonsChange, currentTime, currentTime,
1401 /* down= */ GESTURES_BUTTON_LEFT,
1402 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ true);
1403 args = converter.handleGesture(currentTime, currentTime, currentTime, tapGesture);
1404 ASSERT_THAT(args,
1405 ElementsAre(VariantWith<NotifyMotionArgs>(
1406 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1407 WithButtonState(0))),
1408 VariantWith<NotifyMotionArgs>(
1409 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1410 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))),
1411 VariantWith<NotifyMotionArgs>(
1412 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1413 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1414 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY))),
1415 VariantWith<NotifyMotionArgs>(
1416 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1417 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1418 WithButtonState(0))),
1419 VariantWith<NotifyMotionArgs>(
1420 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
1421 WithButtonState(0))),
1422 VariantWith<NotifyMotionArgs>(
1423 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1424 WithButtonState(0)))));
1425 ASSERT_THAT(args, Each(VariantWith<NotifyMotionArgs>(WithRelativeMotion(0.f, 0.f))));
1426 }
1427
TEST_F_WITH_FLAGS(GestureConverterTest,ClickWithTapToClickDisabled,REQUIRES_FLAGS_ENABLED (TOUCHPAD_PALM_REJECTION))1428 TEST_F_WITH_FLAGS(GestureConverterTest, ClickWithTapToClickDisabled,
1429 REQUIRES_FLAGS_ENABLED(TOUCHPAD_PALM_REJECTION)) {
1430 // Click should still produce button press/release events
1431 mReader->getContext()->setPreventingTouchpadTaps(true);
1432
1433 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1434 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1435 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
1436
1437 Gesture flingGesture(kGestureFling, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, /* vx= */ 0,
1438 /* vy= */ 0, GESTURES_FLING_TAP_DOWN);
1439 std::list<NotifyArgs> args =
1440 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, flingGesture);
1441 // We don't need to check args here, since it's covered by the FlingTapDown test.
1442
1443 Gesture buttonDownGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1444 /* down= */ GESTURES_BUTTON_LEFT,
1445 /* up= */ GESTURES_BUTTON_NONE, /* is_tap= */ false);
1446 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, buttonDownGesture);
1447
1448 ASSERT_THAT(args,
1449 ElementsAre(VariantWith<NotifyMotionArgs>(
1450 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1451 WithButtonState(0), WithPressure(0.0f))),
1452 VariantWith<NotifyMotionArgs>(
1453 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1454 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
1455 WithPressure(1.0f))),
1456 VariantWith<NotifyMotionArgs>(
1457 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1458 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1459 WithButtonState(AMOTION_EVENT_BUTTON_PRIMARY),
1460 WithPressure(1.0f)))));
1461 ASSERT_THAT(args,
1462 Each(VariantWith<NotifyMotionArgs>(
1463 AllOf(WithCoords(0, 0), WithRelativeMotion(0.f, 0.f),
1464 WithToolType(ToolType::FINGER),
1465 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
1466
1467 Gesture buttonUpGesture(kGestureButtonsChange, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME,
1468 /* down= */ GESTURES_BUTTON_NONE,
1469 /* up= */ GESTURES_BUTTON_LEFT, /* is_tap= */ false);
1470 args = converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, buttonUpGesture);
1471
1472 ASSERT_THAT(args,
1473 ElementsAre(VariantWith<NotifyMotionArgs>(
1474 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1475 WithActionButton(AMOTION_EVENT_BUTTON_PRIMARY),
1476 WithButtonState(0), WithCoords(0, 0),
1477 WithRelativeMotion(0.f, 0.f),
1478 WithToolType(ToolType::FINGER), WithButtonState(0),
1479 WithPressure(1.0f),
1480 WithDisplayId(ui::LogicalDisplayId::DEFAULT))),
1481 VariantWith<NotifyMotionArgs>(
1482 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
1483 WithCoords(0, 0), WithRelativeMotion(0.f, 0.f),
1484 WithToolType(ToolType::FINGER), WithButtonState(0),
1485 WithPressure(0.0f),
1486 WithDisplayId(ui::LogicalDisplayId::DEFAULT))),
1487 VariantWith<NotifyMotionArgs>(
1488 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1489 WithCoords(0, 0), WithRelativeMotion(0, 0),
1490 WithToolType(ToolType::FINGER), WithButtonState(0),
1491 WithPressure(0.0f),
1492 WithDisplayId(ui::LogicalDisplayId::DEFAULT)))));
1493
1494 // Future taps should be re-enabled
1495 ASSERT_FALSE(mReader->getContext()->isPreventingTouchpadTaps());
1496 }
1497
TEST_F_WITH_FLAGS(GestureConverterTest,MoveEnablesTapToClick,REQUIRES_FLAGS_ENABLED (TOUCHPAD_PALM_REJECTION))1498 TEST_F_WITH_FLAGS(GestureConverterTest, MoveEnablesTapToClick,
1499 REQUIRES_FLAGS_ENABLED(TOUCHPAD_PALM_REJECTION)) {
1500 // initially disable tap-to-click
1501 mReader->getContext()->setPreventingTouchpadTaps(true);
1502
1503 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1504 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1505 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
1506
1507 Gesture moveGesture(kGestureMove, ARBITRARY_GESTURE_TIME, ARBITRARY_GESTURE_TIME, -5, 10);
1508 std::list<NotifyArgs> args =
1509 converter.handleGesture(ARBITRARY_TIME, READ_TIME, ARBITRARY_TIME, moveGesture);
1510 // We don't need to check args here, since it's covered by the Move test.
1511
1512 // Future taps should be re-enabled
1513 ASSERT_FALSE(mReader->getContext()->isPreventingTouchpadTaps());
1514 }
1515
TEST_F_WITH_FLAGS(GestureConverterTest,KeypressCancelsHoverMove,REQUIRES_FLAGS_ENABLED (TOUCHPAD_PALM_REJECTION_V2))1516 TEST_F_WITH_FLAGS(GestureConverterTest, KeypressCancelsHoverMove,
1517 REQUIRES_FLAGS_ENABLED(TOUCHPAD_PALM_REJECTION_V2)) {
1518 const nsecs_t gestureStartTime = 1000;
1519 InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID);
1520 GestureConverter converter(*mReader->getContext(), deviceContext, DEVICE_ID);
1521 converter.setDisplayId(ui::LogicalDisplayId::DEFAULT);
1522
1523 // Start a move gesture at gestureStartTime
1524 Gesture moveGesture(kGestureMove, gestureStartTime, gestureStartTime, -5, 10);
1525 std::list<NotifyArgs> args =
1526 converter.handleGesture(gestureStartTime, READ_TIME, gestureStartTime, moveGesture);
1527 ASSERT_THAT(args,
1528 ElementsAre(VariantWith<NotifyMotionArgs>(
1529 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)),
1530 VariantWith<NotifyMotionArgs>(
1531 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE))));
1532
1533 // Key presses with IME connection should cancel ongoing move gesture
1534 nsecs_t currentTime = gestureStartTime + 100;
1535 mFakePolicy->setIsInputMethodConnectionActive(true);
1536 mReader->getContext()->setLastKeyDownTimestamp(currentTime);
1537 moveGesture = Gesture(kGestureMove, currentTime, currentTime, -5, 10);
1538 args = converter.handleGesture(currentTime, READ_TIME, gestureStartTime, moveGesture);
1539 ASSERT_THAT(args,
1540 ElementsAre(VariantWith<NotifyMotionArgs>(
1541 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT))));
1542
1543 // any updates in existing move gesture should be ignored
1544 moveGesture = Gesture(kGestureMove, currentTime, currentTime, -5, 10);
1545 args = converter.handleGesture(currentTime, READ_TIME, gestureStartTime, moveGesture);
1546 ASSERT_EQ(0u, args.size());
1547
1548 // New gesture should not be affected
1549 currentTime += 100;
1550 moveGesture = Gesture(kGestureMove, currentTime, currentTime, -5, 10);
1551 args = converter.handleGesture(currentTime, READ_TIME, currentTime, moveGesture);
1552 ASSERT_THAT(args,
1553 ElementsAre(VariantWith<NotifyMotionArgs>(
1554 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)),
1555 VariantWith<NotifyMotionArgs>(
1556 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE))));
1557 }
1558
1559 } // namespace android
1560