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