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