blob: 0062f426d71f76b4a8a52495e7861b82230dc2bb [file] [log] [blame]
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -07001/*
2 * Copyright (C) 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 "../UnwantedInteractionBlocker.h"
18#include <android-base/silent_death_test.h>
19#include <gtest/gtest.h>
20#include <gui/constants.h>
21#include <linux/input.h>
Siarhei Vishniakoua91d8572022-05-17 05:03:42 -070022#include <thread>
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070023
24#include "TestInputListener.h"
25
26namespace android {
27
28constexpr int32_t DEVICE_ID = 3;
29constexpr int32_t X_RESOLUTION = 11;
30constexpr int32_t Y_RESOLUTION = 11;
31constexpr int32_t MAJOR_RESOLUTION = 1;
32
33constexpr int POINTER_0_DOWN =
34 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
35constexpr int POINTER_1_DOWN =
36 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
37constexpr int POINTER_2_DOWN =
38 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
39constexpr int POINTER_0_UP =
40 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
41constexpr int POINTER_1_UP =
42 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
43constexpr int POINTER_2_UP =
44 AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
45constexpr int DOWN = AMOTION_EVENT_ACTION_DOWN;
46constexpr int MOVE = AMOTION_EVENT_ACTION_MOVE;
47constexpr int UP = AMOTION_EVENT_ACTION_UP;
48constexpr int CANCEL = AMOTION_EVENT_ACTION_CANCEL;
49
50struct PointerData {
51 float x;
52 float y;
53 float major;
54};
55
56static NotifyMotionArgs generateMotionArgs(nsecs_t downTime, nsecs_t eventTime, int32_t action,
57 const std::vector<PointerData>& points) {
58 size_t pointerCount = points.size();
59 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
60 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
61 }
62
63 PointerProperties pointerProperties[pointerCount];
64 PointerCoords pointerCoords[pointerCount];
65
66 for (size_t i = 0; i < pointerCount; i++) {
67 pointerProperties[i].clear();
68 pointerProperties[i].id = i;
69 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
70
71 pointerCoords[i].clear();
72 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
73 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
74 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, points[i].major);
75 }
76
77 // Define a valid motion event.
78 NotifyMotionArgs args(/* id */ 0, eventTime, 0 /*readTime*/, DEVICE_ID,
79 AINPUT_SOURCE_TOUCHSCREEN, 0 /*displayId*/, POLICY_FLAG_PASS_TO_USER,
80 action, /* actionButton */ 0,
81 /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
82 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount,
83 pointerProperties, pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
84 AMOTION_EVENT_INVALID_CURSOR_POSITION,
85 AMOTION_EVENT_INVALID_CURSOR_POSITION, downTime, /* videoFrames */ {});
86
87 return args;
88}
89
90static InputDeviceInfo generateTestDeviceInfo() {
91 InputDeviceIdentifier identifier;
92
93 auto info = InputDeviceInfo();
94 info.initialize(DEVICE_ID, /*generation*/ 1, /*controllerNumber*/ 1, identifier, "alias",
95 /*isExternal*/ false, /*hasMic*/ false);
96 info.addSource(AINPUT_SOURCE_TOUCHSCREEN);
97 info.addMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN, 0, 1599, /*flat*/ 0,
98 /*fuzz*/ 0, X_RESOLUTION);
99 info.addMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN, 0, 2559, /*flat*/ 0,
100 /*fuzz*/ 0, Y_RESOLUTION);
101 info.addMotionRange(AMOTION_EVENT_AXIS_TOUCH_MAJOR, AINPUT_SOURCE_TOUCHSCREEN, 0, 255,
102 /*flat*/ 0, /*fuzz*/ 0, MAJOR_RESOLUTION);
103
104 return info;
105}
106
107static AndroidPalmFilterDeviceInfo generatePalmFilterDeviceInfo() {
108 InputDeviceInfo androidInfo = generateTestDeviceInfo();
109 std::optional<AndroidPalmFilterDeviceInfo> info = createPalmFilterDeviceInfo(androidInfo);
110 if (!info) {
111 ADD_FAILURE() << "Could not convert android device info to ::ui version";
112 return {};
113 }
114 return *info;
115}
116
117TEST(DeviceInfoConversionTest, TabletDeviceTest) {
118 AndroidPalmFilterDeviceInfo info = generatePalmFilterDeviceInfo();
119 ASSERT_EQ(X_RESOLUTION, info.x_res);
120 ASSERT_EQ(Y_RESOLUTION, info.y_res);
121 ASSERT_EQ(MAJOR_RESOLUTION, info.touch_major_res);
122 ASSERT_EQ(1599, info.max_x);
123 ASSERT_EQ(2559, info.max_y);
124}
125
126static void assertArgs(const NotifyMotionArgs& args, int32_t action,
127 const std::vector<std::pair<int32_t /*pointerId*/, PointerData>>& pointers) {
128 ASSERT_EQ(action, args.action);
129 ASSERT_EQ(pointers.size(), args.pointerCount);
130 for (size_t i = 0; i < args.pointerCount; i++) {
131 const auto& [pointerId, pointerData] = pointers[i];
132 ASSERT_EQ(pointerId, args.pointerProperties[i].id);
133 ASSERT_EQ(pointerData.x, args.pointerCoords[i].getX());
134 ASSERT_EQ(pointerData.y, args.pointerCoords[i].getY());
135 ASSERT_EQ(pointerData.major,
136 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR));
137 }
138}
139
140TEST(RemovePointerIdsTest, RemoveOnePointer) {
141 NotifyMotionArgs args = generateMotionArgs(/*downTime*/ 0, /*eventTime*/ 0,
142 AMOTION_EVENT_ACTION_MOVE, {{1, 2, 3}, {4, 5, 6}});
143
144 NotifyMotionArgs pointer1Only = removePointerIds(args, {0});
145 assertArgs(pointer1Only, AMOTION_EVENT_ACTION_MOVE, {{1, {4, 5, 6}}});
146
147 NotifyMotionArgs pointer0Only = removePointerIds(args, {1});
148 assertArgs(pointer0Only, AMOTION_EVENT_ACTION_MOVE, {{0, {1, 2, 3}}});
149}
150
151/**
152 * Remove 2 out of 3 pointers during a MOVE event.
153 */
154TEST(RemovePointerIdsTest, RemoveTwoPointers) {
155 NotifyMotionArgs args =
156 generateMotionArgs(/*downTime*/ 0, /*eventTime*/ 0, AMOTION_EVENT_ACTION_MOVE,
157 {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
158
159 NotifyMotionArgs pointer1Only = removePointerIds(args, {0, 2});
160 assertArgs(pointer1Only, AMOTION_EVENT_ACTION_MOVE, {{1, {4, 5, 6}}});
161}
162
163/**
164 * Remove an active pointer during a POINTER_DOWN event, and also remove a non-active
165 * pointer during a POINTER_DOWN event.
166 */
167TEST(RemovePointerIdsTest, ActionPointerDown) {
168 NotifyMotionArgs args = generateMotionArgs(/*downTime*/ 0, /*eventTime*/ 0, POINTER_1_DOWN,
169 {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
170
171 NotifyMotionArgs pointers0And2 = removePointerIds(args, {1});
172 assertArgs(pointers0And2, ACTION_UNKNOWN, {{0, {1, 2, 3}}, {2, {7, 8, 9}}});
173
174 NotifyMotionArgs pointers1And2 = removePointerIds(args, {0});
175 assertArgs(pointers1And2, POINTER_0_DOWN, {{1, {4, 5, 6}}, {2, {7, 8, 9}}});
176}
177
178/**
179 * Remove all pointers during a MOVE event.
180 */
181TEST(RemovePointerIdsTest, RemoveAllPointersDuringMove) {
182 NotifyMotionArgs args = generateMotionArgs(/*downTime*/ 0, /*eventTime*/ 0,
183 AMOTION_EVENT_ACTION_MOVE, {{1, 2, 3}, {4, 5, 6}});
184
185 NotifyMotionArgs noPointers = removePointerIds(args, {0, 1});
186 ASSERT_EQ(0u, noPointers.pointerCount);
187}
188
189/**
190 * If we have ACTION_POINTER_DOWN, and we remove all pointers except for the active pointer,
191 * then we should just have ACTION_DOWN. Likewise, a POINTER_UP event should become an UP event.
192 */
193TEST(RemovePointerIdsTest, PointerDownBecomesDown) {
194 NotifyMotionArgs args = generateMotionArgs(/*downTime*/ 0, /*eventTime*/ 0, POINTER_1_DOWN,
195 {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
196
197 NotifyMotionArgs pointer1 = removePointerIds(args, {0, 2});
198 assertArgs(pointer1, DOWN, {{1, {4, 5, 6}}});
199
200 args.action = POINTER_1_UP;
201 pointer1 = removePointerIds(args, {0, 2});
202 assertArgs(pointer1, UP, {{1, {4, 5, 6}}});
203}
204
205/**
206 * If a pointer that is now going down is canceled, then we can just drop the POINTER_DOWN event.
207 */
208TEST(CancelSuppressedPointersTest, CanceledPointerDownIsDropped) {
209 NotifyMotionArgs args = generateMotionArgs(/*downTime*/ 0, /*eventTime*/ 0, POINTER_1_DOWN,
210 {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
211 std::vector<NotifyMotionArgs> result =
212 cancelSuppressedPointers(args, /*oldSuppressedPointerIds*/ {},
213 /*newSuppressedPointerIds*/ {1});
214 ASSERT_TRUE(result.empty());
215}
216
217/**
218 * If a pointer is already suppressed, the POINTER_UP event for this pointer should be dropped
219 */
220TEST(CancelSuppressedPointersTest, SuppressedPointerUpIsDropped) {
221 NotifyMotionArgs args = generateMotionArgs(/*downTime*/ 0, /*eventTime*/ 0, POINTER_1_UP,
222 {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
223 std::vector<NotifyMotionArgs> result =
224 cancelSuppressedPointers(args, /*oldSuppressedPointerIds*/ {1},
225 /*newSuppressedPointerIds*/ {1});
226 ASSERT_TRUE(result.empty());
227}
228
229/**
230 * If a pointer is already suppressed, it should be removed from a MOVE event.
231 */
232TEST(CancelSuppressedPointersTest, SuppressedPointerIsRemovedDuringMove) {
233 NotifyMotionArgs args = generateMotionArgs(/*downTime*/ 0, /*eventTime*/ 0, MOVE,
234 {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
235 std::vector<NotifyMotionArgs> result =
236 cancelSuppressedPointers(args, /*oldSuppressedPointerIds*/ {1},
237 /*newSuppressedPointerIds*/ {1});
238 ASSERT_EQ(1u, result.size());
239 assertArgs(result[0], MOVE, {{0, {1, 2, 3}}, {2, {7, 8, 9}}});
240}
241
242/**
243 * If a pointer just got canceled during a MOVE event, we should see two events:
244 * 1) ACTION_POINTER_UP with FLAG_CANCELED so that this pointer is lifted
245 * 2) A MOVE event without this pointer
246 */
247TEST(CancelSuppressedPointersTest, NewlySuppressedPointerIsCanceled) {
248 NotifyMotionArgs args = generateMotionArgs(/*downTime*/ 0, /*eventTime*/ 0, MOVE,
249 {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
250 std::vector<NotifyMotionArgs> result =
251 cancelSuppressedPointers(args, /*oldSuppressedPointerIds*/ {},
252 /*newSuppressedPointerIds*/ {1});
253 ASSERT_EQ(2u, result.size());
254 assertArgs(result[0], POINTER_1_UP, {{0, {1, 2, 3}}, {1, {4, 5, 6}}, {2, {7, 8, 9}}});
255 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, result[0].flags);
256 assertArgs(result[1], MOVE, {{0, {1, 2, 3}}, {2, {7, 8, 9}}});
257}
258
259/**
260 * If we have a single pointer that gets canceled during a MOVE, the entire gesture
261 * should be canceled with ACTION_CANCEL.
262 */
263TEST(CancelSuppressedPointersTest, SingleSuppressedPointerIsCanceled) {
264 NotifyMotionArgs args = generateMotionArgs(/*downTime*/ 0, /*eventTime*/ 0, MOVE, {{1, 2, 3}});
265 std::vector<NotifyMotionArgs> result =
266 cancelSuppressedPointers(args, /*oldSuppressedPointerIds*/ {},
267 /*newSuppressedPointerIds*/ {0});
268 ASSERT_EQ(1u, result.size());
269 assertArgs(result[0], CANCEL, {{0, {1, 2, 3}}});
270 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, result[0].flags);
271}
272
273/**
274 * If one of 3 pointers gets canceled during a POINTER_UP event, we should proceed with POINTER_UP,
275 * but this event should also have FLAG_CANCELED to indicate that this pointer was unintentional.
276 */
277TEST(CancelSuppressedPointersTest, SuppressedPointer1GoingUpIsCanceled) {
278 NotifyMotionArgs args = generateMotionArgs(/*downTime*/ 0, /*eventTime*/ 0, POINTER_1_UP,
279 {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
280 std::vector<NotifyMotionArgs> result =
281 cancelSuppressedPointers(args, /*oldSuppressedPointerIds*/ {},
282 /*newSuppressedPointerIds*/ {1});
283 ASSERT_EQ(1u, result.size());
284 assertArgs(result[0], POINTER_1_UP, {{0, {1, 2, 3}}, {1, {4, 5, 6}}, {2, {7, 8, 9}}});
285 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, result[0].flags);
286}
287
288/**
289 * Same test as above, but we change the pointer's index to 0 instead of 1. This helps detect
290 * errors with handling pointer index inside the action.
291 */
292TEST(CancelSuppressedPointersTest, SuppressedPointer0GoingUpIsCanceled) {
293 NotifyMotionArgs args = generateMotionArgs(/*downTime*/ 0, /*eventTime*/ 0, POINTER_0_UP,
294 {{1, 2, 3}, {4, 5, 6}});
295 std::vector<NotifyMotionArgs> result =
296 cancelSuppressedPointers(args, /*oldSuppressedPointerIds*/ {},
297 /*newSuppressedPointerIds*/ {0});
298 ASSERT_EQ(1u, result.size());
299 assertArgs(result[0], POINTER_0_UP, {{0, {1, 2, 3}}, {1, {4, 5, 6}}});
300 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, result[0].flags);
301}
302
303/**
304 * If two pointers are canceled simultaneously during MOVE, we should see a single ACTION_CANCEL
305 * event. This event would cancel the entire gesture.
306 */
307TEST(CancelSuppressedPointersTest, TwoNewlySuppressedPointersAreBothCanceled) {
308 NotifyMotionArgs args =
309 generateMotionArgs(/*downTime*/ 0, /*eventTime*/ 0, MOVE, {{1, 2, 3}, {4, 5, 6}});
310 std::vector<NotifyMotionArgs> result =
311 cancelSuppressedPointers(args, /*oldSuppressedPointerIds*/ {},
312 /*newSuppressedPointerIds*/ {0, 1});
313 ASSERT_EQ(1u, result.size());
314 assertArgs(result[0], CANCEL, {{0, {1, 2, 3}}, {1, {4, 5, 6}}});
315 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, result[0].flags);
316}
317
318/**
319 * Similar test to above. During a POINTER_UP event, both pointers are detected as 'palm' and
320 * therefore should be removed. In this case, we should send a single ACTION_CANCEL that
321 * would undo the entire gesture.
322 */
323TEST(CancelSuppressedPointersTest, TwoPointersAreCanceledDuringPointerUp) {
324 NotifyMotionArgs args = generateMotionArgs(/*downTime*/ 0, /*eventTime*/ 0, POINTER_1_UP,
325 {{1, 2, 3}, {4, 5, 6}});
326 std::vector<NotifyMotionArgs> result =
327 cancelSuppressedPointers(args, /*oldSuppressedPointerIds*/ {1},
328 /*newSuppressedPointerIds*/ {0, 1});
329 ASSERT_EQ(1u, result.size());
330 assertArgs(result[0], CANCEL, {{0, {1, 2, 3}}});
331 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, result[0].flags);
332}
333
334/**
335 * When all pointers have been removed from the touch stream, and we have a new POINTER_DOWN,
336 * this should become a regular DOWN event because it's the only pointer that will be valid now.
337 */
338TEST(CancelSuppressedPointersTest, NewPointerDownBecomesDown) {
339 NotifyMotionArgs args = generateMotionArgs(/*downTime*/ 0, /*eventTime*/ 0, POINTER_2_DOWN,
340 {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
341 std::vector<NotifyMotionArgs> result =
342 cancelSuppressedPointers(args, /*oldSuppressedPointerIds*/ {0, 1},
343 /*newSuppressedPointerIds*/ {0, 1});
344 ASSERT_EQ(1u, result.size());
345 assertArgs(result[0], DOWN, {{2, {7, 8, 9}}});
346 ASSERT_EQ(0, result[0].flags);
347}
348
349/**
350 * Call 'getTouches' for a DOWN event and check that the resulting 'InProgressTouchEvdev'
351 * struct is populated as expected.
352 */
353TEST(GetTouchesTest, ConvertDownEvent) {
354 NotifyMotionArgs args = generateMotionArgs(/*downTime*/ 0, /*eventTime*/ 0, DOWN, {{1, 2, 3}});
355 AndroidPalmFilterDeviceInfo deviceInfo = generatePalmFilterDeviceInfo();
356 SlotState slotState;
357 SlotState oldSlotState = slotState;
358 slotState.update(args);
359 std::vector<::ui::InProgressTouchEvdev> touches =
360 getTouches(args, deviceInfo, oldSlotState, slotState);
361 ASSERT_EQ(1u, touches.size());
362 ::ui::InProgressTouchEvdev expected;
363
364 expected.major = 3;
365 expected.minor = 0;
366 expected.tool_type = MT_TOOL_FINGER;
367 expected.altered = true;
368 expected.was_cancelled = false;
369 expected.cancelled = false;
370 expected.delayed = false;
371 expected.was_delayed = false;
372 expected.held = false;
373 expected.was_held = false;
374 expected.was_touching = false;
375 expected.touching = true;
376 expected.x = 1;
377 expected.y = 2;
378 expected.tracking_id = 0;
379 std::optional<size_t> slot = slotState.getSlotForPointerId(0);
380 ASSERT_TRUE(slot);
381 expected.slot = *slot;
382 expected.pressure = 0;
383 expected.tool_code = BTN_TOOL_FINGER;
384 expected.reported_tool_type = ::ui::EventPointerType::kTouch;
385 expected.stylus_button = false;
386
387 ASSERT_EQ(expected, touches[0]) << toString(touches[0]);
388}
389
390// --- UnwantedInteractionBlockerTest ---
391
392class UnwantedInteractionBlockerTest : public testing::Test {
393protected:
394 TestInputListener mTestListener;
395 std::unique_ptr<UnwantedInteractionBlockerInterface> mBlocker;
396
397 void SetUp() override {
398 mBlocker = std::make_unique<UnwantedInteractionBlocker>(mTestListener,
399 /*enablePalmRejection*/ true);
400 }
401};
402
403/**
404 * Create a basic configuration change and send it to input classifier.
405 * Expect that the event is received by the next input stage, unmodified.
406 */
407TEST_F(UnwantedInteractionBlockerTest, ConfigurationChangedIsPassedToNextListener) {
408 // Create a basic configuration change and send to classifier
409 NotifyConfigurationChangedArgs args(1 /*sequenceNum*/, 2 /*eventTime*/);
410
411 mBlocker->notifyConfigurationChanged(&args);
412 NotifyConfigurationChangedArgs outArgs;
413 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyConfigurationChangedWasCalled(&outArgs));
414 ASSERT_EQ(args, outArgs);
415}
416
417/**
418 * Keys are not handled in 'UnwantedInteractionBlocker' and should be passed
419 * to next stage unmodified.
420 */
421TEST_F(UnwantedInteractionBlockerTest, KeyIsPassedToNextListener) {
422 // Create a basic key event and send to classifier
423 NotifyKeyArgs args(1 /*sequenceNum*/, 2 /*eventTime*/, 21 /*readTime*/, 3 /*deviceId*/,
424 AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_DEFAULT, 0 /*policyFlags*/,
425 AKEY_EVENT_ACTION_DOWN, 4 /*flags*/, AKEYCODE_HOME, 5 /*scanCode*/,
426 AMETA_NONE, 6 /*downTime*/);
427
428 mBlocker->notifyKey(&args);
429 NotifyKeyArgs outArgs;
430 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyKeyWasCalled(&outArgs));
431 ASSERT_EQ(args, outArgs);
432}
433
434/**
435 * Create a basic motion event. Since it's just a DOWN event, it should not
436 * be detected as palm and should be sent to the next listener stage
437 * unmodified.
438 */
439TEST_F(UnwantedInteractionBlockerTest, DownEventIsPassedToNextListener) {
440 NotifyMotionArgs motionArgs =
441 generateMotionArgs(0 /*downTime*/, 0 /*eventTime*/, DOWN, {{1, 2, 3}});
442 mBlocker->notifyMotion(&motionArgs);
443 NotifyMotionArgs args;
444 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyMotionWasCalled(&args));
445 ASSERT_EQ(motionArgs, args);
446}
447
448/**
449 * Create a basic switch event and send it to the UnwantedInteractionBlocker.
450 * Expect that the event is received by the next input stage, unmodified.
451 */
452TEST_F(UnwantedInteractionBlockerTest, SwitchIsPassedToNextListener) {
453 NotifySwitchArgs args(1 /*sequenceNum*/, 2 /*eventTime*/, 3 /*policyFlags*/, 4 /*switchValues*/,
454 5 /*switchMask*/);
455
456 mBlocker->notifySwitch(&args);
457 NotifySwitchArgs outArgs;
458 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifySwitchWasCalled(&outArgs));
459 ASSERT_EQ(args, outArgs);
460}
461
462/**
463 * Create a basic device reset event and send it to UnwantedInteractionBlocker.
464 * Expect that the event is received by the next input stage, unmodified.
465 */
466TEST_F(UnwantedInteractionBlockerTest, DeviceResetIsPassedToNextListener) {
467 NotifyDeviceResetArgs args(1 /*sequenceNum*/, 2 /*eventTime*/, DEVICE_ID);
468
469 mBlocker->notifyDeviceReset(&args);
470 NotifyDeviceResetArgs outArgs;
471 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyDeviceResetWasCalled(&outArgs));
472 ASSERT_EQ(args, outArgs);
473}
474
475/**
476 * The state should be reset when device reset happens. That means, we can reset in the middle of a
477 * gesture, and start a new stream. There should be no crash. If the state wasn't reset correctly,
478 * a crash due to inconsistent event stream could have occurred.
479 */
480TEST_F(UnwantedInteractionBlockerTest, NoCrashWhenResetHappens) {
481 NotifyMotionArgs args;
482 mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
483 mBlocker->notifyMotion(
484 &(args = generateMotionArgs(0 /*downTime*/, 1 /*eventTime*/, DOWN, {{1, 2, 3}})));
485 mBlocker->notifyMotion(
486 &(args = generateMotionArgs(0 /*downTime*/, 2 /*eventTime*/, MOVE, {{4, 5, 6}})));
487 NotifyDeviceResetArgs resetArgs(1 /*sequenceNum*/, 3 /*eventTime*/, DEVICE_ID);
488 mBlocker->notifyDeviceReset(&resetArgs);
489 // Start a new gesture with a DOWN event, even though the previous event stream was incomplete.
490 mBlocker->notifyMotion(
491 &(args = generateMotionArgs(0 /*downTime*/, 4 /*eventTime*/, DOWN, {{7, 8, 9}})));
492}
493
Siarhei Vishniakoue844e012022-03-08 11:06:34 -0800494TEST_F(UnwantedInteractionBlockerTest, NoCrashWhenStylusSourceWithFingerToolIsReceived) {
495 mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
496 NotifyMotionArgs args = generateMotionArgs(0 /*downTime*/, 1 /*eventTime*/, DOWN, {{1, 2, 3}});
497 args.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
498 args.source = AINPUT_SOURCE_STYLUS;
499 mBlocker->notifyMotion(&args);
500}
501
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700502/**
503 * If input devices have changed, but the important device info that's used by the
504 * UnwantedInteractionBlocker has not changed, there should not be a reset.
505 */
506TEST_F(UnwantedInteractionBlockerTest, NoResetIfDeviceInfoChanges) {
507 NotifyMotionArgs args;
508 mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
509 mBlocker->notifyMotion(
510 &(args = generateMotionArgs(0 /*downTime*/, 1 /*eventTime*/, DOWN, {{1, 2, 3}})));
511 mBlocker->notifyMotion(
512 &(args = generateMotionArgs(0 /*downTime*/, 2 /*eventTime*/, MOVE, {{4, 5, 6}})));
513
514 // Now pretend the device changed, even though nothing is different for DEVICE_ID in practice.
515 mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
516
517 // The MOVE event continues the gesture that started before 'devices changed', so it should not
518 // cause a crash.
519 mBlocker->notifyMotion(
520 &(args = generateMotionArgs(0 /*downTime*/, 4 /*eventTime*/, MOVE, {{7, 8, 9}})));
521}
522
Siarhei Vishniakou814ace32022-03-04 15:12:16 -0800523/**
524 * Send a touch event, and then a stylus event. Make sure that both work.
525 */
526TEST_F(UnwantedInteractionBlockerTest, StylusAfterTouchWorks) {
527 NotifyMotionArgs args;
528 mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
529 args = generateMotionArgs(0 /*downTime*/, 0 /*eventTime*/, DOWN, {{1, 2, 3}});
530 mBlocker->notifyMotion(&args);
531 args = generateMotionArgs(0 /*downTime*/, 1 /*eventTime*/, MOVE, {{4, 5, 6}});
532 mBlocker->notifyMotion(&args);
533 args = generateMotionArgs(0 /*downTime*/, 2 /*eventTime*/, UP, {{4, 5, 6}});
534 mBlocker->notifyMotion(&args);
535
536 // Now touch down stylus
537 args = generateMotionArgs(3 /*downTime*/, 3 /*eventTime*/, DOWN, {{10, 20, 30}});
538 args.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
539 args.source |= AINPUT_SOURCE_STYLUS;
540 mBlocker->notifyMotion(&args);
541 args = generateMotionArgs(3 /*downTime*/, 4 /*eventTime*/, MOVE, {{40, 50, 60}});
542 args.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
543 args.source |= AINPUT_SOURCE_STYLUS;
544 mBlocker->notifyMotion(&args);
545 args = generateMotionArgs(3 /*downTime*/, 5 /*eventTime*/, UP, {{40, 50, 60}});
546 args.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
547 args.source |= AINPUT_SOURCE_STYLUS;
548 mBlocker->notifyMotion(&args);
549}
550
Siarhei Vishniakoua91d8572022-05-17 05:03:42 -0700551/**
552 * Call dump, and on another thread, try to send some motions. The blocker should
553 * not crash. On 2022 hardware, this test requires ~ 13K executions (about 20 seconds) to reproduce
554 * the original bug. This is meant to be run with "--gtest_repeat=100000 --gtest_break_on_failure"
555 * options
556 */
557TEST_F(UnwantedInteractionBlockerTest, DumpCanBeAccessedOnAnotherThread) {
558 mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
559 NotifyMotionArgs args1 = generateMotionArgs(0 /*downTime*/, 0 /*eventTime*/, DOWN, {{1, 2, 3}});
560 mBlocker->notifyMotion(&args1);
561 std::thread dumpThread([this]() {
562 std::string dump;
563 mBlocker->dump(dump);
564 });
565 NotifyMotionArgs args2 = generateMotionArgs(0 /*downTime*/, 1 /*eventTime*/, MOVE, {{4, 5, 6}});
566 mBlocker->notifyMotion(&args2);
567 NotifyMotionArgs args3 = generateMotionArgs(0 /*downTime*/, 2 /*eventTime*/, UP, {{4, 5, 6}});
568 mBlocker->notifyMotion(&args3);
569 dumpThread.join();
570}
571
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700572using UnwantedInteractionBlockerTestDeathTest = UnwantedInteractionBlockerTest;
573
574/**
575 * The state should be reset when device reset happens. If we receive an inconsistent event after
576 * the reset happens, crash should occur.
577 */
578TEST_F(UnwantedInteractionBlockerTestDeathTest, InconsistentEventAfterResetCausesACrash) {
579 ScopedSilentDeath _silentDeath;
580 NotifyMotionArgs args;
581 mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
582 mBlocker->notifyMotion(
583 &(args = generateMotionArgs(0 /*downTime*/, 1 /*eventTime*/, DOWN, {{1, 2, 3}})));
584 mBlocker->notifyMotion(
585 &(args = generateMotionArgs(0 /*downTime*/, 2 /*eventTime*/, MOVE, {{4, 5, 6}})));
586 NotifyDeviceResetArgs resetArgs(1 /*sequenceNum*/, 3 /*eventTime*/, DEVICE_ID);
587 mBlocker->notifyDeviceReset(&resetArgs);
588 // Sending MOVE without a DOWN -> should crash!
589 ASSERT_DEATH(
590 {
591 mBlocker->notifyMotion(&(args = generateMotionArgs(0 /*downTime*/, 4 /*eventTime*/,
592 MOVE, {{7, 8, 9}})));
593 },
594 "Could not find slot");
595}
596
597/**
598 * There should be a crash when an inconsistent event is received.
599 */
600TEST_F(UnwantedInteractionBlockerTestDeathTest, WhenMoveWithoutDownCausesACrash) {
601 ScopedSilentDeath _silentDeath;
602 NotifyMotionArgs args = generateMotionArgs(0 /*downTime*/, 1 /*eventTime*/, MOVE, {{1, 2, 3}});
603 mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
604 ASSERT_DEATH({ mBlocker->notifyMotion(&args); }, "Could not find slot");
605}
606
607class PalmRejectorTest : public testing::Test {
608protected:
609 std::unique_ptr<PalmRejector> mPalmRejector;
610
611 void SetUp() override {
612 AndroidPalmFilterDeviceInfo info = generatePalmFilterDeviceInfo();
613 mPalmRejector = std::make_unique<PalmRejector>(info);
614 }
615};
616
617using PalmRejectorTestDeathTest = PalmRejectorTest;
618
619TEST_F(PalmRejectorTestDeathTest, InconsistentEventCausesACrash) {
620 ScopedSilentDeath _silentDeath;
621 constexpr nsecs_t downTime = 0;
622 NotifyMotionArgs args =
623 generateMotionArgs(downTime, 2 /*eventTime*/, MOVE, {{1406.0, 650.0, 52.0}});
624 ASSERT_DEATH({ mPalmRejector->processMotion(args); }, "Could not find slot");
625}
626
627/**
628 * Use PalmRejector with actual touchscreen data and real model.
629 * Two pointers that should both be classified as palms.
630 */
631TEST_F(PalmRejectorTest, TwoPointersAreCanceled) {
632 std::vector<NotifyMotionArgs> argsList;
633 constexpr nsecs_t downTime = 255955749837000;
634
635 mPalmRejector->processMotion(
636 generateMotionArgs(downTime, downTime, DOWN, {{1342.0, 613.0, 79.0}}));
637 mPalmRejector->processMotion(
638 generateMotionArgs(downTime, 255955759313000, MOVE, {{1406.0, 650.0, 52.0}}));
639 mPalmRejector->processMotion(
640 generateMotionArgs(downTime, 255955766361000, MOVE, {{1429.0, 672.0, 46.0}}));
641 mPalmRejector->processMotion(
642 generateMotionArgs(downTime, 255955775989000, MOVE, {{1417.0, 685.0, 41.0}}));
643 mPalmRejector->processMotion(
644 generateMotionArgs(downTime, 255955775989000, POINTER_1_DOWN,
645 {{1417.0, 685.0, 41.0}, {1062.0, 697.0, 10.0}}));
646 mPalmRejector->processMotion(
647 generateMotionArgs(downTime, 255955783039000, MOVE,
648 {{1414.0, 702.0, 41.0}, {1059.0, 731.0, 12.0}}));
649 mPalmRejector->processMotion(
650 generateMotionArgs(downTime, 255955792536000, MOVE,
651 {{1415.0, 719.0, 44.0}, {1060.0, 760.0, 11.0}}));
652 mPalmRejector->processMotion(
653 generateMotionArgs(downTime, 255955799474000, MOVE,
654 {{1421.0, 733.0, 42.0}, {1065.0, 769.0, 13.0}}));
655 mPalmRejector->processMotion(
656 generateMotionArgs(downTime, 255955809177000, MOVE,
657 {{1426.0, 742.0, 43.0}, {1068.0, 771.0, 13.0}}));
658 mPalmRejector->processMotion(
659 generateMotionArgs(downTime, 255955816131000, MOVE,
660 {{1430.0, 748.0, 45.0}, {1069.0, 772.0, 13.0}}));
661 argsList = mPalmRejector->processMotion(
662 generateMotionArgs(downTime, 255955825907000, MOVE,
663 {{1432.0, 750.0, 44.0}, {1069.0, 772.0, 13.0}}));
664 ASSERT_EQ(1u, argsList.size());
665 ASSERT_EQ(0 /* No FLAG_CANCELED */, argsList[0].flags);
666 argsList = mPalmRejector->processMotion(
667 generateMotionArgs(downTime, 255955832736000, MOVE,
668 {{1433.0, 751.0, 44.0}, {1070.0, 771.0, 13.0}}));
669 ASSERT_EQ(2u, argsList.size());
670 ASSERT_EQ(POINTER_0_UP, argsList[0].action);
671 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, argsList[0].flags);
672 ASSERT_EQ(MOVE, argsList[1].action);
673 ASSERT_EQ(1u, argsList[1].pointerCount);
674 ASSERT_EQ(0, argsList[1].flags);
675
676 mPalmRejector->processMotion(
677 generateMotionArgs(downTime, 255955842432000, MOVE,
678 {{1433.0, 751.0, 42.0}, {1071.0, 770.0, 13.0}}));
679 mPalmRejector->processMotion(
680 generateMotionArgs(downTime, 255955849380000, MOVE,
681 {{1433.0, 751.0, 45.0}, {1072.0, 769.0, 13.0}}));
682 mPalmRejector->processMotion(
683 generateMotionArgs(downTime, 255955859046000, MOVE,
684 {{1433.0, 751.0, 43.0}, {1072.0, 768.0, 13.0}}));
685 argsList = mPalmRejector->processMotion(
686 generateMotionArgs(downTime, 255955869823000, MOVE,
687 {{1433.0, 751.0, 45.0}, {1072.0, 767.0, 13.0}}));
688 ASSERT_EQ(1u, argsList.size());
689 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, argsList[0].action);
690 mPalmRejector->processMotion(
691 generateMotionArgs(downTime, 255955875641000, MOVE,
692 {{1433.0, 751.0, 43.0}, {1072.0, 766.0, 13.0}}));
693 mPalmRejector->processMotion(
694 generateMotionArgs(downTime, 255955882693000, MOVE,
695 {{1433.0, 750.0, 44.0}, {1072.0, 765.0, 13.0}}));
696 mPalmRejector->processMotion(
697 generateMotionArgs(downTime, 255955892324000, MOVE,
698 {{1433.0, 750.0, 42.0}, {1072.0, 763.0, 14.0}}));
699 mPalmRejector->processMotion(
700 generateMotionArgs(downTime, 255955899425000, MOVE,
701 {{1434.0, 750.0, 44.0}, {1073.0, 761.0, 14.0}}));
702 mPalmRejector->processMotion(
703 generateMotionArgs(downTime, 255955909400000, MOVE,
704 {{1435.0, 750.0, 43.0}, {1073.0, 759.0, 15.0}}));
705 mPalmRejector->processMotion(
706 generateMotionArgs(downTime, 255955915885000, MOVE,
707 {{1436.0, 750.0, 45.0}, {1074.0, 757.0, 15.0}}));
708 mPalmRejector->processMotion(
709 generateMotionArgs(downTime, 255955925607000, MOVE,
710 {{1436.0, 750.0, 44.0}, {1074.0, 755.0, 15.0}}));
711 mPalmRejector->processMotion(
712 generateMotionArgs(downTime, 255955932580000, MOVE,
713 {{1436.0, 750.0, 45.0}, {1074.0, 753.0, 15.0}}));
714 mPalmRejector->processMotion(
715 generateMotionArgs(downTime, 255955942231000, MOVE,
716 {{1436.0, 749.0, 44.0}, {1074.0, 751.0, 15.0}}));
717 mPalmRejector->processMotion(
718 generateMotionArgs(downTime, 255955949204000, MOVE,
719 {{1435.0, 748.0, 45.0}, {1074.0, 749.0, 15.0}}));
720 mPalmRejector->processMotion(
721 generateMotionArgs(downTime, 255955959103000, MOVE,
722 {{1434.0, 746.0, 44.0}, {1074.0, 747.0, 14.0}}));
723 mPalmRejector->processMotion(
724 generateMotionArgs(downTime, 255955965884000, MOVE,
725 {{1433.0, 744.0, 44.0}, {1075.0, 745.0, 14.0}}));
726 mPalmRejector->processMotion(
727 generateMotionArgs(downTime, 255955975649000, MOVE,
728 {{1431.0, 741.0, 43.0}, {1075.0, 742.0, 13.0}}));
729 mPalmRejector->processMotion(
730 generateMotionArgs(downTime, 255955982537000, MOVE,
731 {{1428.0, 738.0, 43.0}, {1076.0, 739.0, 12.0}}));
732 mPalmRejector->processMotion(
733 generateMotionArgs(downTime, 255955992284000, MOVE,
734 {{1400.0, 726.0, 54.0}, {1076.0, 739.0, 13.0}}));
735 argsList = mPalmRejector->processMotion(
736 generateMotionArgs(downTime, 255955999348000, POINTER_1_UP,
737 {{1362.0, 716.0, 55.0}, {1076.0, 739.0, 13.0}}));
738 ASSERT_TRUE(argsList.empty());
739 mPalmRejector->processMotion(
740 generateMotionArgs(downTime, 255955999348000, MOVE, {{1362.0, 716.0, 55.0}}));
741 mPalmRejector->processMotion(
742 generateMotionArgs(downTime, 255956008885000, MOVE, {{1347.0, 707.0, 54.0}}));
743 mPalmRejector->processMotion(
744 generateMotionArgs(downTime, 255956015791000, MOVE, {{1340.0, 698.0, 54.0}}));
745 mPalmRejector->processMotion(
746 generateMotionArgs(downTime, 255956025804000, MOVE, {{1338.0, 694.0, 55.0}}));
747 mPalmRejector->processMotion(
748 generateMotionArgs(downTime, 255956032314000, MOVE, {{1336.0, 690.0, 53.0}}));
749 mPalmRejector->processMotion(
750 generateMotionArgs(downTime, 255956042329000, MOVE, {{1334.0, 685.0, 47.0}}));
751 mPalmRejector->processMotion(
752 generateMotionArgs(downTime, 255956048979000, MOVE, {{1333.0, 679.0, 46.0}}));
753 mPalmRejector->processMotion(
754 generateMotionArgs(downTime, 255956058813000, MOVE, {{1332.0, 672.0, 45.0}}));
755 mPalmRejector->processMotion(
756 generateMotionArgs(downTime, 255956065592000, MOVE, {{1333.0, 666.0, 40.0}}));
757 mPalmRejector->processMotion(
758 generateMotionArgs(downTime, 255956075276000, MOVE, {{1336.0, 661.0, 24.0}}));
759 mPalmRejector->processMotion(
760 generateMotionArgs(downTime, 255956082198000, MOVE, {{1338.0, 656.0, 16.0}}));
761 mPalmRejector->processMotion(
762 generateMotionArgs(downTime, 255956092059000, MOVE, {{1341.0, 649.0, 1.0}}));
763 argsList = mPalmRejector->processMotion(
764 generateMotionArgs(downTime, 255956098764000, UP, {{1341.0, 649.0, 1.0}}));
765 ASSERT_TRUE(argsList.empty());
766}
767
768/**
769 * A test implementation of PalmDetectionFilter that allows you to specify which pointer you want
770 * the model to consider 'suppressed'. The pointer is specified using its position (x, y).
771 * Current limitation:
772 * Pointers may not cross each other in space during motion. Otherwise, any pointer with the
773 * position matching the suppressed position will be considered "palm".
774 */
775class TestFilter : public ::ui::PalmDetectionFilter {
776public:
777 TestFilter(::ui::SharedPalmDetectionFilterState* state,
778 std::vector<std::pair<float, float>>& suppressedPointers)
779 : ::ui::PalmDetectionFilter(state), mSuppressedPointers(suppressedPointers) {}
780
781 void Filter(const std::vector<::ui::InProgressTouchEvdev>& touches, ::base::TimeTicks time,
782 std::bitset<::ui::kNumTouchEvdevSlots>* slots_to_hold,
783 std::bitset<::ui::kNumTouchEvdevSlots>* slots_to_suppress) override {
784 updateSuppressedSlots(touches);
785 *slots_to_suppress = mSuppressedSlots;
786 }
787
788 std::string FilterNameForTesting() const override { return "test filter"; }
789
790private:
791 void updateSuppressedSlots(const std::vector<::ui::InProgressTouchEvdev>& touches) {
792 for (::ui::InProgressTouchEvdev touch : touches) {
793 for (const auto& [x, y] : mSuppressedPointers) {
794 const float dx = (touch.x - x);
795 const float dy = (touch.y - y);
796 const float distanceSquared = dx * dx + dy * dy;
797 if (distanceSquared < 1) {
798 mSuppressedSlots.set(touch.slot, true);
799 }
800 }
801 }
802 }
803
804 std::bitset<::ui::kNumTouchEvdevSlots> mSuppressedSlots;
805 std::vector<std::pair<float, float>>& mSuppressedPointers;
806};
807
808class PalmRejectorFakeFilterTest : public testing::Test {
809protected:
810 std::unique_ptr<PalmRejector> mPalmRejector;
811
812 void SetUp() override {
813 std::unique_ptr<::ui::PalmDetectionFilter> filter =
814 std::make_unique<TestFilter>(&mSharedPalmState, /*byref*/ mSuppressedPointers);
815 mPalmRejector =
816 std::make_unique<PalmRejector>(generatePalmFilterDeviceInfo(), std::move(filter));
817 }
818
819 void suppressPointerAtPosition(float x, float y) { mSuppressedPointers.push_back({x, y}); }
820
821private:
822 std::vector<std::pair<float, float>> mSuppressedPointers;
823 ::ui::SharedPalmDetectionFilterState mSharedPalmState; // unused, but we must retain ownership
824};
825
826/**
827 * When a MOVE event happens, the model identifies the pointer as palm. At that time, the palm
828 * rejector should send a POINTER_UP event for this pointer with FLAG_CANCELED, and subsequent
829 * events should have this pointer removed.
830 */
831TEST_F(PalmRejectorFakeFilterTest, OneOfTwoPointersIsCanceled) {
832 std::vector<NotifyMotionArgs> argsList;
833 constexpr nsecs_t downTime = 0;
834
835 mPalmRejector->processMotion(
836 generateMotionArgs(downTime, downTime, DOWN, {{1342.0, 613.0, 79.0}}));
837 mPalmRejector->processMotion(
838 generateMotionArgs(downTime, 1, POINTER_1_DOWN,
839 {{1417.0, 685.0, 41.0}, {1062.0, 697.0, 10.0}}));
840 // Cancel the second pointer
841 suppressPointerAtPosition(1059, 731);
842 argsList = mPalmRejector->processMotion(
843 generateMotionArgs(downTime, 255955783039000, MOVE,
844 {{1414.0, 702.0, 41.0}, {1059.0, 731.0, 12.0}}));
845 ASSERT_EQ(2u, argsList.size());
846 // First event - cancel pointer 1
847 ASSERT_EQ(POINTER_1_UP, argsList[0].action);
848 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, argsList[0].flags);
849 // Second event - send MOVE for the remaining pointer
850 ASSERT_EQ(MOVE, argsList[1].action);
851 ASSERT_EQ(0, argsList[1].flags);
852
853 // Future move events only contain 1 pointer, because the second pointer will continue
854 // to be suppressed
855 argsList = mPalmRejector->processMotion(
856 generateMotionArgs(downTime, 255955783039000, MOVE,
857 {{1433.0, 751.0, 43.0}, {1072.0, 766.0, 13.0}}));
858 ASSERT_EQ(1u, argsList.size());
859 ASSERT_EQ(MOVE, argsList[0].action);
860 ASSERT_EQ(1u, argsList[0].pointerCount);
861 ASSERT_EQ(1433, argsList[0].pointerCoords[0].getX());
862 ASSERT_EQ(751, argsList[0].pointerCoords[0].getY());
863}
864
865/**
866 * Send two pointers, and suppress both of them. Check that ACTION_CANCEL is generated.
867 * Afterwards:
868 * 1) Future MOVE events are ignored.
869 * 2) When a new pointer goes down, ACTION_DOWN is generated
870 */
871TEST_F(PalmRejectorFakeFilterTest, NewDownEventAfterCancel) {
872 std::vector<NotifyMotionArgs> argsList;
873 constexpr nsecs_t downTime = 0;
874
875 mPalmRejector->processMotion(
876 generateMotionArgs(downTime, downTime, DOWN, {{1342.0, 613.0, 79.0}}));
877 mPalmRejector->processMotion(
878 generateMotionArgs(downTime, 1, POINTER_1_DOWN,
879 {{1417.0, 685.0, 41.0}, {1062.0, 697.0, 10.0}}));
880 // Cancel both pointers
881 suppressPointerAtPosition(1059, 731);
882 suppressPointerAtPosition(1400, 680);
883 argsList = mPalmRejector->processMotion(
884 generateMotionArgs(downTime, 1, MOVE, {{1400, 680, 41}, {1059, 731, 10}}));
885 ASSERT_EQ(1u, argsList.size());
886 // Cancel all
887 ASSERT_EQ(CANCEL, argsList[0].action);
888 ASSERT_EQ(2u, argsList[0].pointerCount);
889 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, argsList[0].flags);
890
891 // Future move events are ignored
892 argsList = mPalmRejector->processMotion(
893 generateMotionArgs(downTime, 255955783039000, MOVE,
894 {{1433.0, 751.0, 43.0}, {1072.0, 766.0, 13.0}}));
895 ASSERT_EQ(0u, argsList.size());
896
897 // When a new pointer goes down, a new DOWN event is generated
898 argsList = mPalmRejector->processMotion(
899 generateMotionArgs(downTime, 255955783039000, POINTER_2_DOWN,
900 {{1433.0, 751.0, 43.0}, {1072.0, 766.0, 13.0}, {1000, 700, 10}}));
901 ASSERT_EQ(1u, argsList.size());
902 ASSERT_EQ(DOWN, argsList[0].action);
903 ASSERT_EQ(1u, argsList[0].pointerCount);
904 ASSERT_EQ(2, argsList[0].pointerProperties[0].id);
905}
906
907/**
908 * 2 pointers are classified as palm simultaneously. When they are later
909 * released by Android, make sure that we drop both of these POINTER_UP events.
910 * Since they are classified as palm at the same time, we just need to receive a single CANCEL
911 * event. From MotionEvent docs: """A pointer id remains valid until the pointer eventually goes up
912 * (indicated by ACTION_UP or ACTION_POINTER_UP) or when the gesture is canceled (indicated by
913 * ACTION_CANCEL)."""
914 * This means that generating additional POINTER_UP events is not necessary.
915 * The risk here is that "oldSuppressedPointerIds" will not be correct, because it will update after
916 * each motion, but pointers are canceled one at a time by Android.
917 */
918TEST_F(PalmRejectorFakeFilterTest, TwoPointersCanceledWhenOnePointerGoesUp) {
919 std::vector<NotifyMotionArgs> argsList;
920 constexpr nsecs_t downTime = 0;
921
922 mPalmRejector->processMotion(
923 generateMotionArgs(downTime, downTime, DOWN, {{1342.0, 613.0, 79.0}}));
924 mPalmRejector->processMotion(
925 generateMotionArgs(downTime, /*eventTime*/ 1, POINTER_1_DOWN,
926 {{1417.0, 685.0, 41.0}, {1062.0, 697.0, 10.0}}));
927 // Suppress both pointers!!
928 suppressPointerAtPosition(1414, 702);
929 suppressPointerAtPosition(1059, 731);
930 argsList = mPalmRejector->processMotion(
931 generateMotionArgs(downTime, 255955783039000, POINTER_1_UP,
932 {{1414.0, 702.0, 41.0}, {1059.0, 731.0, 12.0}}));
933 ASSERT_EQ(1u, argsList.size());
934 ASSERT_EQ(CANCEL, argsList[0].action) << MotionEvent::actionToString(argsList[0].action);
935 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, argsList[0].flags);
936
937 // Future move events should not go to the listener.
938 argsList = mPalmRejector->processMotion(
939 generateMotionArgs(downTime, 255955783049000, MOVE, {{1435.0, 755.0, 43.0}}));
940 ASSERT_EQ(0u, argsList.size());
941
942 argsList = mPalmRejector->processMotion(
943 generateMotionArgs(downTime, 255955783059000, UP, {{1436.0, 756.0, 43.0}}));
944 ASSERT_EQ(0u, argsList.size());
945}
946
947/**
948 * Send 3 pointers, and then cancel one of them during a MOVE event. We should see ACTION_POINTER_UP
949 * generated for that. Next, another pointer is canceled during ACTION_POINTER_DOWN. For that
950 * pointer, we simply shouldn't send the event.
951 */
952TEST_F(PalmRejectorFakeFilterTest, CancelTwoPointers) {
953 std::vector<NotifyMotionArgs> argsList;
954 constexpr nsecs_t downTime = 0;
955
956 mPalmRejector->processMotion(
957 generateMotionArgs(downTime, downTime, DOWN, {{1342.0, 613.0, 79.0}}));
958 mPalmRejector->processMotion(
959 generateMotionArgs(downTime, /*eventTime*/ 1, POINTER_1_DOWN,
960 {{1417.0, 685.0, 41.0}, {1062.0, 697.0, 10.0}}));
961
962 // Suppress second pointer (pointer 1)
963 suppressPointerAtPosition(1060, 700);
964 argsList = mPalmRejector->processMotion(
965 generateMotionArgs(downTime, /*eventTime*/ 1, MOVE,
966 {{1417.0, 685.0, 41.0}, {1060, 700, 10.0}}));
967 ASSERT_EQ(2u, argsList.size());
968 ASSERT_EQ(POINTER_1_UP, argsList[0].action);
969 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, argsList[0].flags);
970
971 ASSERT_EQ(MOVE, argsList[1].action) << MotionEvent::actionToString(argsList[1].action);
972 ASSERT_EQ(0, argsList[1].flags);
973
974 // A new pointer goes down and gets suppressed right away. It should just be dropped
975 suppressPointerAtPosition(1001, 601);
976 argsList = mPalmRejector->processMotion(
977 generateMotionArgs(downTime, /*eventTime*/ 1, POINTER_2_DOWN,
978 {{1417.0, 685.0, 41.0}, {1062.0, 697.0, 10.0}, {1001, 601, 5}}));
979
980 ASSERT_EQ(0u, argsList.size());
981 // Likewise, pointer that's already canceled should be ignored
982 argsList = mPalmRejector->processMotion(
983 generateMotionArgs(downTime, /*eventTime*/ 1, POINTER_2_UP,
984 {{1417.0, 685.0, 41.0}, {1062.0, 697.0, 10.0}, {1001, 601, 5}}));
985 ASSERT_EQ(0u, argsList.size());
986
987 // Cancel all pointers when pointer 1 goes up. Pointer 1 was already canceled earlier.
988 suppressPointerAtPosition(1417, 685);
989 argsList = mPalmRejector->processMotion(
990 generateMotionArgs(downTime, /*eventTime*/ 1, POINTER_1_UP,
991 {{1417.0, 685.0, 41.0}, {1062.0, 697.0, 10.0}}));
992 ASSERT_EQ(1u, argsList.size());
993 ASSERT_EQ(CANCEL, argsList[0].action);
994}
995
996} // namespace android