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