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