blob: a92dce5dd0edbcbabcb3f240a605ea6c112c4468 [file] [log] [blame]
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -07001/*
2 * Copyright 2023 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 "TouchpadInputMapper.h"
18
19#include <android-base/logging.h>
20#include <gtest/gtest.h>
21
Byoungho Jungee6268f2023-10-30 17:27:26 +090022#include <com_android_input_flags.h>
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070023#include <thread>
24#include "FakePointerController.h"
25#include "InputMapperTest.h"
26#include "InterfaceMocks.h"
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000027#include "TestEventMatchers.h"
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070028
29#define TAG "TouchpadInputMapper_test"
30
31namespace android {
32
33using testing::Return;
34using testing::VariantWith;
35constexpr auto ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN;
36constexpr auto ACTION_UP = AMOTION_EVENT_ACTION_UP;
37constexpr auto BUTTON_PRESS = AMOTION_EVENT_ACTION_BUTTON_PRESS;
38constexpr auto BUTTON_RELEASE = AMOTION_EVENT_ACTION_BUTTON_RELEASE;
39constexpr auto HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE;
Harry Cutts379ea422023-12-21 15:31:47 +000040constexpr auto HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER;
41constexpr auto HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT;
Byoungho Jungee6268f2023-10-30 17:27:26 +090042constexpr int32_t DISPLAY_ID = 0;
43constexpr int32_t DISPLAY_WIDTH = 480;
44constexpr int32_t DISPLAY_HEIGHT = 800;
45constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
46
47namespace input_flags = com::android::input::flags;
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070048
49/**
50 * Unit tests for TouchpadInputMapper.
51 */
Byoungho Jungee6268f2023-10-30 17:27:26 +090052class TouchpadInputMapperTestBase : public InputMapperUnitTest {
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070053protected:
54 void SetUp() override {
55 InputMapperUnitTest::SetUp();
56
57 // Present scan codes: BTN_TOUCH and BTN_TOOL_FINGER
58 expectScanCodes(/*present=*/true,
59 {BTN_LEFT, BTN_RIGHT, BTN_TOOL_FINGER, BTN_TOOL_QUINTTAP, BTN_TOUCH,
60 BTN_TOOL_DOUBLETAP, BTN_TOOL_TRIPLETAP, BTN_TOOL_QUADTAP});
61 // Missing scan codes that the mapper checks for.
62 expectScanCodes(/*present=*/false,
63 {BTN_TOOL_PEN, BTN_TOOL_RUBBER, BTN_TOOL_BRUSH, BTN_TOOL_PENCIL,
64 BTN_TOOL_AIRBRUSH});
65
66 // Current scan code state - all keys are UP by default
67 setScanCodeState(KeyState::UP, {BTN_TOUCH, BTN_STYLUS,
68 BTN_STYLUS2, BTN_0,
69 BTN_TOOL_FINGER, BTN_TOOL_PEN,
70 BTN_TOOL_RUBBER, BTN_TOOL_BRUSH,
71 BTN_TOOL_PENCIL, BTN_TOOL_AIRBRUSH,
72 BTN_TOOL_MOUSE, BTN_TOOL_LENS,
73 BTN_TOOL_DOUBLETAP, BTN_TOOL_TRIPLETAP,
74 BTN_TOOL_QUADTAP, BTN_TOOL_QUINTTAP,
75 BTN_LEFT, BTN_RIGHT,
76 BTN_MIDDLE, BTN_BACK,
77 BTN_SIDE, BTN_FORWARD,
78 BTN_EXTRA, BTN_TASK});
79
80 setKeyCodeState(KeyState::UP,
81 {AKEYCODE_STYLUS_BUTTON_PRIMARY, AKEYCODE_STYLUS_BUTTON_SECONDARY});
82
83 // Key mappings
84 EXPECT_CALL(mMockEventHub,
85 mapKey(EVENTHUB_ID, BTN_LEFT, /*usageCode=*/0, /*metaState=*/0, testing::_,
86 testing::_, testing::_))
87 .WillRepeatedly(Return(NAME_NOT_FOUND));
88
89 // Input properties - only INPUT_PROP_BUTTONPAD
90 EXPECT_CALL(mMockEventHub, hasInputProperty(EVENTHUB_ID, INPUT_PROP_BUTTONPAD))
91 .WillRepeatedly(Return(true));
92 EXPECT_CALL(mMockEventHub, hasInputProperty(EVENTHUB_ID, INPUT_PROP_SEMI_MT))
93 .WillRepeatedly(Return(false));
94
95 // Axes that the device has
96 setupAxis(ABS_MT_SLOT, /*valid=*/true, /*min=*/0, /*max=*/4, /*resolution=*/0);
97 setupAxis(ABS_MT_POSITION_X, /*valid=*/true, /*min=*/0, /*max=*/2000, /*resolution=*/24);
98 setupAxis(ABS_MT_POSITION_Y, /*valid=*/true, /*min=*/0, /*max=*/1000, /*resolution=*/24);
99 setupAxis(ABS_MT_PRESSURE, /*valid=*/true, /*min*/ 0, /*max=*/255, /*resolution=*/0);
100 // Axes that the device does not have
101 setupAxis(ABS_MT_ORIENTATION, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0);
102 setupAxis(ABS_MT_TOUCH_MAJOR, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0);
103 setupAxis(ABS_MT_TOUCH_MINOR, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0);
104 setupAxis(ABS_MT_WIDTH_MAJOR, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0);
105 setupAxis(ABS_MT_WIDTH_MINOR, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0);
Arpit Singh4b4a4572023-11-24 18:19:56 +0000106 setupAxis(ABS_MT_TRACKING_ID, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0);
107 setupAxis(ABS_MT_DISTANCE, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0);
108 setupAxis(ABS_MT_TOOL_TYPE, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0);
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -0700109
110 EXPECT_CALL(mMockEventHub, getAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, testing::_))
111 .WillRepeatedly([](int32_t eventHubId, int32_t, int32_t* outValue) {
112 *outValue = 0;
113 return OK;
114 });
Arpit Singh4b4a4572023-11-24 18:19:56 +0000115 EXPECT_CALL(mMockEventHub, getMtSlotValues(EVENTHUB_ID, testing::_, testing::_))
116 .WillRepeatedly([]() -> base::Result<std::vector<int32_t>> {
117 return base::ResultError("Axis not supported", NAME_NOT_FOUND);
118 });
Harry Cutts1b5bde42023-12-18 16:08:29 +0000119 createDevice();
Prabir Pradhanc8377b02024-02-26 21:20:30 +0000120 mMapper = createInputMapper<TouchpadInputMapper>(*mDeviceContext, mReaderConfiguration,
121 isPointerChoreographerEnabled());
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -0700122 }
Prabir Pradhanc8377b02024-02-26 21:20:30 +0000123
124 virtual bool isPointerChoreographerEnabled() { return false; }
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -0700125};
126
Byoungho Jungee6268f2023-10-30 17:27:26 +0900127class TouchpadInputMapperTest : public TouchpadInputMapperTestBase {
128protected:
129 void SetUp() override {
130 input_flags::enable_pointer_choreographer(false);
131 TouchpadInputMapperTestBase::SetUp();
132 }
133};
134
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -0700135/**
136 * Start moving the finger and then click the left touchpad button. Check whether HOVER_EXIT is
137 * generated when hovering stops. Currently, it is not.
138 * In the current implementation, HOVER_MOVE and ACTION_DOWN events are not sent out right away,
139 * but only after the button is released.
140 */
141TEST_F(TouchpadInputMapperTest, HoverAndLeftButtonPress) {
142 std::list<NotifyArgs> args;
143
144 args += process(EV_ABS, ABS_MT_TRACKING_ID, 1);
145 args += process(EV_KEY, BTN_TOUCH, 1);
146 setScanCodeState(KeyState::DOWN, {BTN_TOOL_FINGER});
147 args += process(EV_KEY, BTN_TOOL_FINGER, 1);
148 args += process(EV_ABS, ABS_MT_POSITION_X, 50);
149 args += process(EV_ABS, ABS_MT_POSITION_Y, 50);
150 args += process(EV_ABS, ABS_MT_PRESSURE, 1);
151 args += process(EV_SYN, SYN_REPORT, 0);
152 ASSERT_THAT(args, testing::IsEmpty());
153
154 // Without this sleep, the test fails.
155 // TODO(b/284133337): Figure out whether this can be removed
156 std::this_thread::sleep_for(std::chrono::milliseconds(20));
157
158 args += process(EV_KEY, BTN_LEFT, 1);
159 setScanCodeState(KeyState::DOWN, {BTN_LEFT});
160 args += process(EV_SYN, SYN_REPORT, 0);
161
162 args += process(EV_KEY, BTN_LEFT, 0);
163 setScanCodeState(KeyState::UP, {BTN_LEFT});
164 args += process(EV_SYN, SYN_REPORT, 0);
165 ASSERT_THAT(args,
Harry Cutts379ea422023-12-21 15:31:47 +0000166 ElementsAre(VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_ENTER)),
167 VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_MOVE)),
168 VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_EXIT)),
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -0700169 VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_DOWN)),
170 VariantWith<NotifyMotionArgs>(WithMotionAction(BUTTON_PRESS)),
171 VariantWith<NotifyMotionArgs>(WithMotionAction(BUTTON_RELEASE)),
Harry Cutts48e7a402023-07-07 15:22:40 +0000172 VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_UP)),
Harry Cutts379ea422023-12-21 15:31:47 +0000173 VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_ENTER))));
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -0700174
175 // Liftoff
176 args.clear();
177 args += process(EV_ABS, ABS_MT_PRESSURE, 0);
178 args += process(EV_ABS, ABS_MT_TRACKING_ID, -1);
179 args += process(EV_KEY, BTN_TOUCH, 0);
180 setScanCodeState(KeyState::UP, {BTN_TOOL_FINGER});
181 args += process(EV_KEY, BTN_TOOL_FINGER, 0);
182 args += process(EV_SYN, SYN_REPORT, 0);
183 ASSERT_THAT(args, testing::IsEmpty());
184}
185
Byoungho Jungee6268f2023-10-30 17:27:26 +0900186class TouchpadInputMapperTestWithChoreographer : public TouchpadInputMapperTestBase {
187protected:
Prabir Pradhanc8377b02024-02-26 21:20:30 +0000188 void SetUp() override { TouchpadInputMapperTestBase::SetUp(); }
189
190 bool isPointerChoreographerEnabled() override { return true; }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900191};
192
Prabir Pradhancc7268a2023-11-16 18:54:13 +0000193// TODO(b/311416205): De-duplicate the test cases after the refactoring is complete and the flagging
194// logic can be removed.
Byoungho Jungee6268f2023-10-30 17:27:26 +0900195/**
196 * Start moving the finger and then click the left touchpad button. Check whether HOVER_EXIT is
197 * generated when hovering stops. Currently, it is not.
198 * In the current implementation, HOVER_MOVE and ACTION_DOWN events are not sent out right away,
199 * but only after the button is released.
200 */
201TEST_F(TouchpadInputMapperTestWithChoreographer, HoverAndLeftButtonPress) {
202 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
203 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
204 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
205
206 std::list<NotifyArgs> args;
207
208 args += mMapper->reconfigure(systemTime(SYSTEM_TIME_MONOTONIC), mReaderConfiguration,
209 InputReaderConfiguration::Change::DISPLAY_INFO);
210 ASSERT_THAT(args, testing::IsEmpty());
211
212 args += process(EV_ABS, ABS_MT_TRACKING_ID, 1);
213 args += process(EV_KEY, BTN_TOUCH, 1);
214 setScanCodeState(KeyState::DOWN, {BTN_TOOL_FINGER});
215 args += process(EV_KEY, BTN_TOOL_FINGER, 1);
216 args += process(EV_ABS, ABS_MT_POSITION_X, 50);
217 args += process(EV_ABS, ABS_MT_POSITION_Y, 50);
218 args += process(EV_ABS, ABS_MT_PRESSURE, 1);
219 args += process(EV_SYN, SYN_REPORT, 0);
220 ASSERT_THAT(args, testing::IsEmpty());
221
222 // Without this sleep, the test fails.
223 // TODO(b/284133337): Figure out whether this can be removed
224 std::this_thread::sleep_for(std::chrono::milliseconds(20));
225
226 args += process(EV_KEY, BTN_LEFT, 1);
227 setScanCodeState(KeyState::DOWN, {BTN_LEFT});
228 args += process(EV_SYN, SYN_REPORT, 0);
229
230 args += process(EV_KEY, BTN_LEFT, 0);
231 setScanCodeState(KeyState::UP, {BTN_LEFT});
232 args += process(EV_SYN, SYN_REPORT, 0);
233 ASSERT_THAT(args,
Harry Cutts379ea422023-12-21 15:31:47 +0000234 ElementsAre(VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_ENTER)),
235 VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_MOVE)),
236 VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_EXIT)),
Byoungho Jungee6268f2023-10-30 17:27:26 +0900237 VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_DOWN)),
238 VariantWith<NotifyMotionArgs>(WithMotionAction(BUTTON_PRESS)),
239 VariantWith<NotifyMotionArgs>(WithMotionAction(BUTTON_RELEASE)),
240 VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_UP)),
Harry Cutts379ea422023-12-21 15:31:47 +0000241 VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_ENTER))));
Byoungho Jungee6268f2023-10-30 17:27:26 +0900242
243 // Liftoff
244 args.clear();
245 args += process(EV_ABS, ABS_MT_PRESSURE, 0);
246 args += process(EV_ABS, ABS_MT_TRACKING_ID, -1);
247 args += process(EV_KEY, BTN_TOUCH, 0);
248 setScanCodeState(KeyState::UP, {BTN_TOOL_FINGER});
249 args += process(EV_KEY, BTN_TOOL_FINGER, 0);
250 args += process(EV_SYN, SYN_REPORT, 0);
251 ASSERT_THAT(args, testing::IsEmpty());
252}
253
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -0700254} // namespace android