blob: c57c251b389e3304a94d9738f80bce5f76c859b8 [file] [log] [blame]
Arpit Singh4b4a4572023-11-24 18:19:56 +00001/*
2 * Copyright 2024 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 "MultiTouchInputMapper.h"
18
19#include <android-base/logging.h>
20#include <gtest/gtest.h>
21#include <list>
22#include <optional>
23
24#include "InputMapperTest.h"
25#include "InterfaceMocks.h"
26#include "TestEventMatchers.h"
27
28#define TAG "MultiTouchpadInputMapperUnit_test"
29
30namespace android {
31
32using testing::_;
33using testing::IsEmpty;
34using testing::Return;
35using testing::SetArgPointee;
36using testing::VariantWith;
37
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070038static constexpr ui::LogicalDisplayId DISPLAY_ID = ui::LogicalDisplayId::DEFAULT;
Arpit Singh4b4a4572023-11-24 18:19:56 +000039static constexpr int32_t DISPLAY_WIDTH = 480;
40static constexpr int32_t DISPLAY_HEIGHT = 800;
41static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
42static constexpr int32_t SLOT_COUNT = 5;
43
44static constexpr int32_t ACTION_POINTER_0_UP =
45 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
46static constexpr int32_t ACTION_POINTER_1_DOWN =
47 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
48
49/**
50 * Unit tests for MultiTouchInputMapper.
51 */
52class MultiTouchInputMapperUnitTest : public InputMapperUnitTest {
53protected:
54 void SetUp() override {
55 InputMapperUnitTest::SetUp();
56
57 // Present scan codes
58 expectScanCodes(/*present=*/true,
59 {BTN_TOUCH, BTN_TOOL_FINGER, BTN_TOOL_DOUBLETAP, BTN_TOOL_TRIPLETAP,
60 BTN_TOOL_QUADTAP, BTN_TOOL_QUINTTAP});
61
62 // Missing scan codes that the mapper checks for.
63 expectScanCodes(/*present=*/false,
64 {BTN_TOOL_PEN, BTN_TOOL_RUBBER, BTN_TOOL_BRUSH, BTN_TOOL_PENCIL,
65 BTN_TOOL_AIRBRUSH});
66
67 // Current scan code state - all keys are UP by default
68 setScanCodeState(KeyState::UP, {BTN_LEFT, BTN_RIGHT, BTN_MIDDLE,
69 BTN_BACK, BTN_SIDE, BTN_FORWARD,
70 BTN_EXTRA, BTN_TASK, BTN_TOUCH,
71 BTN_STYLUS, BTN_STYLUS2, BTN_0,
72 BTN_TOOL_FINGER, BTN_TOOL_PEN, BTN_TOOL_RUBBER,
73 BTN_TOOL_BRUSH, BTN_TOOL_PENCIL, BTN_TOOL_AIRBRUSH,
74 BTN_TOOL_MOUSE, BTN_TOOL_LENS, BTN_TOOL_DOUBLETAP,
75 BTN_TOOL_TRIPLETAP, BTN_TOOL_QUADTAP, BTN_TOOL_QUINTTAP});
76
77 setKeyCodeState(KeyState::UP,
78 {AKEYCODE_STYLUS_BUTTON_PRIMARY, AKEYCODE_STYLUS_BUTTON_SECONDARY});
79
80 // Input properties - only INPUT_PROP_DIRECT for touchscreen
81 EXPECT_CALL(mMockEventHub, hasInputProperty(EVENTHUB_ID, _)).WillRepeatedly(Return(false));
82 EXPECT_CALL(mMockEventHub, hasInputProperty(EVENTHUB_ID, INPUT_PROP_DIRECT))
83 .WillRepeatedly(Return(true));
84
85 // Axes that the device has
86 setupAxis(ABS_MT_SLOT, /*valid=*/true, /*min=*/0, /*max=*/SLOT_COUNT - 1, /*resolution=*/0);
87 setupAxis(ABS_MT_TRACKING_ID, /*valid=*/true, /*min*/ 0, /*max=*/255, /*resolution=*/0);
88 setupAxis(ABS_MT_POSITION_X, /*valid=*/true, /*min=*/0, /*max=*/2000, /*resolution=*/24);
89 setupAxis(ABS_MT_POSITION_Y, /*valid=*/true, /*min=*/0, /*max=*/1000, /*resolution=*/24);
90
91 // Axes that the device does not have
92 setupAxis(ABS_MT_PRESSURE, /*valid=*/false, /*min*/ 0, /*max=*/255, /*resolution=*/0);
93 setupAxis(ABS_MT_ORIENTATION, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0);
94 setupAxis(ABS_MT_DISTANCE, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0);
95 setupAxis(ABS_MT_TOUCH_MAJOR, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0);
96 setupAxis(ABS_MT_TOUCH_MINOR, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0);
97 setupAxis(ABS_MT_WIDTH_MAJOR, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0);
98 setupAxis(ABS_MT_WIDTH_MINOR, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0);
99 setupAxis(ABS_MT_TOOL_TYPE, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0);
100
101 // reset current slot at the beginning
102 EXPECT_CALL(mMockEventHub, getAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, _))
103 .WillRepeatedly([](int32_t, int32_t, int32_t* outValue) {
104 *outValue = 0;
105 return OK;
106 });
107
108 // mark all slots not in use
109 mockSlotValues({});
110
111 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
112 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
113 /*isActive=*/true, "local:0", NO_PORT,
114 ViewportType::INTERNAL);
Arpit Singh4b4a4572023-11-24 18:19:56 +0000115 mMapper = createInputMapper<MultiTouchInputMapper>(*mDeviceContext,
116 mFakePolicy->getReaderConfiguration());
117 }
118
119 // Mocks position and tracking Ids for the provided slots. Remaining slots will be marked
120 // unused.
121 void mockSlotValues(
122 const std::unordered_map<int32_t /*slotIndex*/,
123 std::pair<Point /*position*/, int32_t /*trackingId*/>>&
124 slotValues) {
125 EXPECT_CALL(mMockEventHub, getMtSlotValues(EVENTHUB_ID, _, SLOT_COUNT))
126 .WillRepeatedly([=](int32_t, int32_t axis,
127 size_t slotCount) -> base::Result<std::vector<int32_t>> {
128 // tracking Id for the unused slots must set to be < 0
129 std::vector<int32_t> outMtSlotValues(slotCount + 1, -1);
130 outMtSlotValues[0] = axis;
131 switch (axis) {
132 case ABS_MT_POSITION_X:
133 for (const auto& [slotIndex, valuePair] : slotValues) {
134 outMtSlotValues[slotIndex] = valuePair.first.x;
135 }
136 return outMtSlotValues;
137 case ABS_MT_POSITION_Y:
138 for (const auto& [slotIndex, valuePair] : slotValues) {
139 outMtSlotValues[slotIndex] = valuePair.first.y;
140 }
141 return outMtSlotValues;
142 case ABS_MT_TRACKING_ID:
143 for (const auto& [slotIndex, valuePair] : slotValues) {
144 outMtSlotValues[slotIndex] = valuePair.second;
145 }
146 return outMtSlotValues;
147 default:
148 return base::ResultError("Axis not supported", NAME_NOT_FOUND);
149 }
150 });
151 }
152
153 std::list<NotifyArgs> processPosition(int32_t x, int32_t y) {
154 std::list<NotifyArgs> args;
155 args += process(EV_ABS, ABS_MT_POSITION_X, x);
156 args += process(EV_ABS, ABS_MT_POSITION_Y, y);
157 return args;
158 }
159
160 std::list<NotifyArgs> processId(int32_t id) { return process(EV_ABS, ABS_MT_TRACKING_ID, id); }
161
162 std::list<NotifyArgs> processKey(int32_t code, int32_t value) {
163 return process(EV_KEY, code, value);
164 }
165
166 std::list<NotifyArgs> processSlot(int32_t slot) { return process(EV_ABS, ABS_MT_SLOT, slot); }
167
168 std::list<NotifyArgs> processSync() { return process(EV_SYN, SYN_REPORT, 0); }
169};
170
171// This test simulates a multi-finger gesture with unexpected reset in between. This might happen
172// due to buffer overflow and device with report a SYN_DROPPED. In this case we expect mapper to be
173// reset, MT slot state to be re-populated and the gesture should be cancelled and restarted.
174TEST_F(MultiTouchInputMapperUnitTest, MultiFingerGestureWithUnexpectedReset) {
175 std::list<NotifyArgs> args;
176
177 // Two fingers down at once.
178 constexpr int32_t FIRST_TRACKING_ID = 1, SECOND_TRACKING_ID = 2;
179 int32_t x1 = 100, y1 = 125, x2 = 200, y2 = 225;
180 processKey(BTN_TOUCH, 1);
181 args += processPosition(x1, y1);
182 args += processId(FIRST_TRACKING_ID);
183 args += processSlot(1);
184 args += processPosition(x2, y2);
185 args += processId(SECOND_TRACKING_ID);
186 ASSERT_THAT(args, IsEmpty());
187
188 args = processSync();
189 ASSERT_THAT(args,
190 ElementsAre(VariantWith<NotifyMotionArgs>(
191 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)),
192 VariantWith<NotifyMotionArgs>(
193 WithMotionAction(ACTION_POINTER_1_DOWN))));
194
195 // Move.
196 x1 += 10;
197 y1 += 15;
198 x2 += 5;
199 y2 -= 10;
200 args = processSlot(0);
201 args += processPosition(x1, y1);
202 args += processSlot(1);
203 args += processPosition(x2, y2);
204 ASSERT_THAT(args, IsEmpty());
205
206 args = processSync();
207 ASSERT_THAT(args,
208 ElementsAre(VariantWith<NotifyMotionArgs>(
209 WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
210 const auto pointerCoordsBeforeReset = std::get<NotifyMotionArgs>(args.back()).pointerCoords;
211
212 // On buffer overflow mapper will be reset and MT slots data will be repopulated
213 EXPECT_CALL(mMockEventHub, getAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, _))
214 .WillRepeatedly([=](int32_t, int32_t, int32_t* outValue) {
215 *outValue = 1;
216 return OK;
217 });
218
219 mockSlotValues(
220 {{1, {Point{x1, y1}, FIRST_TRACKING_ID}}, {2, {Point{x2, y2}, SECOND_TRACKING_ID}}});
221
222 setScanCodeState(KeyState::DOWN, {BTN_TOUCH});
223
224 args = mMapper->reset(systemTime(SYSTEM_TIME_MONOTONIC));
225 ASSERT_THAT(args,
226 ElementsAre(VariantWith<NotifyMotionArgs>(
227 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL))));
228
229 // SYN_REPORT should restart the gesture again
230 args = processSync();
231 ASSERT_THAT(args,
232 ElementsAre(VariantWith<NotifyMotionArgs>(
233 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)),
234 VariantWith<NotifyMotionArgs>(
235 WithMotionAction(ACTION_POINTER_1_DOWN))));
236 ASSERT_EQ(std::get<NotifyMotionArgs>(args.back()).pointerCoords, pointerCoordsBeforeReset);
237
238 // Move.
239 x1 += 10;
240 y1 += 15;
241 x2 += 5;
242 y2 -= 10;
243 args = processSlot(0);
244 args += processPosition(x1, y1);
245 args += processSlot(1);
246 args += processPosition(x2, y2);
247 ASSERT_THAT(args, IsEmpty());
248
249 args = processSync();
250 ASSERT_THAT(args,
251 ElementsAre(VariantWith<NotifyMotionArgs>(
252 WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
253
254 // First finger up.
255 args = processSlot(0);
256 args += processId(-1);
257 ASSERT_THAT(args, IsEmpty());
258
259 args = processSync();
260 ASSERT_THAT(args,
261 ElementsAre(VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_POINTER_0_UP))));
262
263 // Second finger up.
264 processKey(BTN_TOUCH, 0);
265 args = processSlot(1);
266 args += processId(-1);
267 ASSERT_THAT(args, IsEmpty());
268
269 args = processSync();
270 ASSERT_THAT(args,
271 ElementsAre(
272 VariantWith<NotifyMotionArgs>(WithMotionAction(AMOTION_EVENT_ACTION_UP))));
273}
274
275} // namespace android