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