blob: 2a9ace00c5064d110b67940badd84cdd5e67521c [file] [log] [blame]
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -07001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "../UnwantedInteractionBlocker.h"
18#include <android-base/silent_death_test.h>
Siarhei Vishniakou88151b82022-08-11 00:53:38 +000019#include <gmock/gmock.h>
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070020#include <gtest/gtest.h>
21#include <gui/constants.h>
22#include <linux/input.h>
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -070023#include <thread>
Siarhei Vishniakou88151b82022-08-11 00:53:38 +000024#include "ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter.h"
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070025
26#include "TestInputListener.h"
Prabir Pradhan739dca42022-09-09 20:12:01 +000027#include "TestInputListenerMatchers.h"
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070028
Siarhei Vishniakou88151b82022-08-11 00:53:38 +000029using ::testing::AllOf;
30
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070031namespace android {
32
33constexpr int32_t DEVICE_ID = 3;
34constexpr int32_t X_RESOLUTION = 11;
35constexpr int32_t Y_RESOLUTION = 11;
36constexpr int32_t MAJOR_RESOLUTION = 1;
37
Siarhei Vishniakou88151b82022-08-11 00:53:38 +000038const nsecs_t RESAMPLE_PERIOD = ::ui::kResamplePeriod.InNanoseconds();
39
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070040constexpr int POINTER_0_DOWN =
41 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
42constexpr int POINTER_1_DOWN =
43 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
44constexpr int POINTER_2_DOWN =
45 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
46constexpr int POINTER_0_UP =
47 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
48constexpr int POINTER_1_UP =
49 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
50constexpr int POINTER_2_UP =
51 AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
52constexpr int DOWN = AMOTION_EVENT_ACTION_DOWN;
53constexpr int MOVE = AMOTION_EVENT_ACTION_MOVE;
54constexpr int UP = AMOTION_EVENT_ACTION_UP;
55constexpr int CANCEL = AMOTION_EVENT_ACTION_CANCEL;
56
Siarhei Vishniakou88151b82022-08-11 00:53:38 +000057constexpr int32_t FLAG_CANCELED = AMOTION_EVENT_FLAG_CANCELED;
58
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -070059static nsecs_t toNs(std::chrono::nanoseconds duration) {
60 return duration.count();
61}
62
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070063struct PointerData {
64 float x;
65 float y;
66 float major;
67};
68
69static NotifyMotionArgs generateMotionArgs(nsecs_t downTime, nsecs_t eventTime, int32_t action,
70 const std::vector<PointerData>& points) {
71 size_t pointerCount = points.size();
72 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
73 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
74 }
75
76 PointerProperties pointerProperties[pointerCount];
77 PointerCoords pointerCoords[pointerCount];
78
79 for (size_t i = 0; i < pointerCount; i++) {
80 pointerProperties[i].clear();
81 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070082 pointerProperties[i].toolType = ToolType::FINGER;
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070083
84 pointerCoords[i].clear();
85 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
86 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
87 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, points[i].major);
88 }
89
90 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +000091 NotifyMotionArgs args(/* id */ 0, eventTime, /*readTime=*/0, DEVICE_ID,
92 AINPUT_SOURCE_TOUCHSCREEN, /*displayId=*/0, POLICY_FLAG_PASS_TO_USER,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070093 action, /* actionButton */ 0,
94 /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
95 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount,
96 pointerProperties, pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
97 AMOTION_EVENT_INVALID_CURSOR_POSITION,
98 AMOTION_EVENT_INVALID_CURSOR_POSITION, downTime, /* videoFrames */ {});
99
100 return args;
101}
102
103static InputDeviceInfo generateTestDeviceInfo() {
104 InputDeviceIdentifier identifier;
105
106 auto info = InputDeviceInfo();
107 info.initialize(DEVICE_ID, /*generation*/ 1, /*controllerNumber*/ 1, identifier, "alias",
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000108 /*isExternal*/ false, /*hasMic*/ false, ADISPLAY_ID_NONE);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700109 info.addSource(AINPUT_SOURCE_TOUCHSCREEN);
110 info.addMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN, 0, 1599, /*flat*/ 0,
111 /*fuzz*/ 0, X_RESOLUTION);
112 info.addMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN, 0, 2559, /*flat*/ 0,
113 /*fuzz*/ 0, Y_RESOLUTION);
114 info.addMotionRange(AMOTION_EVENT_AXIS_TOUCH_MAJOR, AINPUT_SOURCE_TOUCHSCREEN, 0, 255,
115 /*flat*/ 0, /*fuzz*/ 0, MAJOR_RESOLUTION);
116
117 return info;
118}
119
120static AndroidPalmFilterDeviceInfo generatePalmFilterDeviceInfo() {
121 InputDeviceInfo androidInfo = generateTestDeviceInfo();
122 std::optional<AndroidPalmFilterDeviceInfo> info = createPalmFilterDeviceInfo(androidInfo);
123 if (!info) {
124 ADD_FAILURE() << "Could not convert android device info to ::ui version";
125 return {};
126 }
127 return *info;
128}
129
130TEST(DeviceInfoConversionTest, TabletDeviceTest) {
131 AndroidPalmFilterDeviceInfo info = generatePalmFilterDeviceInfo();
132 ASSERT_EQ(X_RESOLUTION, info.x_res);
133 ASSERT_EQ(Y_RESOLUTION, info.y_res);
134 ASSERT_EQ(MAJOR_RESOLUTION, info.touch_major_res);
135 ASSERT_EQ(1599, info.max_x);
136 ASSERT_EQ(2559, info.max_y);
137}
138
139static void assertArgs(const NotifyMotionArgs& args, int32_t action,
140 const std::vector<std::pair<int32_t /*pointerId*/, PointerData>>& pointers) {
141 ASSERT_EQ(action, args.action);
142 ASSERT_EQ(pointers.size(), args.pointerCount);
143 for (size_t i = 0; i < args.pointerCount; 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
153TEST(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 */
167TEST(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 */
180TEST(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 */
194TEST(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.pointerCount);
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 */
206TEST(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 */
221TEST(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 */
233TEST(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 */
245TEST(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 */
260TEST(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}}});
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000268 ASSERT_EQ(FLAG_CANCELED, result[0].flags);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700269 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 */
276TEST(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}}});
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000283 ASSERT_EQ(FLAG_CANCELED, result[0].flags);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700284}
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 */
290TEST(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}}});
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000298 ASSERT_EQ(FLAG_CANCELED, result[0].flags);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700299}
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 */
305TEST(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}}});
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000313 ASSERT_EQ(FLAG_CANCELED, result[0].flags);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700314}
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 */
320TEST(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}}});
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000328 ASSERT_EQ(FLAG_CANCELED, result[0].flags);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700329}
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 */
336TEST(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}}});
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000344 ASSERT_EQ(FLAG_CANCELED, result[0].flags);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700345}
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 */
351TEST(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 */
366TEST(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
Siarhei Vishniakou47494ef2022-07-06 15:42:57 -0700400 ASSERT_EQ(expected, touches[0]) << touches[0];
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700401}
402
403// --- UnwantedInteractionBlockerTest ---
404
405class UnwantedInteractionBlockerTest : public testing::Test {
406protected:
407 TestInputListener mTestListener;
408 std::unique_ptr<UnwantedInteractionBlockerInterface> mBlocker;
409
410 void SetUp() override {
411 mBlocker = std::make_unique<UnwantedInteractionBlocker>(mTestListener,
Harry Cutts33476232023-01-30 19:57:29 +0000412 /*enablePalmRejection=*/true);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700413 }
414};
415
416/**
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700417 * Create a basic configuration change and send it to input processor.
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700418 * Expect that the event is received by the next input stage, unmodified.
419 */
420TEST_F(UnwantedInteractionBlockerTest, ConfigurationChangedIsPassedToNextListener) {
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700421 // Create a basic configuration change and send to blocker
Harry Cutts33476232023-01-30 19:57:29 +0000422 NotifyConfigurationChangedArgs args(/*sequenceNum=*/1, /*eventTime=*/2);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700423
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 */
434TEST_F(UnwantedInteractionBlockerTest, KeyIsPassedToNextListener) {
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700435 // Create a basic key event and send to blocker
Harry Cutts33476232023-01-30 19:57:29 +0000436 NotifyKeyArgs args(/*sequenceNum=*/1, /*eventTime=*/2, /*readTime=*/21, /*deviceId=*/3,
437 AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_DEFAULT, /*policyFlags=*/0,
438 AKEY_EVENT_ACTION_DOWN, /*flags=*/4, AKEYCODE_HOME, /*scanCode=*/5,
439 AMETA_NONE, /*downTime=*/6);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700440
441 mBlocker->notifyKey(&args);
442 NotifyKeyArgs outArgs;
443 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyKeyWasCalled(&outArgs));
444 ASSERT_EQ(args, outArgs);
445}
446
447/**
448 * Create a basic motion event. Since it's just a DOWN event, it should not
449 * be detected as palm and should be sent to the next listener stage
450 * unmodified.
451 */
452TEST_F(UnwantedInteractionBlockerTest, DownEventIsPassedToNextListener) {
453 NotifyMotionArgs motionArgs =
Harry Cutts33476232023-01-30 19:57:29 +0000454 generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, DOWN, {{1, 2, 3}});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700455 mBlocker->notifyMotion(&motionArgs);
456 NotifyMotionArgs args;
457 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyMotionWasCalled(&args));
458 ASSERT_EQ(motionArgs, args);
459}
460
461/**
462 * Create a basic switch event and send it to the UnwantedInteractionBlocker.
463 * Expect that the event is received by the next input stage, unmodified.
464 */
465TEST_F(UnwantedInteractionBlockerTest, SwitchIsPassedToNextListener) {
Harry Cutts33476232023-01-30 19:57:29 +0000466 NotifySwitchArgs args(/*sequenceNum=*/1, /*eventTime=*/2, /*policyFlags=*/3,
467 /*switchValues=*/4, /*switchMask=*/5);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700468
469 mBlocker->notifySwitch(&args);
470 NotifySwitchArgs outArgs;
471 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifySwitchWasCalled(&outArgs));
472 ASSERT_EQ(args, outArgs);
473}
474
475/**
476 * Create a basic device reset event and send it to UnwantedInteractionBlocker.
477 * Expect that the event is received by the next input stage, unmodified.
478 */
479TEST_F(UnwantedInteractionBlockerTest, DeviceResetIsPassedToNextListener) {
Harry Cutts33476232023-01-30 19:57:29 +0000480 NotifyDeviceResetArgs args(/*sequenceNum=*/1, /*eventTime=*/2, DEVICE_ID);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700481
482 mBlocker->notifyDeviceReset(&args);
483 NotifyDeviceResetArgs outArgs;
484 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyDeviceResetWasCalled(&outArgs));
485 ASSERT_EQ(args, outArgs);
486}
487
488/**
489 * The state should be reset when device reset happens. That means, we can reset in the middle of a
490 * gesture, and start a new stream. There should be no crash. If the state wasn't reset correctly,
491 * a crash due to inconsistent event stream could have occurred.
492 */
493TEST_F(UnwantedInteractionBlockerTest, NoCrashWhenResetHappens) {
494 NotifyMotionArgs args;
495 mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
496 mBlocker->notifyMotion(
Harry Cutts33476232023-01-30 19:57:29 +0000497 &(args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/1, DOWN, {{1, 2, 3}})));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700498 mBlocker->notifyMotion(
Harry Cutts33476232023-01-30 19:57:29 +0000499 &(args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/2, MOVE, {{4, 5, 6}})));
500 NotifyDeviceResetArgs resetArgs(/*sequenceNum=*/1, /*eventTime=*/3, DEVICE_ID);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700501 mBlocker->notifyDeviceReset(&resetArgs);
502 // Start a new gesture with a DOWN event, even though the previous event stream was incomplete.
503 mBlocker->notifyMotion(
Harry Cutts33476232023-01-30 19:57:29 +0000504 &(args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/4, DOWN, {{7, 8, 9}})));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700505}
506
Siarhei Vishniakou27568392022-03-08 11:06:34 -0800507TEST_F(UnwantedInteractionBlockerTest, NoCrashWhenStylusSourceWithFingerToolIsReceived) {
508 mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
Harry Cutts33476232023-01-30 19:57:29 +0000509 NotifyMotionArgs args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/1, DOWN, {{1, 2, 3}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700510 args.pointerProperties[0].toolType = ToolType::FINGER;
Siarhei Vishniakou27568392022-03-08 11:06:34 -0800511 args.source = AINPUT_SOURCE_STYLUS;
512 mBlocker->notifyMotion(&args);
513}
514
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700515/**
516 * If input devices have changed, but the important device info that's used by the
517 * UnwantedInteractionBlocker has not changed, there should not be a reset.
518 */
519TEST_F(UnwantedInteractionBlockerTest, NoResetIfDeviceInfoChanges) {
520 NotifyMotionArgs args;
521 mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
522 mBlocker->notifyMotion(
Harry Cutts33476232023-01-30 19:57:29 +0000523 &(args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/1, DOWN, {{1, 2, 3}})));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700524 mBlocker->notifyMotion(
Harry Cutts33476232023-01-30 19:57:29 +0000525 &(args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/2, MOVE, {{4, 5, 6}})));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700526
527 // Now pretend the device changed, even though nothing is different for DEVICE_ID in practice.
528 mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
529
530 // The MOVE event continues the gesture that started before 'devices changed', so it should not
531 // cause a crash.
532 mBlocker->notifyMotion(
Harry Cutts33476232023-01-30 19:57:29 +0000533 &(args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/4, MOVE, {{7, 8, 9}})));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700534}
535
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -0800536/**
537 * Send a touch event, and then a stylus event. Make sure that both work.
538 */
539TEST_F(UnwantedInteractionBlockerTest, StylusAfterTouchWorks) {
540 NotifyMotionArgs args;
541 mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
Harry Cutts33476232023-01-30 19:57:29 +0000542 args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, DOWN, {{1, 2, 3}});
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -0800543 mBlocker->notifyMotion(&args);
Harry Cutts33476232023-01-30 19:57:29 +0000544 args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/1, MOVE, {{4, 5, 6}});
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -0800545 mBlocker->notifyMotion(&args);
Harry Cutts33476232023-01-30 19:57:29 +0000546 args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/2, UP, {{4, 5, 6}});
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -0800547 mBlocker->notifyMotion(&args);
548
549 // Now touch down stylus
Harry Cutts33476232023-01-30 19:57:29 +0000550 args = generateMotionArgs(/*downTime=*/3, /*eventTime=*/3, DOWN, {{10, 20, 30}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700551 args.pointerProperties[0].toolType = ToolType::STYLUS;
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -0800552 args.source |= AINPUT_SOURCE_STYLUS;
553 mBlocker->notifyMotion(&args);
Harry Cutts33476232023-01-30 19:57:29 +0000554 args = generateMotionArgs(/*downTime=*/3, /*eventTime=*/4, MOVE, {{40, 50, 60}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700555 args.pointerProperties[0].toolType = ToolType::STYLUS;
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -0800556 args.source |= AINPUT_SOURCE_STYLUS;
557 mBlocker->notifyMotion(&args);
Harry Cutts33476232023-01-30 19:57:29 +0000558 args = generateMotionArgs(/*downTime=*/3, /*eventTime=*/5, UP, {{40, 50, 60}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700559 args.pointerProperties[0].toolType = ToolType::STYLUS;
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -0800560 args.source |= AINPUT_SOURCE_STYLUS;
561 mBlocker->notifyMotion(&args);
562}
563
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700564/**
565 * Call dump, and on another thread, try to send some motions. The blocker should
566 * not crash. On 2022 hardware, this test requires ~ 13K executions (about 20 seconds) to reproduce
567 * the original bug. This is meant to be run with "--gtest_repeat=100000 --gtest_break_on_failure"
568 * options
569 */
570TEST_F(UnwantedInteractionBlockerTest, DumpCanBeAccessedOnAnotherThread) {
571 mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
Harry Cutts33476232023-01-30 19:57:29 +0000572 NotifyMotionArgs args1 = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, DOWN, {{1, 2, 3}});
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700573 mBlocker->notifyMotion(&args1);
574 std::thread dumpThread([this]() {
575 std::string dump;
576 mBlocker->dump(dump);
577 });
Harry Cutts33476232023-01-30 19:57:29 +0000578 NotifyMotionArgs args2 = generateMotionArgs(/*downTime=*/0, /*eventTime=*/1, MOVE, {{4, 5, 6}});
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700579 mBlocker->notifyMotion(&args2);
Harry Cutts33476232023-01-30 19:57:29 +0000580 NotifyMotionArgs args3 = generateMotionArgs(/*downTime=*/0, /*eventTime=*/2, UP, {{4, 5, 6}});
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700581 mBlocker->notifyMotion(&args3);
582 dumpThread.join();
583}
584
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000585/**
586 * Heuristic filter that's present in the palm rejection model blocks touches early if the size
587 * of the touch is large. This is an integration test that checks that this filter kicks in.
588 */
589TEST_F(UnwantedInteractionBlockerTest, HeuristicFilterWorks) {
590 mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
591 // Small touch down
Harry Cutts33476232023-01-30 19:57:29 +0000592 NotifyMotionArgs args1 = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, DOWN, {{1, 2, 3}});
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000593 mBlocker->notifyMotion(&args1);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000594 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(DOWN));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000595
596 // Large touch oval on the next move
597 NotifyMotionArgs args2 =
Harry Cutts33476232023-01-30 19:57:29 +0000598 generateMotionArgs(/*downTime=*/0, RESAMPLE_PERIOD, MOVE, {{4, 5, 200}});
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000599 mBlocker->notifyMotion(&args2);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000600 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(MOVE));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000601
602 // Lift up the touch to force the model to decide on whether it's a palm
603 NotifyMotionArgs args3 =
Harry Cutts33476232023-01-30 19:57:29 +0000604 generateMotionArgs(/*downTime=*/0, 2 * RESAMPLE_PERIOD, UP, {{4, 5, 200}});
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000605 mBlocker->notifyMotion(&args3);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000606 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(CANCEL));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000607}
608
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000609/**
610 * Send a stylus event that would have triggered the heuristic palm detector if it were a touch
611 * event. However, since it's a stylus event, it should propagate without being canceled through
612 * the blocker.
613 * This is similar to `HeuristicFilterWorks` test, but for stylus tool.
614 */
615TEST_F(UnwantedInteractionBlockerTest, StylusIsNotBlocked) {
616 InputDeviceInfo info = generateTestDeviceInfo();
617 info.addSource(AINPUT_SOURCE_STYLUS);
618 mBlocker->notifyInputDevicesChanged({info});
Harry Cutts33476232023-01-30 19:57:29 +0000619 NotifyMotionArgs args1 = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, DOWN, {{1, 2, 3}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700620 args1.pointerProperties[0].toolType = ToolType::STYLUS;
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000621 mBlocker->notifyMotion(&args1);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000622 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(DOWN));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000623
624 // Move the stylus, setting large TOUCH_MAJOR/TOUCH_MINOR dimensions
625 NotifyMotionArgs args2 =
Harry Cutts33476232023-01-30 19:57:29 +0000626 generateMotionArgs(/*downTime=*/0, RESAMPLE_PERIOD, MOVE, {{4, 5, 200}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700627 args2.pointerProperties[0].toolType = ToolType::STYLUS;
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000628 mBlocker->notifyMotion(&args2);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000629 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(MOVE));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000630
631 // Lift up the stylus. If it were a touch event, this would force the model to decide on whether
632 // it's a palm.
633 NotifyMotionArgs args3 =
Harry Cutts33476232023-01-30 19:57:29 +0000634 generateMotionArgs(/*downTime=*/0, 2 * RESAMPLE_PERIOD, UP, {{4, 5, 200}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700635 args3.pointerProperties[0].toolType = ToolType::STYLUS;
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000636 mBlocker->notifyMotion(&args3);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000637 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(UP));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000638}
639
640/**
641 * Send a mixed touch and stylus event.
642 * The touch event goes first, and is a palm. The stylus event goes down after.
643 * Stylus event should continue to work even after touch is detected as a palm.
644 */
645TEST_F(UnwantedInteractionBlockerTest, TouchIsBlockedWhenMixedWithStylus) {
646 InputDeviceInfo info = generateTestDeviceInfo();
647 info.addSource(AINPUT_SOURCE_STYLUS);
648 mBlocker->notifyInputDevicesChanged({info});
649
650 // Touch down
Harry Cutts33476232023-01-30 19:57:29 +0000651 NotifyMotionArgs args1 = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, DOWN, {{1, 2, 3}});
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000652 mBlocker->notifyMotion(&args1);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000653 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(DOWN));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000654
655 // Stylus pointer down
Harry Cutts33476232023-01-30 19:57:29 +0000656 NotifyMotionArgs args2 = generateMotionArgs(/*downTime=*/0, RESAMPLE_PERIOD, POINTER_1_DOWN,
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000657 {{1, 2, 3}, {10, 20, 30}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700658 args2.pointerProperties[1].toolType = ToolType::STYLUS;
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000659 mBlocker->notifyMotion(&args2);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000660 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(POINTER_1_DOWN));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000661
662 // Large touch oval on the next finger move
Harry Cutts33476232023-01-30 19:57:29 +0000663 NotifyMotionArgs args3 = generateMotionArgs(/*downTime=*/0, 2 * RESAMPLE_PERIOD, MOVE,
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000664 {{1, 2, 300}, {11, 21, 30}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700665 args3.pointerProperties[1].toolType = ToolType::STYLUS;
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000666 mBlocker->notifyMotion(&args3);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000667 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(MOVE));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000668
669 // Lift up the finger pointer. It should be canceled due to the heuristic filter.
Harry Cutts33476232023-01-30 19:57:29 +0000670 NotifyMotionArgs args4 = generateMotionArgs(/*downTime=*/0, 3 * RESAMPLE_PERIOD, POINTER_0_UP,
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000671 {{1, 2, 300}, {11, 21, 30}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700672 args4.pointerProperties[1].toolType = ToolType::STYLUS;
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000673 mBlocker->notifyMotion(&args4);
674 mTestListener.assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +0000675 AllOf(WithMotionAction(POINTER_0_UP), WithFlags(FLAG_CANCELED)));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000676
677 NotifyMotionArgs args5 =
Harry Cutts33476232023-01-30 19:57:29 +0000678 generateMotionArgs(/*downTime=*/0, 4 * RESAMPLE_PERIOD, MOVE, {{12, 22, 30}});
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000679 args5.pointerProperties[0].id = args4.pointerProperties[1].id;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700680 args5.pointerProperties[0].toolType = ToolType::STYLUS;
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000681 mBlocker->notifyMotion(&args5);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000682 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(MOVE));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000683
684 // Lift up the stylus pointer
685 NotifyMotionArgs args6 =
Harry Cutts33476232023-01-30 19:57:29 +0000686 generateMotionArgs(/*downTime=*/0, 5 * RESAMPLE_PERIOD, UP, {{4, 5, 200}});
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000687 args6.pointerProperties[0].id = args4.pointerProperties[1].id;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700688 args6.pointerProperties[0].toolType = ToolType::STYLUS;
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000689 mBlocker->notifyMotion(&args6);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000690 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(UP));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000691}
692
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700693using UnwantedInteractionBlockerTestDeathTest = UnwantedInteractionBlockerTest;
694
695/**
696 * The state should be reset when device reset happens. If we receive an inconsistent event after
697 * the reset happens, crash should occur.
698 */
699TEST_F(UnwantedInteractionBlockerTestDeathTest, InconsistentEventAfterResetCausesACrash) {
700 ScopedSilentDeath _silentDeath;
701 NotifyMotionArgs args;
702 mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
703 mBlocker->notifyMotion(
Harry Cutts33476232023-01-30 19:57:29 +0000704 &(args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/1, DOWN, {{1, 2, 3}})));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700705 mBlocker->notifyMotion(
Harry Cutts33476232023-01-30 19:57:29 +0000706 &(args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/2, MOVE, {{4, 5, 6}})));
707 NotifyDeviceResetArgs resetArgs(/*sequenceNum=*/1, /*eventTime=*/3, DEVICE_ID);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700708 mBlocker->notifyDeviceReset(&resetArgs);
709 // Sending MOVE without a DOWN -> should crash!
710 ASSERT_DEATH(
711 {
Harry Cutts33476232023-01-30 19:57:29 +0000712 mBlocker->notifyMotion(&(args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/4,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700713 MOVE, {{7, 8, 9}})));
714 },
715 "Could not find slot");
716}
717
718/**
719 * There should be a crash when an inconsistent event is received.
720 */
721TEST_F(UnwantedInteractionBlockerTestDeathTest, WhenMoveWithoutDownCausesACrash) {
722 ScopedSilentDeath _silentDeath;
Harry Cutts33476232023-01-30 19:57:29 +0000723 NotifyMotionArgs args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/1, MOVE, {{1, 2, 3}});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700724 mBlocker->notifyInputDevicesChanged({generateTestDeviceInfo()});
725 ASSERT_DEATH({ mBlocker->notifyMotion(&args); }, "Could not find slot");
726}
727
728class PalmRejectorTest : public testing::Test {
729protected:
730 std::unique_ptr<PalmRejector> mPalmRejector;
731
732 void SetUp() override {
733 AndroidPalmFilterDeviceInfo info = generatePalmFilterDeviceInfo();
734 mPalmRejector = std::make_unique<PalmRejector>(info);
735 }
736};
737
738using PalmRejectorTestDeathTest = PalmRejectorTest;
739
740TEST_F(PalmRejectorTestDeathTest, InconsistentEventCausesACrash) {
741 ScopedSilentDeath _silentDeath;
742 constexpr nsecs_t downTime = 0;
743 NotifyMotionArgs args =
Harry Cutts33476232023-01-30 19:57:29 +0000744 generateMotionArgs(downTime, /*eventTime=*/2, MOVE, {{1406.0, 650.0, 52.0}});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700745 ASSERT_DEATH({ mPalmRejector->processMotion(args); }, "Could not find slot");
746}
747
748/**
749 * Use PalmRejector with actual touchscreen data and real model.
750 * Two pointers that should both be classified as palms.
751 */
752TEST_F(PalmRejectorTest, TwoPointersAreCanceled) {
753 std::vector<NotifyMotionArgs> argsList;
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700754 const nsecs_t downTime = toNs(0ms);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700755
756 mPalmRejector->processMotion(
757 generateMotionArgs(downTime, downTime, DOWN, {{1342.0, 613.0, 79.0}}));
758 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700759 generateMotionArgs(downTime, toNs(8ms), MOVE, {{1406.0, 650.0, 52.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700760 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700761 generateMotionArgs(downTime, toNs(16ms), MOVE, {{1429.0, 672.0, 46.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700762 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700763 generateMotionArgs(downTime, toNs(24ms), MOVE, {{1417.0, 685.0, 41.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700764 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700765 generateMotionArgs(downTime, toNs(32ms), POINTER_1_DOWN,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700766 {{1417.0, 685.0, 41.0}, {1062.0, 697.0, 10.0}}));
767 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700768 generateMotionArgs(downTime, toNs(40ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700769 {{1414.0, 702.0, 41.0}, {1059.0, 731.0, 12.0}}));
770 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700771 generateMotionArgs(downTime, toNs(48ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700772 {{1415.0, 719.0, 44.0}, {1060.0, 760.0, 11.0}}));
773 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700774 generateMotionArgs(downTime, toNs(56ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700775 {{1421.0, 733.0, 42.0}, {1065.0, 769.0, 13.0}}));
776 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700777 generateMotionArgs(downTime, toNs(64ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700778 {{1426.0, 742.0, 43.0}, {1068.0, 771.0, 13.0}}));
779 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700780 generateMotionArgs(downTime, toNs(72ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700781 {{1430.0, 748.0, 45.0}, {1069.0, 772.0, 13.0}}));
782 argsList = mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700783 generateMotionArgs(downTime, toNs(80ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700784 {{1432.0, 750.0, 44.0}, {1069.0, 772.0, 13.0}}));
785 ASSERT_EQ(1u, argsList.size());
786 ASSERT_EQ(0 /* No FLAG_CANCELED */, argsList[0].flags);
787 argsList = mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700788 generateMotionArgs(downTime, toNs(88ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700789 {{1433.0, 751.0, 44.0}, {1070.0, 771.0, 13.0}}));
790 ASSERT_EQ(2u, argsList.size());
791 ASSERT_EQ(POINTER_0_UP, argsList[0].action);
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000792 ASSERT_EQ(FLAG_CANCELED, argsList[0].flags);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700793 ASSERT_EQ(MOVE, argsList[1].action);
794 ASSERT_EQ(1u, argsList[1].pointerCount);
795 ASSERT_EQ(0, argsList[1].flags);
796
797 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700798 generateMotionArgs(downTime, toNs(96ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700799 {{1433.0, 751.0, 42.0}, {1071.0, 770.0, 13.0}}));
800 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700801 generateMotionArgs(downTime, toNs(104ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700802 {{1433.0, 751.0, 45.0}, {1072.0, 769.0, 13.0}}));
803 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700804 generateMotionArgs(downTime, toNs(112ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700805 {{1433.0, 751.0, 43.0}, {1072.0, 768.0, 13.0}}));
806 argsList = mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700807 generateMotionArgs(downTime, toNs(120ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700808 {{1433.0, 751.0, 45.0}, {1072.0, 767.0, 13.0}}));
809 ASSERT_EQ(1u, argsList.size());
810 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, argsList[0].action);
811 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700812 generateMotionArgs(downTime, toNs(128ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700813 {{1433.0, 751.0, 43.0}, {1072.0, 766.0, 13.0}}));
814 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700815 generateMotionArgs(downTime, toNs(136ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700816 {{1433.0, 750.0, 44.0}, {1072.0, 765.0, 13.0}}));
817 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700818 generateMotionArgs(downTime, toNs(144ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700819 {{1433.0, 750.0, 42.0}, {1072.0, 763.0, 14.0}}));
820 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700821 generateMotionArgs(downTime, toNs(152ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700822 {{1434.0, 750.0, 44.0}, {1073.0, 761.0, 14.0}}));
823 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700824 generateMotionArgs(downTime, toNs(160ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700825 {{1435.0, 750.0, 43.0}, {1073.0, 759.0, 15.0}}));
826 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700827 generateMotionArgs(downTime, toNs(168ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700828 {{1436.0, 750.0, 45.0}, {1074.0, 757.0, 15.0}}));
829 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700830 generateMotionArgs(downTime, toNs(176ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700831 {{1436.0, 750.0, 44.0}, {1074.0, 755.0, 15.0}}));
832 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700833 generateMotionArgs(downTime, toNs(184ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700834 {{1436.0, 750.0, 45.0}, {1074.0, 753.0, 15.0}}));
835 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700836 generateMotionArgs(downTime, toNs(192ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700837 {{1436.0, 749.0, 44.0}, {1074.0, 751.0, 15.0}}));
838 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700839 generateMotionArgs(downTime, toNs(200ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700840 {{1435.0, 748.0, 45.0}, {1074.0, 749.0, 15.0}}));
841 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700842 generateMotionArgs(downTime, toNs(208ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700843 {{1434.0, 746.0, 44.0}, {1074.0, 747.0, 14.0}}));
844 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700845 generateMotionArgs(downTime, toNs(216ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700846 {{1433.0, 744.0, 44.0}, {1075.0, 745.0, 14.0}}));
847 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700848 generateMotionArgs(downTime, toNs(224ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700849 {{1431.0, 741.0, 43.0}, {1075.0, 742.0, 13.0}}));
850 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700851 generateMotionArgs(downTime, toNs(232ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700852 {{1428.0, 738.0, 43.0}, {1076.0, 739.0, 12.0}}));
853 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700854 generateMotionArgs(downTime, toNs(240ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700855 {{1400.0, 726.0, 54.0}, {1076.0, 739.0, 13.0}}));
856 argsList = mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700857 generateMotionArgs(downTime, toNs(248ms), POINTER_1_UP,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700858 {{1362.0, 716.0, 55.0}, {1076.0, 739.0, 13.0}}));
859 ASSERT_TRUE(argsList.empty());
860 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700861 generateMotionArgs(downTime, toNs(256ms), MOVE, {{1362.0, 716.0, 55.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700862 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700863 generateMotionArgs(downTime, toNs(264ms), MOVE, {{1347.0, 707.0, 54.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700864 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700865 generateMotionArgs(downTime, toNs(272ms), MOVE, {{1340.0, 698.0, 54.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700866 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700867 generateMotionArgs(downTime, toNs(280ms), MOVE, {{1338.0, 694.0, 55.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700868 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700869 generateMotionArgs(downTime, toNs(288ms), MOVE, {{1336.0, 690.0, 53.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700870 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700871 generateMotionArgs(downTime, toNs(296ms), MOVE, {{1334.0, 685.0, 47.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700872 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700873 generateMotionArgs(downTime, toNs(304ms), MOVE, {{1333.0, 679.0, 46.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700874 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700875 generateMotionArgs(downTime, toNs(312ms), MOVE, {{1332.0, 672.0, 45.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700876 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700877 generateMotionArgs(downTime, toNs(320ms), MOVE, {{1333.0, 666.0, 40.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700878 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700879 generateMotionArgs(downTime, toNs(328ms), MOVE, {{1336.0, 661.0, 24.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700880 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700881 generateMotionArgs(downTime, toNs(336ms), MOVE, {{1338.0, 656.0, 16.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700882 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700883 generateMotionArgs(downTime, toNs(344ms), MOVE, {{1341.0, 649.0, 1.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700884 argsList = mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700885 generateMotionArgs(downTime, toNs(352ms), UP, {{1341.0, 649.0, 1.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700886 ASSERT_TRUE(argsList.empty());
887}
888
889/**
890 * A test implementation of PalmDetectionFilter that allows you to specify which pointer you want
891 * the model to consider 'suppressed'. The pointer is specified using its position (x, y).
892 * Current limitation:
893 * Pointers may not cross each other in space during motion. Otherwise, any pointer with the
894 * position matching the suppressed position will be considered "palm".
895 */
896class TestFilter : public ::ui::PalmDetectionFilter {
897public:
898 TestFilter(::ui::SharedPalmDetectionFilterState* state,
899 std::vector<std::pair<float, float>>& suppressedPointers)
900 : ::ui::PalmDetectionFilter(state), mSuppressedPointers(suppressedPointers) {}
901
902 void Filter(const std::vector<::ui::InProgressTouchEvdev>& touches, ::base::TimeTicks time,
903 std::bitset<::ui::kNumTouchEvdevSlots>* slots_to_hold,
904 std::bitset<::ui::kNumTouchEvdevSlots>* slots_to_suppress) override {
905 updateSuppressedSlots(touches);
906 *slots_to_suppress = mSuppressedSlots;
907 }
908
909 std::string FilterNameForTesting() const override { return "test filter"; }
910
911private:
912 void updateSuppressedSlots(const std::vector<::ui::InProgressTouchEvdev>& touches) {
913 for (::ui::InProgressTouchEvdev touch : touches) {
914 for (const auto& [x, y] : mSuppressedPointers) {
915 const float dx = (touch.x - x);
916 const float dy = (touch.y - y);
917 const float distanceSquared = dx * dx + dy * dy;
918 if (distanceSquared < 1) {
919 mSuppressedSlots.set(touch.slot, true);
920 }
921 }
922 }
923 }
924
925 std::bitset<::ui::kNumTouchEvdevSlots> mSuppressedSlots;
926 std::vector<std::pair<float, float>>& mSuppressedPointers;
927};
928
929class PalmRejectorFakeFilterTest : public testing::Test {
930protected:
931 std::unique_ptr<PalmRejector> mPalmRejector;
932
933 void SetUp() override {
934 std::unique_ptr<::ui::PalmDetectionFilter> filter =
935 std::make_unique<TestFilter>(&mSharedPalmState, /*byref*/ mSuppressedPointers);
936 mPalmRejector =
937 std::make_unique<PalmRejector>(generatePalmFilterDeviceInfo(), std::move(filter));
938 }
939
940 void suppressPointerAtPosition(float x, float y) { mSuppressedPointers.push_back({x, y}); }
941
942private:
943 std::vector<std::pair<float, float>> mSuppressedPointers;
944 ::ui::SharedPalmDetectionFilterState mSharedPalmState; // unused, but we must retain ownership
945};
946
947/**
948 * When a MOVE event happens, the model identifies the pointer as palm. At that time, the palm
949 * rejector should send a POINTER_UP event for this pointer with FLAG_CANCELED, and subsequent
950 * events should have this pointer removed.
951 */
952TEST_F(PalmRejectorFakeFilterTest, OneOfTwoPointersIsCanceled) {
953 std::vector<NotifyMotionArgs> argsList;
954 constexpr nsecs_t downTime = 0;
955
956 mPalmRejector->processMotion(
957 generateMotionArgs(downTime, downTime, DOWN, {{1342.0, 613.0, 79.0}}));
958 mPalmRejector->processMotion(
959 generateMotionArgs(downTime, 1, POINTER_1_DOWN,
960 {{1417.0, 685.0, 41.0}, {1062.0, 697.0, 10.0}}));
961 // Cancel the second pointer
962 suppressPointerAtPosition(1059, 731);
963 argsList = mPalmRejector->processMotion(
964 generateMotionArgs(downTime, 255955783039000, MOVE,
965 {{1414.0, 702.0, 41.0}, {1059.0, 731.0, 12.0}}));
966 ASSERT_EQ(2u, argsList.size());
967 // First event - cancel pointer 1
968 ASSERT_EQ(POINTER_1_UP, argsList[0].action);
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000969 ASSERT_EQ(FLAG_CANCELED, argsList[0].flags);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700970 // Second event - send MOVE for the remaining pointer
971 ASSERT_EQ(MOVE, argsList[1].action);
972 ASSERT_EQ(0, argsList[1].flags);
973
974 // Future move events only contain 1 pointer, because the second pointer will continue
975 // to be suppressed
976 argsList = mPalmRejector->processMotion(
977 generateMotionArgs(downTime, 255955783039000, MOVE,
978 {{1433.0, 751.0, 43.0}, {1072.0, 766.0, 13.0}}));
979 ASSERT_EQ(1u, argsList.size());
980 ASSERT_EQ(MOVE, argsList[0].action);
981 ASSERT_EQ(1u, argsList[0].pointerCount);
982 ASSERT_EQ(1433, argsList[0].pointerCoords[0].getX());
983 ASSERT_EQ(751, argsList[0].pointerCoords[0].getY());
984}
985
986/**
987 * Send two pointers, and suppress both of them. Check that ACTION_CANCEL is generated.
988 * Afterwards:
989 * 1) Future MOVE events are ignored.
990 * 2) When a new pointer goes down, ACTION_DOWN is generated
991 */
992TEST_F(PalmRejectorFakeFilterTest, NewDownEventAfterCancel) {
993 std::vector<NotifyMotionArgs> argsList;
994 constexpr nsecs_t downTime = 0;
995
996 mPalmRejector->processMotion(
997 generateMotionArgs(downTime, downTime, DOWN, {{1342.0, 613.0, 79.0}}));
998 mPalmRejector->processMotion(
999 generateMotionArgs(downTime, 1, POINTER_1_DOWN,
1000 {{1417.0, 685.0, 41.0}, {1062.0, 697.0, 10.0}}));
1001 // Cancel both pointers
1002 suppressPointerAtPosition(1059, 731);
1003 suppressPointerAtPosition(1400, 680);
1004 argsList = mPalmRejector->processMotion(
1005 generateMotionArgs(downTime, 1, MOVE, {{1400, 680, 41}, {1059, 731, 10}}));
1006 ASSERT_EQ(1u, argsList.size());
1007 // Cancel all
1008 ASSERT_EQ(CANCEL, argsList[0].action);
1009 ASSERT_EQ(2u, argsList[0].pointerCount);
Siarhei Vishniakou88151b82022-08-11 00:53:38 +00001010 ASSERT_EQ(FLAG_CANCELED, argsList[0].flags);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -07001011
1012 // Future move events are ignored
1013 argsList = mPalmRejector->processMotion(
1014 generateMotionArgs(downTime, 255955783039000, MOVE,
1015 {{1433.0, 751.0, 43.0}, {1072.0, 766.0, 13.0}}));
1016 ASSERT_EQ(0u, argsList.size());
1017
1018 // When a new pointer goes down, a new DOWN event is generated
1019 argsList = mPalmRejector->processMotion(
1020 generateMotionArgs(downTime, 255955783039000, POINTER_2_DOWN,
1021 {{1433.0, 751.0, 43.0}, {1072.0, 766.0, 13.0}, {1000, 700, 10}}));
1022 ASSERT_EQ(1u, argsList.size());
1023 ASSERT_EQ(DOWN, argsList[0].action);
1024 ASSERT_EQ(1u, argsList[0].pointerCount);
1025 ASSERT_EQ(2, argsList[0].pointerProperties[0].id);
1026}
1027
1028/**
1029 * 2 pointers are classified as palm simultaneously. When they are later
1030 * released by Android, make sure that we drop both of these POINTER_UP events.
1031 * Since they are classified as palm at the same time, we just need to receive a single CANCEL
1032 * event. From MotionEvent docs: """A pointer id remains valid until the pointer eventually goes up
1033 * (indicated by ACTION_UP or ACTION_POINTER_UP) or when the gesture is canceled (indicated by
1034 * ACTION_CANCEL)."""
1035 * This means that generating additional POINTER_UP events is not necessary.
1036 * The risk here is that "oldSuppressedPointerIds" will not be correct, because it will update after
1037 * each motion, but pointers are canceled one at a time by Android.
1038 */
1039TEST_F(PalmRejectorFakeFilterTest, TwoPointersCanceledWhenOnePointerGoesUp) {
1040 std::vector<NotifyMotionArgs> argsList;
1041 constexpr nsecs_t downTime = 0;
1042
1043 mPalmRejector->processMotion(
1044 generateMotionArgs(downTime, downTime, DOWN, {{1342.0, 613.0, 79.0}}));
1045 mPalmRejector->processMotion(
1046 generateMotionArgs(downTime, /*eventTime*/ 1, POINTER_1_DOWN,
1047 {{1417.0, 685.0, 41.0}, {1062.0, 697.0, 10.0}}));
1048 // Suppress both pointers!!
1049 suppressPointerAtPosition(1414, 702);
1050 suppressPointerAtPosition(1059, 731);
1051 argsList = mPalmRejector->processMotion(
1052 generateMotionArgs(downTime, 255955783039000, POINTER_1_UP,
1053 {{1414.0, 702.0, 41.0}, {1059.0, 731.0, 12.0}}));
1054 ASSERT_EQ(1u, argsList.size());
1055 ASSERT_EQ(CANCEL, argsList[0].action) << MotionEvent::actionToString(argsList[0].action);
Siarhei Vishniakou88151b82022-08-11 00:53:38 +00001056 ASSERT_EQ(FLAG_CANCELED, argsList[0].flags);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -07001057
1058 // Future move events should not go to the listener.
1059 argsList = mPalmRejector->processMotion(
1060 generateMotionArgs(downTime, 255955783049000, MOVE, {{1435.0, 755.0, 43.0}}));
1061 ASSERT_EQ(0u, argsList.size());
1062
1063 argsList = mPalmRejector->processMotion(
1064 generateMotionArgs(downTime, 255955783059000, UP, {{1436.0, 756.0, 43.0}}));
1065 ASSERT_EQ(0u, argsList.size());
1066}
1067
1068/**
1069 * Send 3 pointers, and then cancel one of them during a MOVE event. We should see ACTION_POINTER_UP
1070 * generated for that. Next, another pointer is canceled during ACTION_POINTER_DOWN. For that
1071 * pointer, we simply shouldn't send the event.
1072 */
1073TEST_F(PalmRejectorFakeFilterTest, CancelTwoPointers) {
1074 std::vector<NotifyMotionArgs> argsList;
1075 constexpr nsecs_t downTime = 0;
1076
1077 mPalmRejector->processMotion(
1078 generateMotionArgs(downTime, downTime, DOWN, {{1342.0, 613.0, 79.0}}));
1079 mPalmRejector->processMotion(
1080 generateMotionArgs(downTime, /*eventTime*/ 1, POINTER_1_DOWN,
1081 {{1417.0, 685.0, 41.0}, {1062.0, 697.0, 10.0}}));
1082
1083 // Suppress second pointer (pointer 1)
1084 suppressPointerAtPosition(1060, 700);
1085 argsList = mPalmRejector->processMotion(
1086 generateMotionArgs(downTime, /*eventTime*/ 1, MOVE,
1087 {{1417.0, 685.0, 41.0}, {1060, 700, 10.0}}));
1088 ASSERT_EQ(2u, argsList.size());
1089 ASSERT_EQ(POINTER_1_UP, argsList[0].action);
Siarhei Vishniakou88151b82022-08-11 00:53:38 +00001090 ASSERT_EQ(FLAG_CANCELED, argsList[0].flags);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -07001091
1092 ASSERT_EQ(MOVE, argsList[1].action) << MotionEvent::actionToString(argsList[1].action);
1093 ASSERT_EQ(0, argsList[1].flags);
1094
1095 // A new pointer goes down and gets suppressed right away. It should just be dropped
1096 suppressPointerAtPosition(1001, 601);
1097 argsList = mPalmRejector->processMotion(
1098 generateMotionArgs(downTime, /*eventTime*/ 1, POINTER_2_DOWN,
1099 {{1417.0, 685.0, 41.0}, {1062.0, 697.0, 10.0}, {1001, 601, 5}}));
1100
1101 ASSERT_EQ(0u, argsList.size());
1102 // Likewise, pointer that's already canceled should be ignored
1103 argsList = mPalmRejector->processMotion(
1104 generateMotionArgs(downTime, /*eventTime*/ 1, POINTER_2_UP,
1105 {{1417.0, 685.0, 41.0}, {1062.0, 697.0, 10.0}, {1001, 601, 5}}));
1106 ASSERT_EQ(0u, argsList.size());
1107
1108 // Cancel all pointers when pointer 1 goes up. Pointer 1 was already canceled earlier.
1109 suppressPointerAtPosition(1417, 685);
1110 argsList = mPalmRejector->processMotion(
1111 generateMotionArgs(downTime, /*eventTime*/ 1, POINTER_1_UP,
1112 {{1417.0, 685.0, 41.0}, {1062.0, 697.0, 10.0}}));
1113 ASSERT_EQ(1u, argsList.size());
1114 ASSERT_EQ(CANCEL, argsList[0].action);
1115}
1116
1117} // namespace android