blob: 853f628a13485b0389bc26806ea92fb9efd69ab2 [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>
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070021#include <linux/input.h>
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -070022#include <thread>
Siarhei Vishniakou88151b82022-08-11 00:53:38 +000023#include "ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter.h"
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070024
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000025#include "TestEventMatchers.h"
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070026#include "TestInputListener.h"
27
Siarhei Vishniakou88151b82022-08-11 00:53:38 +000028using ::testing::AllOf;
29
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070030namespace android {
31
32constexpr int32_t DEVICE_ID = 3;
33constexpr int32_t X_RESOLUTION = 11;
34constexpr int32_t Y_RESOLUTION = 11;
35constexpr int32_t MAJOR_RESOLUTION = 1;
36
Siarhei Vishniakou88151b82022-08-11 00:53:38 +000037const nsecs_t RESAMPLE_PERIOD = ::ui::kResamplePeriod.InNanoseconds();
38
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070039constexpr int POINTER_0_DOWN =
40 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
41constexpr int POINTER_1_DOWN =
42 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
43constexpr int POINTER_2_DOWN =
44 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
45constexpr int POINTER_0_UP =
46 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
47constexpr int POINTER_1_UP =
48 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
49constexpr int POINTER_2_UP =
50 AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
51constexpr int DOWN = AMOTION_EVENT_ACTION_DOWN;
52constexpr int MOVE = AMOTION_EVENT_ACTION_MOVE;
53constexpr int UP = AMOTION_EVENT_ACTION_UP;
54constexpr int CANCEL = AMOTION_EVENT_ACTION_CANCEL;
55
Siarhei Vishniakou88151b82022-08-11 00:53:38 +000056constexpr int32_t FLAG_CANCELED = AMOTION_EVENT_FLAG_CANCELED;
57
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -070058static nsecs_t toNs(std::chrono::nanoseconds duration) {
59 return duration.count();
60}
61
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070062struct PointerData {
63 float x;
64 float y;
65 float major;
66};
67
68static 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;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070081 pointerProperties[i].toolType = ToolType::FINGER;
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070082
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.
Harry Cutts101ee9b2023-07-06 18:04:14 +000090 NotifyMotionArgs args(/*id=*/0, eventTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070091 ui::LogicalDisplayId::DEFAULT, POLICY_FLAG_PASS_TO_USER, action,
Linnan Li13bf76a2024-05-05 19:18:02 +080092 /*actionButton=*/0,
Harry Cutts101ee9b2023-07-06 18:04:14 +000093 /*flags=*/0, AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE,
94 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
95 pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070096 AMOTION_EVENT_INVALID_CURSOR_POSITION,
Harry Cutts101ee9b2023-07-06 18:04:14 +000097 AMOTION_EVENT_INVALID_CURSOR_POSITION, downTime, /*videoFrames=*/{});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070098
99 return args;
100}
101
102static InputDeviceInfo generateTestDeviceInfo() {
103 InputDeviceIdentifier identifier;
104
105 auto info = InputDeviceInfo();
Harry Cutts101ee9b2023-07-06 18:04:14 +0000106 info.initialize(DEVICE_ID, /*generation=*/1, /*controllerNumber=*/1, identifier, "alias",
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700107 /*isExternal=*/false, /*hasMic=*/false, ui::LogicalDisplayId::INVALID);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700108 info.addSource(AINPUT_SOURCE_TOUCHSCREEN);
Harry Cutts101ee9b2023-07-06 18:04:14 +0000109 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);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700113 info.addMotionRange(AMOTION_EVENT_AXIS_TOUCH_MAJOR, AINPUT_SOURCE_TOUCHSCREEN, 0, 255,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000114 /*flat=*/0, /*fuzz=*/0, MAJOR_RESOLUTION);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700115
116 return info;
117}
118
119static 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
129TEST(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
138static void assertArgs(const NotifyMotionArgs& args, int32_t action,
139 const std::vector<std::pair<int32_t /*pointerId*/, PointerData>>& pointers) {
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -0700140 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++) {
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700144 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) {
Harry Cutts101ee9b2023-07-06 18:04:14 +0000154 NotifyMotionArgs args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700155 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 =
Harry Cutts101ee9b2023-07-06 18:04:14 +0000169 generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700170 {{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) {
Harry Cutts101ee9b2023-07-06 18:04:14 +0000181 NotifyMotionArgs args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, POINTER_1_DOWN,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700182 {{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) {
Harry Cutts101ee9b2023-07-06 18:04:14 +0000195 NotifyMotionArgs args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700196 AMOTION_EVENT_ACTION_MOVE, {{1, 2, 3}, {4, 5, 6}});
197
198 NotifyMotionArgs noPointers = removePointerIds(args, {0, 1});
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -0700199 ASSERT_EQ(0u, noPointers.getPointerCount());
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700200}
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) {
Harry Cutts101ee9b2023-07-06 18:04:14 +0000207 NotifyMotionArgs args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, POINTER_1_DOWN,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700208 {{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) {
Harry Cutts101ee9b2023-07-06 18:04:14 +0000222 NotifyMotionArgs args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, POINTER_1_DOWN,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700223 {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
224 std::vector<NotifyMotionArgs> result =
Harry Cutts101ee9b2023-07-06 18:04:14 +0000225 cancelSuppressedPointers(args, /*oldSuppressedPointerIds=*/{},
226 /*newSuppressedPointerIds=*/{1});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700227 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) {
Harry Cutts101ee9b2023-07-06 18:04:14 +0000234 NotifyMotionArgs args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, POINTER_1_UP,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700235 {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
236 std::vector<NotifyMotionArgs> result =
Harry Cutts101ee9b2023-07-06 18:04:14 +0000237 cancelSuppressedPointers(args, /*oldSuppressedPointerIds=*/{1},
238 /*newSuppressedPointerIds=*/{1});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700239 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) {
Harry Cutts101ee9b2023-07-06 18:04:14 +0000246 NotifyMotionArgs args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700247 {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
248 std::vector<NotifyMotionArgs> result =
Harry Cutts101ee9b2023-07-06 18:04:14 +0000249 cancelSuppressedPointers(args, /*oldSuppressedPointerIds=*/{1},
250 /*newSuppressedPointerIds=*/{1});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700251 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) {
Harry Cutts101ee9b2023-07-06 18:04:14 +0000261 NotifyMotionArgs args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700262 {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
263 std::vector<NotifyMotionArgs> result =
Harry Cutts101ee9b2023-07-06 18:04:14 +0000264 cancelSuppressedPointers(args, /*oldSuppressedPointerIds=*/{},
265 /*newSuppressedPointerIds=*/{1});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700266 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) {
Harry Cutts101ee9b2023-07-06 18:04:14 +0000277 NotifyMotionArgs args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, MOVE, {{1, 2, 3}});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700278 std::vector<NotifyMotionArgs> result =
Harry Cutts101ee9b2023-07-06 18:04:14 +0000279 cancelSuppressedPointers(args, /*oldSuppressedPointerIds=*/{},
280 /*newSuppressedPointerIds=*/{0});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700281 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) {
Harry Cutts101ee9b2023-07-06 18:04:14 +0000291 NotifyMotionArgs args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, POINTER_1_UP,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700292 {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
293 std::vector<NotifyMotionArgs> result =
Harry Cutts101ee9b2023-07-06 18:04:14 +0000294 cancelSuppressedPointers(args, /*oldSuppressedPointerIds=*/{},
295 /*newSuppressedPointerIds=*/{1});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700296 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) {
Harry Cutts101ee9b2023-07-06 18:04:14 +0000306 NotifyMotionArgs args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, POINTER_0_UP,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700307 {{1, 2, 3}, {4, 5, 6}});
308 std::vector<NotifyMotionArgs> result =
Harry Cutts101ee9b2023-07-06 18:04:14 +0000309 cancelSuppressedPointers(args, /*oldSuppressedPointerIds=*/{},
310 /*newSuppressedPointerIds=*/{0});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700311 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 =
Harry Cutts101ee9b2023-07-06 18:04:14 +0000322 generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, MOVE, {{1, 2, 3}, {4, 5, 6}});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700323 std::vector<NotifyMotionArgs> result =
Harry Cutts101ee9b2023-07-06 18:04:14 +0000324 cancelSuppressedPointers(args, /*oldSuppressedPointerIds=*/{},
325 /*newSuppressedPointerIds=*/{0, 1});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700326 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) {
Harry Cutts101ee9b2023-07-06 18:04:14 +0000337 NotifyMotionArgs args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, POINTER_1_UP,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700338 {{1, 2, 3}, {4, 5, 6}});
339 std::vector<NotifyMotionArgs> result =
Harry Cutts101ee9b2023-07-06 18:04:14 +0000340 cancelSuppressedPointers(args, /*oldSuppressedPointerIds=*/{1},
341 /*newSuppressedPointerIds=*/{0, 1});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700342 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) {
Harry Cutts101ee9b2023-07-06 18:04:14 +0000352 NotifyMotionArgs args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, POINTER_2_DOWN,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700353 {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
354 std::vector<NotifyMotionArgs> result =
Harry Cutts101ee9b2023-07-06 18:04:14 +0000355 cancelSuppressedPointers(args, /*oldSuppressedPointerIds=*/{0, 1},
356 /*newSuppressedPointerIds=*/{0, 1});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700357 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) {
Harry Cutts101ee9b2023-07-06 18:04:14 +0000367 NotifyMotionArgs args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, DOWN, {{1, 2, 3}});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700368 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
Prabir Pradhan678438e2023-04-13 19:32:51 +0000424 mBlocker->notifyConfigurationChanged(args);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700425 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,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700437 AINPUT_SOURCE_KEYBOARD, ui::LogicalDisplayId::DEFAULT, /*policyFlags=*/0,
Harry Cutts33476232023-01-30 19:57:29 +0000438 AKEY_EVENT_ACTION_DOWN, /*flags=*/4, AKEYCODE_HOME, /*scanCode=*/5,
439 AMETA_NONE, /*downTime=*/6);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700440
Prabir Pradhan678438e2023-04-13 19:32:51 +0000441 mBlocker->notifyKey(args);
442 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyKeyWasCalled(testing::Eq(args)));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700443}
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 */
450TEST_F(UnwantedInteractionBlockerTest, DownEventIsPassedToNextListener) {
451 NotifyMotionArgs motionArgs =
Harry Cutts33476232023-01-30 19:57:29 +0000452 generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, DOWN, {{1, 2, 3}});
Prabir Pradhan678438e2023-04-13 19:32:51 +0000453 mBlocker->notifyMotion(motionArgs);
454 ASSERT_NO_FATAL_FAILURE(mTestListener.assertNotifyMotionWasCalled(testing::Eq(motionArgs)));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700455}
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 */
461TEST_F(UnwantedInteractionBlockerTest, SwitchIsPassedToNextListener) {
Harry Cutts33476232023-01-30 19:57:29 +0000462 NotifySwitchArgs args(/*sequenceNum=*/1, /*eventTime=*/2, /*policyFlags=*/3,
463 /*switchValues=*/4, /*switchMask=*/5);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700464
Prabir Pradhan678438e2023-04-13 19:32:51 +0000465 mBlocker->notifySwitch(args);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700466 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 */
475TEST_F(UnwantedInteractionBlockerTest, DeviceResetIsPassedToNextListener) {
Harry Cutts33476232023-01-30 19:57:29 +0000476 NotifyDeviceResetArgs args(/*sequenceNum=*/1, /*eventTime=*/2, DEVICE_ID);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700477
Prabir Pradhan678438e2023-04-13 19:32:51 +0000478 mBlocker->notifyDeviceReset(args);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700479 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 */
489TEST_F(UnwantedInteractionBlockerTest, NoCrashWhenResetHappens) {
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000490 mBlocker->notifyInputDevicesChanged({/*id=*/0, {generateTestDeviceInfo()}});
Prabir Pradhan678438e2023-04-13 19:32:51 +0000491 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});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700494 // Start a new gesture with a DOWN event, even though the previous event stream was incomplete.
Prabir Pradhan678438e2023-04-13 19:32:51 +0000495 mBlocker->notifyMotion(generateMotionArgs(/*downTime=*/0, /*eventTime=*/4, DOWN, {{7, 8, 9}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700496}
497
Siarhei Vishniakou27568392022-03-08 11:06:34 -0800498TEST_F(UnwantedInteractionBlockerTest, NoCrashWhenStylusSourceWithFingerToolIsReceived) {
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000499 mBlocker->notifyInputDevicesChanged({/*id=*/0, {generateTestDeviceInfo()}});
Harry Cutts33476232023-01-30 19:57:29 +0000500 NotifyMotionArgs args = generateMotionArgs(/*downTime=*/0, /*eventTime=*/1, DOWN, {{1, 2, 3}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700501 args.pointerProperties[0].toolType = ToolType::FINGER;
Siarhei Vishniakou27568392022-03-08 11:06:34 -0800502 args.source = AINPUT_SOURCE_STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000503 mBlocker->notifyMotion(args);
Siarhei Vishniakou27568392022-03-08 11:06:34 -0800504}
505
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700506/**
507 * If input devices have changed, but the important device info that's used by the
508 * UnwantedInteractionBlocker has not changed, there should not be a reset.
509 */
510TEST_F(UnwantedInteractionBlockerTest, NoResetIfDeviceInfoChanges) {
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000511 mBlocker->notifyInputDevicesChanged({/*id=*/0, {generateTestDeviceInfo()}});
Prabir Pradhan678438e2023-04-13 19:32:51 +0000512 mBlocker->notifyMotion(generateMotionArgs(/*downTime=*/0, /*eventTime=*/1, DOWN, {{1, 2, 3}}));
513 mBlocker->notifyMotion(generateMotionArgs(/*downTime=*/0, /*eventTime=*/2, MOVE, {{4, 5, 6}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700514
515 // Now pretend the device changed, even though nothing is different for DEVICE_ID in practice.
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000516 mBlocker->notifyInputDevicesChanged({/*id=*/0, {generateTestDeviceInfo()}});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700517
518 // The MOVE event continues the gesture that started before 'devices changed', so it should not
519 // cause a crash.
Prabir Pradhan678438e2023-04-13 19:32:51 +0000520 mBlocker->notifyMotion(generateMotionArgs(/*downTime=*/0, /*eventTime=*/4, MOVE, {{7, 8, 9}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700521}
522
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -0800523/**
524 * Send a touch event, and then a stylus event. Make sure that both work.
525 */
526TEST_F(UnwantedInteractionBlockerTest, StylusAfterTouchWorks) {
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000527 mBlocker->notifyInputDevicesChanged({/*id=*/0, {generateTestDeviceInfo()}});
Prabir Pradhan678438e2023-04-13 19:32:51 +0000528 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}}));
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -0800531
532 // Now touch down stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +0000533 NotifyMotionArgs args;
Harry Cutts33476232023-01-30 19:57:29 +0000534 args = generateMotionArgs(/*downTime=*/3, /*eventTime=*/3, DOWN, {{10, 20, 30}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700535 args.pointerProperties[0].toolType = ToolType::STYLUS;
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -0800536 args.source |= AINPUT_SOURCE_STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000537 mBlocker->notifyMotion(args);
Harry Cutts33476232023-01-30 19:57:29 +0000538 args = generateMotionArgs(/*downTime=*/3, /*eventTime=*/4, MOVE, {{40, 50, 60}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700539 args.pointerProperties[0].toolType = ToolType::STYLUS;
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -0800540 args.source |= AINPUT_SOURCE_STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000541 mBlocker->notifyMotion(args);
Harry Cutts33476232023-01-30 19:57:29 +0000542 args = generateMotionArgs(/*downTime=*/3, /*eventTime=*/5, UP, {{40, 50, 60}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700543 args.pointerProperties[0].toolType = ToolType::STYLUS;
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -0800544 args.source |= AINPUT_SOURCE_STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000545 mBlocker->notifyMotion(args);
Siarhei Vishniakoua6a660f2022-03-04 15:12:16 -0800546}
547
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700548/**
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 */
554TEST_F(UnwantedInteractionBlockerTest, DumpCanBeAccessedOnAnotherThread) {
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000555 mBlocker->notifyInputDevicesChanged({/*id=*/0, {generateTestDeviceInfo()}});
Prabir Pradhan678438e2023-04-13 19:32:51 +0000556 mBlocker->notifyMotion(generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, DOWN, {{1, 2, 3}}));
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700557 std::thread dumpThread([this]() {
558 std::string dump;
559 mBlocker->dump(dump);
560 });
Prabir Pradhan678438e2023-04-13 19:32:51 +0000561 mBlocker->notifyMotion(generateMotionArgs(/*downTime=*/0, /*eventTime=*/1, MOVE, {{4, 5, 6}}));
562 mBlocker->notifyMotion(generateMotionArgs(/*downTime=*/0, /*eventTime=*/2, UP, {{4, 5, 6}}));
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700563 dumpThread.join();
564}
565
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000566/**
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 */
570TEST_F(UnwantedInteractionBlockerTest, HeuristicFilterWorks) {
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000571 mBlocker->notifyInputDevicesChanged({/*id=*/0, {generateTestDeviceInfo()}});
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000572 // Small touch down
Prabir Pradhan678438e2023-04-13 19:32:51 +0000573 mBlocker->notifyMotion(generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, DOWN, {{1, 2, 3}}));
Prabir Pradhan739dca42022-09-09 20:12:01 +0000574 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(DOWN));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000575
576 // Large touch oval on the next move
Prabir Pradhan678438e2023-04-13 19:32:51 +0000577 mBlocker->notifyMotion(
578 generateMotionArgs(/*downTime=*/0, RESAMPLE_PERIOD, MOVE, {{4, 5, 200}}));
Prabir Pradhan739dca42022-09-09 20:12:01 +0000579 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(MOVE));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000580
581 // Lift up the touch to force the model to decide on whether it's a palm
Prabir Pradhan678438e2023-04-13 19:32:51 +0000582 mBlocker->notifyMotion(
583 generateMotionArgs(/*downTime=*/0, 2 * RESAMPLE_PERIOD, UP, {{4, 5, 200}}));
Prabir Pradhan739dca42022-09-09 20:12:01 +0000584 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(CANCEL));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000585}
586
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000587/**
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 */
593TEST_F(UnwantedInteractionBlockerTest, StylusIsNotBlocked) {
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000594 NotifyInputDevicesChangedArgs deviceChangedArgs = {/*id=*/0, {generateTestDeviceInfo()}};
595 deviceChangedArgs.inputDeviceInfos[0].addSource(AINPUT_SOURCE_STYLUS);
596 mBlocker->notifyInputDevicesChanged(deviceChangedArgs);
Harry Cutts33476232023-01-30 19:57:29 +0000597 NotifyMotionArgs args1 = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, DOWN, {{1, 2, 3}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700598 args1.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000599 mBlocker->notifyMotion(args1);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000600 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(DOWN));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000601
602 // Move the stylus, setting large TOUCH_MAJOR/TOUCH_MINOR dimensions
603 NotifyMotionArgs args2 =
Harry Cutts33476232023-01-30 19:57:29 +0000604 generateMotionArgs(/*downTime=*/0, RESAMPLE_PERIOD, MOVE, {{4, 5, 200}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700605 args2.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000606 mBlocker->notifyMotion(args2);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000607 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(MOVE));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000608
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 =
Harry Cutts33476232023-01-30 19:57:29 +0000612 generateMotionArgs(/*downTime=*/0, 2 * RESAMPLE_PERIOD, UP, {{4, 5, 200}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700613 args3.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000614 mBlocker->notifyMotion(args3);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000615 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(UP));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000616}
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 */
623TEST_F(UnwantedInteractionBlockerTest, TouchIsBlockedWhenMixedWithStylus) {
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000624 NotifyInputDevicesChangedArgs deviceChangedArgs = {/*id=*/0, {generateTestDeviceInfo()}};
625 deviceChangedArgs.inputDeviceInfos[0].addSource(AINPUT_SOURCE_STYLUS);
626 mBlocker->notifyInputDevicesChanged(deviceChangedArgs);
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000627
628 // Touch down
Harry Cutts33476232023-01-30 19:57:29 +0000629 NotifyMotionArgs args1 = generateMotionArgs(/*downTime=*/0, /*eventTime=*/0, DOWN, {{1, 2, 3}});
Prabir Pradhan678438e2023-04-13 19:32:51 +0000630 mBlocker->notifyMotion(args1);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000631 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(DOWN));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000632
633 // Stylus pointer down
Harry Cutts33476232023-01-30 19:57:29 +0000634 NotifyMotionArgs args2 = generateMotionArgs(/*downTime=*/0, RESAMPLE_PERIOD, POINTER_1_DOWN,
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000635 {{1, 2, 3}, {10, 20, 30}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700636 args2.pointerProperties[1].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000637 mBlocker->notifyMotion(args2);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000638 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(POINTER_1_DOWN));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000639
640 // Large touch oval on the next finger move
Harry Cutts33476232023-01-30 19:57:29 +0000641 NotifyMotionArgs args3 = generateMotionArgs(/*downTime=*/0, 2 * RESAMPLE_PERIOD, MOVE,
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000642 {{1, 2, 300}, {11, 21, 30}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700643 args3.pointerProperties[1].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000644 mBlocker->notifyMotion(args3);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000645 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(MOVE));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000646
647 // Lift up the finger pointer. It should be canceled due to the heuristic filter.
Harry Cutts33476232023-01-30 19:57:29 +0000648 NotifyMotionArgs args4 = generateMotionArgs(/*downTime=*/0, 3 * RESAMPLE_PERIOD, POINTER_0_UP,
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000649 {{1, 2, 300}, {11, 21, 30}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700650 args4.pointerProperties[1].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000651 mBlocker->notifyMotion(args4);
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000652 mTestListener.assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +0000653 AllOf(WithMotionAction(POINTER_0_UP), WithFlags(FLAG_CANCELED)));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000654
655 NotifyMotionArgs args5 =
Harry Cutts33476232023-01-30 19:57:29 +0000656 generateMotionArgs(/*downTime=*/0, 4 * RESAMPLE_PERIOD, MOVE, {{12, 22, 30}});
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000657 args5.pointerProperties[0].id = args4.pointerProperties[1].id;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700658 args5.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000659 mBlocker->notifyMotion(args5);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000660 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(MOVE));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000661
662 // Lift up the stylus pointer
663 NotifyMotionArgs args6 =
Harry Cutts33476232023-01-30 19:57:29 +0000664 generateMotionArgs(/*downTime=*/0, 5 * RESAMPLE_PERIOD, UP, {{4, 5, 200}});
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000665 args6.pointerProperties[0].id = args4.pointerProperties[1].id;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700666 args6.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000667 mBlocker->notifyMotion(args6);
Prabir Pradhan739dca42022-09-09 20:12:01 +0000668 mTestListener.assertNotifyMotionWasCalled(WithMotionAction(UP));
Siarhei Vishniakou65735832022-08-09 19:18:37 +0000669}
670
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700671using 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 */
677TEST_F(UnwantedInteractionBlockerTestDeathTest, InconsistentEventAfterResetCausesACrash) {
678 ScopedSilentDeath _silentDeath;
679 NotifyMotionArgs args;
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000680 mBlocker->notifyInputDevicesChanged({/*id=*/0, {generateTestDeviceInfo()}});
Prabir Pradhan678438e2023-04-13 19:32:51 +0000681 mBlocker->notifyMotion(generateMotionArgs(/*downTime=*/0, /*eventTime=*/1, DOWN, {{1, 2, 3}}));
682 mBlocker->notifyMotion(generateMotionArgs(/*downTime=*/0, /*eventTime=*/2, MOVE, {{4, 5, 6}}));
Harry Cutts33476232023-01-30 19:57:29 +0000683 NotifyDeviceResetArgs resetArgs(/*sequenceNum=*/1, /*eventTime=*/3, DEVICE_ID);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000684 mBlocker->notifyDeviceReset(resetArgs);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700685 // Sending MOVE without a DOWN -> should crash!
686 ASSERT_DEATH(
687 {
Prabir Pradhan678438e2023-04-13 19:32:51 +0000688 mBlocker->notifyMotion(
689 generateMotionArgs(/*downTime=*/0, /*eventTime=*/4, MOVE, {{7, 8, 9}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700690 },
691 "Could not find slot");
692}
693
694/**
695 * There should be a crash when an inconsistent event is received.
696 */
697TEST_F(UnwantedInteractionBlockerTestDeathTest, WhenMoveWithoutDownCausesACrash) {
698 ScopedSilentDeath _silentDeath;
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000699 mBlocker->notifyInputDevicesChanged({/*id=*/0, {generateTestDeviceInfo()}});
Prabir Pradhan678438e2023-04-13 19:32:51 +0000700 ASSERT_DEATH(
701 {
702 mBlocker->notifyMotion(
703 generateMotionArgs(/*downTime=*/0, /*eventTime=*/1, MOVE, {{1, 2, 3}}));
704 },
705 "Could not find slot");
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700706}
707
708class PalmRejectorTest : public testing::Test {
709protected:
710 std::unique_ptr<PalmRejector> mPalmRejector;
711
712 void SetUp() override {
713 AndroidPalmFilterDeviceInfo info = generatePalmFilterDeviceInfo();
714 mPalmRejector = std::make_unique<PalmRejector>(info);
715 }
716};
717
718using PalmRejectorTestDeathTest = PalmRejectorTest;
719
720TEST_F(PalmRejectorTestDeathTest, InconsistentEventCausesACrash) {
721 ScopedSilentDeath _silentDeath;
722 constexpr nsecs_t downTime = 0;
723 NotifyMotionArgs args =
Harry Cutts33476232023-01-30 19:57:29 +0000724 generateMotionArgs(downTime, /*eventTime=*/2, MOVE, {{1406.0, 650.0, 52.0}});
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700725 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 */
732TEST_F(PalmRejectorTest, TwoPointersAreCanceled) {
733 std::vector<NotifyMotionArgs> argsList;
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700734 const nsecs_t downTime = toNs(0ms);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700735
736 mPalmRejector->processMotion(
737 generateMotionArgs(downTime, downTime, DOWN, {{1342.0, 613.0, 79.0}}));
738 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700739 generateMotionArgs(downTime, toNs(8ms), MOVE, {{1406.0, 650.0, 52.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700740 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700741 generateMotionArgs(downTime, toNs(16ms), MOVE, {{1429.0, 672.0, 46.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700742 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700743 generateMotionArgs(downTime, toNs(24ms), MOVE, {{1417.0, 685.0, 41.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700744 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700745 generateMotionArgs(downTime, toNs(32ms), POINTER_1_DOWN,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700746 {{1417.0, 685.0, 41.0}, {1062.0, 697.0, 10.0}}));
747 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700748 generateMotionArgs(downTime, toNs(40ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700749 {{1414.0, 702.0, 41.0}, {1059.0, 731.0, 12.0}}));
750 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700751 generateMotionArgs(downTime, toNs(48ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700752 {{1415.0, 719.0, 44.0}, {1060.0, 760.0, 11.0}}));
753 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700754 generateMotionArgs(downTime, toNs(56ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700755 {{1421.0, 733.0, 42.0}, {1065.0, 769.0, 13.0}}));
756 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700757 generateMotionArgs(downTime, toNs(64ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700758 {{1426.0, 742.0, 43.0}, {1068.0, 771.0, 13.0}}));
759 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700760 generateMotionArgs(downTime, toNs(72ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700761 {{1430.0, 748.0, 45.0}, {1069.0, 772.0, 13.0}}));
762 argsList = mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700763 generateMotionArgs(downTime, toNs(80ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700764 {{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(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700768 generateMotionArgs(downTime, toNs(88ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700769 {{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);
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000772 ASSERT_EQ(FLAG_CANCELED, argsList[0].flags);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700773 ASSERT_EQ(MOVE, argsList[1].action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -0700774 ASSERT_EQ(1u, argsList[1].getPointerCount());
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700775 ASSERT_EQ(0, argsList[1].flags);
776
777 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700778 generateMotionArgs(downTime, toNs(96ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700779 {{1433.0, 751.0, 42.0}, {1071.0, 770.0, 13.0}}));
780 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700781 generateMotionArgs(downTime, toNs(104ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700782 {{1433.0, 751.0, 45.0}, {1072.0, 769.0, 13.0}}));
783 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700784 generateMotionArgs(downTime, toNs(112ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700785 {{1433.0, 751.0, 43.0}, {1072.0, 768.0, 13.0}}));
786 argsList = mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700787 generateMotionArgs(downTime, toNs(120ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700788 {{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(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700792 generateMotionArgs(downTime, toNs(128ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700793 {{1433.0, 751.0, 43.0}, {1072.0, 766.0, 13.0}}));
794 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700795 generateMotionArgs(downTime, toNs(136ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700796 {{1433.0, 750.0, 44.0}, {1072.0, 765.0, 13.0}}));
797 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700798 generateMotionArgs(downTime, toNs(144ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700799 {{1433.0, 750.0, 42.0}, {1072.0, 763.0, 14.0}}));
800 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700801 generateMotionArgs(downTime, toNs(152ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700802 {{1434.0, 750.0, 44.0}, {1073.0, 761.0, 14.0}}));
803 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700804 generateMotionArgs(downTime, toNs(160ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700805 {{1435.0, 750.0, 43.0}, {1073.0, 759.0, 15.0}}));
806 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700807 generateMotionArgs(downTime, toNs(168ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700808 {{1436.0, 750.0, 45.0}, {1074.0, 757.0, 15.0}}));
809 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700810 generateMotionArgs(downTime, toNs(176ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700811 {{1436.0, 750.0, 44.0}, {1074.0, 755.0, 15.0}}));
812 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700813 generateMotionArgs(downTime, toNs(184ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700814 {{1436.0, 750.0, 45.0}, {1074.0, 753.0, 15.0}}));
815 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700816 generateMotionArgs(downTime, toNs(192ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700817 {{1436.0, 749.0, 44.0}, {1074.0, 751.0, 15.0}}));
818 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700819 generateMotionArgs(downTime, toNs(200ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700820 {{1435.0, 748.0, 45.0}, {1074.0, 749.0, 15.0}}));
821 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700822 generateMotionArgs(downTime, toNs(208ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700823 {{1434.0, 746.0, 44.0}, {1074.0, 747.0, 14.0}}));
824 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700825 generateMotionArgs(downTime, toNs(216ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700826 {{1433.0, 744.0, 44.0}, {1075.0, 745.0, 14.0}}));
827 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700828 generateMotionArgs(downTime, toNs(224ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700829 {{1431.0, 741.0, 43.0}, {1075.0, 742.0, 13.0}}));
830 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700831 generateMotionArgs(downTime, toNs(232ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700832 {{1428.0, 738.0, 43.0}, {1076.0, 739.0, 12.0}}));
833 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700834 generateMotionArgs(downTime, toNs(240ms), MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700835 {{1400.0, 726.0, 54.0}, {1076.0, 739.0, 13.0}}));
836 argsList = mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700837 generateMotionArgs(downTime, toNs(248ms), POINTER_1_UP,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700838 {{1362.0, 716.0, 55.0}, {1076.0, 739.0, 13.0}}));
839 ASSERT_TRUE(argsList.empty());
840 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700841 generateMotionArgs(downTime, toNs(256ms), MOVE, {{1362.0, 716.0, 55.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700842 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700843 generateMotionArgs(downTime, toNs(264ms), MOVE, {{1347.0, 707.0, 54.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700844 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700845 generateMotionArgs(downTime, toNs(272ms), MOVE, {{1340.0, 698.0, 54.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700846 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700847 generateMotionArgs(downTime, toNs(280ms), MOVE, {{1338.0, 694.0, 55.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700848 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700849 generateMotionArgs(downTime, toNs(288ms), MOVE, {{1336.0, 690.0, 53.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700850 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700851 generateMotionArgs(downTime, toNs(296ms), MOVE, {{1334.0, 685.0, 47.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700852 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700853 generateMotionArgs(downTime, toNs(304ms), MOVE, {{1333.0, 679.0, 46.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700854 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700855 generateMotionArgs(downTime, toNs(312ms), MOVE, {{1332.0, 672.0, 45.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700856 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700857 generateMotionArgs(downTime, toNs(320ms), MOVE, {{1333.0, 666.0, 40.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700858 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700859 generateMotionArgs(downTime, toNs(328ms), MOVE, {{1336.0, 661.0, 24.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700860 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700861 generateMotionArgs(downTime, toNs(336ms), MOVE, {{1338.0, 656.0, 16.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700862 mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700863 generateMotionArgs(downTime, toNs(344ms), MOVE, {{1341.0, 649.0, 1.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700864 argsList = mPalmRejector->processMotion(
Siarhei Vishniakoud5a47632022-06-13 13:56:56 -0700865 generateMotionArgs(downTime, toNs(352ms), UP, {{1341.0, 649.0, 1.0}}));
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700866 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 */
876class TestFilter : public ::ui::PalmDetectionFilter {
877public:
878 TestFilter(::ui::SharedPalmDetectionFilterState* state,
879 std::vector<std::pair<float, float>>& suppressedPointers)
880 : ::ui::PalmDetectionFilter(state), mSuppressedPointers(suppressedPointers) {}
881
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
889 std::string FilterNameForTesting() const override { return "test filter"; }
890
891private:
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
909class PalmRejectorFakeFilterTest : public testing::Test {
910protected:
911 std::unique_ptr<PalmRejector> mPalmRejector;
912
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
920 void suppressPointerAtPosition(float x, float y) { mSuppressedPointers.push_back({x, y}); }
921
922private:
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 */
932TEST_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);
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000949 ASSERT_EQ(FLAG_CANCELED, argsList[0].flags);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700950 // 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);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -0700961 ASSERT_EQ(1u, argsList[0].getPointerCount());
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700962 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 */
972TEST_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);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -0700989 ASSERT_EQ(2u, argsList[0].getPointerCount());
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000990 ASSERT_EQ(FLAG_CANCELED, argsList[0].flags);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700991
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);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001004 ASSERT_EQ(1u, argsList[0].getPointerCount());
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -07001005 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 */
1019TEST_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(
Harry Cutts101ee9b2023-07-06 18:04:14 +00001026 generateMotionArgs(downTime, /*eventTime=*/1, POINTER_1_DOWN,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -07001027 {{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);
Siarhei Vishniakou88151b82022-08-11 00:53:38 +00001036 ASSERT_EQ(FLAG_CANCELED, argsList[0].flags);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -07001037
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 */
1053TEST_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(
Harry Cutts101ee9b2023-07-06 18:04:14 +00001060 generateMotionArgs(downTime, /*eventTime=*/1, POINTER_1_DOWN,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -07001061 {{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(
Harry Cutts101ee9b2023-07-06 18:04:14 +00001066 generateMotionArgs(downTime, /*eventTime=*/1, MOVE,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -07001067 {{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);
Siarhei Vishniakou88151b82022-08-11 00:53:38 +00001070 ASSERT_EQ(FLAG_CANCELED, argsList[0].flags);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -07001071
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(
Harry Cutts101ee9b2023-07-06 18:04:14 +00001078 generateMotionArgs(downTime, /*eventTime=*/1, POINTER_2_DOWN,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -07001079 {{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(
Harry Cutts101ee9b2023-07-06 18:04:14 +00001084 generateMotionArgs(downTime, /*eventTime=*/1, POINTER_2_UP,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -07001085 {{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(
Harry Cutts101ee9b2023-07-06 18:04:14 +00001091 generateMotionArgs(downTime, /*eventTime=*/1, POINTER_1_UP,
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -07001092 {{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