1 /*
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
30 namespace android {
31
32 using testing::_;
33 using testing::IsEmpty;
34 using testing::Return;
35 using testing::SetArgPointee;
36 using testing::VariantWith;
37
38 static constexpr ui::LogicalDisplayId DISPLAY_ID = ui::LogicalDisplayId::DEFAULT;
39 static constexpr int32_t DISPLAY_WIDTH = 480;
40 static constexpr int32_t DISPLAY_HEIGHT = 800;
41 static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
42 static constexpr int32_t SLOT_COUNT = 5;
43
44 static constexpr int32_t ACTION_POINTER_0_UP =
45 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
46 static 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 */
52 class MultiTouchInputMapperUnitTest : public InputMapperUnitTest {
53 protected:
SetUp()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(Return(0));
104
105 // mark all slots not in use
106 mockSlotValues({});
107
108 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
109 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
110 /*isActive=*/true, "local:0", NO_PORT,
111 ViewportType::INTERNAL);
112 mMapper = createInputMapper<MultiTouchInputMapper>(*mDeviceContext,
113 mFakePolicy->getReaderConfiguration());
114 }
115
116 // Mocks position and tracking Ids for the provided slots. Remaining slots will be marked
117 // unused.
mockSlotValues(const std::unordered_map<int32_t,std::pair<Point,int32_t>> & slotValues)118 void mockSlotValues(
119 const std::unordered_map<int32_t /*slotIndex*/,
120 std::pair<Point /*position*/, int32_t /*trackingId*/>>&
121 slotValues) {
122 EXPECT_CALL(mMockEventHub, getMtSlotValues(EVENTHUB_ID, _, SLOT_COUNT))
123 .WillRepeatedly([=](int32_t, int32_t axis,
124 size_t slotCount) -> base::Result<std::vector<int32_t>> {
125 // tracking Id for the unused slots must set to be < 0
126 std::vector<int32_t> outMtSlotValues(slotCount + 1, -1);
127 outMtSlotValues[0] = axis;
128 switch (axis) {
129 case ABS_MT_POSITION_X:
130 for (const auto& [slotIndex, valuePair] : slotValues) {
131 outMtSlotValues[slotIndex] = valuePair.first.x;
132 }
133 return outMtSlotValues;
134 case ABS_MT_POSITION_Y:
135 for (const auto& [slotIndex, valuePair] : slotValues) {
136 outMtSlotValues[slotIndex] = valuePair.first.y;
137 }
138 return outMtSlotValues;
139 case ABS_MT_TRACKING_ID:
140 for (const auto& [slotIndex, valuePair] : slotValues) {
141 outMtSlotValues[slotIndex] = valuePair.second;
142 }
143 return outMtSlotValues;
144 default:
145 return base::ResultError("Axis not supported", NAME_NOT_FOUND);
146 }
147 });
148 }
149
processPosition(int32_t x,int32_t y)150 std::list<NotifyArgs> processPosition(int32_t x, int32_t y) {
151 std::list<NotifyArgs> args;
152 args += process(EV_ABS, ABS_MT_POSITION_X, x);
153 args += process(EV_ABS, ABS_MT_POSITION_Y, y);
154 return args;
155 }
156
processId(int32_t id)157 std::list<NotifyArgs> processId(int32_t id) { return process(EV_ABS, ABS_MT_TRACKING_ID, id); }
158
processKey(int32_t code,int32_t value)159 std::list<NotifyArgs> processKey(int32_t code, int32_t value) {
160 return process(EV_KEY, code, value);
161 }
162
processSlot(int32_t slot)163 std::list<NotifyArgs> processSlot(int32_t slot) { return process(EV_ABS, ABS_MT_SLOT, slot); }
164
processSync()165 std::list<NotifyArgs> processSync() { return process(EV_SYN, SYN_REPORT, 0); }
166 };
167
168 // This test simulates a multi-finger gesture with unexpected reset in between. This might happen
169 // due to buffer overflow and device with report a SYN_DROPPED. In this case we expect mapper to be
170 // reset, MT slot state to be re-populated and the gesture should be cancelled and restarted.
TEST_F(MultiTouchInputMapperUnitTest,MultiFingerGestureWithUnexpectedReset)171 TEST_F(MultiTouchInputMapperUnitTest, MultiFingerGestureWithUnexpectedReset) {
172 std::list<NotifyArgs> args;
173
174 // Two fingers down at once.
175 constexpr int32_t FIRST_TRACKING_ID = 1, SECOND_TRACKING_ID = 2;
176 int32_t x1 = 100, y1 = 125, x2 = 200, y2 = 225;
177 processKey(BTN_TOUCH, 1);
178 args += processPosition(x1, y1);
179 args += processId(FIRST_TRACKING_ID);
180 args += processSlot(1);
181 args += processPosition(x2, y2);
182 args += processId(SECOND_TRACKING_ID);
183 ASSERT_THAT(args, IsEmpty());
184
185 args = processSync();
186 ASSERT_THAT(args,
187 ElementsAre(VariantWith<NotifyMotionArgs>(
188 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)),
189 VariantWith<NotifyMotionArgs>(
190 WithMotionAction(ACTION_POINTER_1_DOWN))));
191
192 // Move.
193 x1 += 10;
194 y1 += 15;
195 x2 += 5;
196 y2 -= 10;
197 args = processSlot(0);
198 args += processPosition(x1, y1);
199 args += processSlot(1);
200 args += processPosition(x2, y2);
201 ASSERT_THAT(args, IsEmpty());
202
203 args = processSync();
204 ASSERT_THAT(args,
205 ElementsAre(VariantWith<NotifyMotionArgs>(
206 WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
207 const auto pointerCoordsBeforeReset = std::get<NotifyMotionArgs>(args.back()).pointerCoords;
208
209 // On buffer overflow mapper will be reset and MT slots data will be repopulated
210 EXPECT_CALL(mMockEventHub, getAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT))
211 .WillRepeatedly(Return(1));
212
213 mockSlotValues(
214 {{1, {Point{x1, y1}, FIRST_TRACKING_ID}}, {2, {Point{x2, y2}, SECOND_TRACKING_ID}}});
215
216 setScanCodeState(KeyState::DOWN, {BTN_TOUCH});
217
218 args = mMapper->reset(systemTime(SYSTEM_TIME_MONOTONIC));
219 ASSERT_THAT(args,
220 ElementsAre(VariantWith<NotifyMotionArgs>(
221 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL))));
222
223 // SYN_REPORT should restart the gesture again
224 args = processSync();
225 ASSERT_THAT(args,
226 ElementsAre(VariantWith<NotifyMotionArgs>(
227 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)),
228 VariantWith<NotifyMotionArgs>(
229 WithMotionAction(ACTION_POINTER_1_DOWN))));
230 ASSERT_EQ(std::get<NotifyMotionArgs>(args.back()).pointerCoords, pointerCoordsBeforeReset);
231
232 // Move.
233 x1 += 10;
234 y1 += 15;
235 x2 += 5;
236 y2 -= 10;
237 args = processSlot(0);
238 args += processPosition(x1, y1);
239 args += processSlot(1);
240 args += processPosition(x2, y2);
241 ASSERT_THAT(args, IsEmpty());
242
243 args = processSync();
244 ASSERT_THAT(args,
245 ElementsAre(VariantWith<NotifyMotionArgs>(
246 WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
247
248 // First finger up.
249 args = processSlot(0);
250 args += processId(-1);
251 ASSERT_THAT(args, IsEmpty());
252
253 args = processSync();
254 ASSERT_THAT(args,
255 ElementsAre(VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_POINTER_0_UP))));
256
257 // Second finger up.
258 processKey(BTN_TOUCH, 0);
259 args = processSlot(1);
260 args += processId(-1);
261 ASSERT_THAT(args, IsEmpty());
262
263 args = processSync();
264 ASSERT_THAT(args,
265 ElementsAre(
266 VariantWith<NotifyMotionArgs>(WithMotionAction(AMOTION_EVENT_ACTION_UP))));
267 }
268
269 } // namespace android
270