blob: 4be1e8c54ae85697feab189a4e6862e33a3e6911 [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);
106
107 EXPECT_CALL(mMockEventHub, getAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, testing::_))
108 .WillRepeatedly([](int32_t eventHubId, int32_t, int32_t* outValue) {
109 *outValue = 0;
110 return OK;
111 });
Harry Cutts1b5bde42023-12-18 16:08:29 +0000112 createDevice();
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -0700113 mMapper = createInputMapper<TouchpadInputMapper>(*mDeviceContext, mReaderConfiguration);
114 }
115};
116
Byoungho Jungee6268f2023-10-30 17:27:26 +0900117class TouchpadInputMapperTest : public TouchpadInputMapperTestBase {
118protected:
119 void SetUp() override {
120 input_flags::enable_pointer_choreographer(false);
121 TouchpadInputMapperTestBase::SetUp();
122 }
123};
124
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -0700125/**
126 * Start moving the finger and then click the left touchpad button. Check whether HOVER_EXIT is
127 * generated when hovering stops. Currently, it is not.
128 * In the current implementation, HOVER_MOVE and ACTION_DOWN events are not sent out right away,
129 * but only after the button is released.
130 */
131TEST_F(TouchpadInputMapperTest, HoverAndLeftButtonPress) {
132 std::list<NotifyArgs> args;
133
134 args += process(EV_ABS, ABS_MT_TRACKING_ID, 1);
135 args += process(EV_KEY, BTN_TOUCH, 1);
136 setScanCodeState(KeyState::DOWN, {BTN_TOOL_FINGER});
137 args += process(EV_KEY, BTN_TOOL_FINGER, 1);
138 args += process(EV_ABS, ABS_MT_POSITION_X, 50);
139 args += process(EV_ABS, ABS_MT_POSITION_Y, 50);
140 args += process(EV_ABS, ABS_MT_PRESSURE, 1);
141 args += process(EV_SYN, SYN_REPORT, 0);
142 ASSERT_THAT(args, testing::IsEmpty());
143
144 // Without this sleep, the test fails.
145 // TODO(b/284133337): Figure out whether this can be removed
146 std::this_thread::sleep_for(std::chrono::milliseconds(20));
147
148 args += process(EV_KEY, BTN_LEFT, 1);
149 setScanCodeState(KeyState::DOWN, {BTN_LEFT});
150 args += process(EV_SYN, SYN_REPORT, 0);
151
152 args += process(EV_KEY, BTN_LEFT, 0);
153 setScanCodeState(KeyState::UP, {BTN_LEFT});
154 args += process(EV_SYN, SYN_REPORT, 0);
155 ASSERT_THAT(args,
Harry Cutts379ea422023-12-21 15:31:47 +0000156 ElementsAre(VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_ENTER)),
157 VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_MOVE)),
158 VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_EXIT)),
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -0700159 VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_DOWN)),
160 VariantWith<NotifyMotionArgs>(WithMotionAction(BUTTON_PRESS)),
161 VariantWith<NotifyMotionArgs>(WithMotionAction(BUTTON_RELEASE)),
Harry Cutts48e7a402023-07-07 15:22:40 +0000162 VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_UP)),
Harry Cutts379ea422023-12-21 15:31:47 +0000163 VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_ENTER))));
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -0700164
165 // Liftoff
166 args.clear();
167 args += process(EV_ABS, ABS_MT_PRESSURE, 0);
168 args += process(EV_ABS, ABS_MT_TRACKING_ID, -1);
169 args += process(EV_KEY, BTN_TOUCH, 0);
170 setScanCodeState(KeyState::UP, {BTN_TOOL_FINGER});
171 args += process(EV_KEY, BTN_TOOL_FINGER, 0);
172 args += process(EV_SYN, SYN_REPORT, 0);
173 ASSERT_THAT(args, testing::IsEmpty());
174}
175
Byoungho Jungee6268f2023-10-30 17:27:26 +0900176class TouchpadInputMapperTestWithChoreographer : public TouchpadInputMapperTestBase {
177protected:
178 void SetUp() override {
179 input_flags::enable_pointer_choreographer(true);
180 TouchpadInputMapperTestBase::SetUp();
181 }
182};
183
Prabir Pradhancc7268a2023-11-16 18:54:13 +0000184// TODO(b/311416205): De-duplicate the test cases after the refactoring is complete and the flagging
185// logic can be removed.
Byoungho Jungee6268f2023-10-30 17:27:26 +0900186/**
187 * Start moving the finger and then click the left touchpad button. Check whether HOVER_EXIT is
188 * generated when hovering stops. Currently, it is not.
189 * In the current implementation, HOVER_MOVE and ACTION_DOWN events are not sent out right away,
190 * but only after the button is released.
191 */
192TEST_F(TouchpadInputMapperTestWithChoreographer, HoverAndLeftButtonPress) {
193 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
194 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
195 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
196
197 std::list<NotifyArgs> args;
198
199 args += mMapper->reconfigure(systemTime(SYSTEM_TIME_MONOTONIC), mReaderConfiguration,
200 InputReaderConfiguration::Change::DISPLAY_INFO);
201 ASSERT_THAT(args, testing::IsEmpty());
202
203 args += process(EV_ABS, ABS_MT_TRACKING_ID, 1);
204 args += process(EV_KEY, BTN_TOUCH, 1);
205 setScanCodeState(KeyState::DOWN, {BTN_TOOL_FINGER});
206 args += process(EV_KEY, BTN_TOOL_FINGER, 1);
207 args += process(EV_ABS, ABS_MT_POSITION_X, 50);
208 args += process(EV_ABS, ABS_MT_POSITION_Y, 50);
209 args += process(EV_ABS, ABS_MT_PRESSURE, 1);
210 args += process(EV_SYN, SYN_REPORT, 0);
211 ASSERT_THAT(args, testing::IsEmpty());
212
213 // Without this sleep, the test fails.
214 // TODO(b/284133337): Figure out whether this can be removed
215 std::this_thread::sleep_for(std::chrono::milliseconds(20));
216
217 args += process(EV_KEY, BTN_LEFT, 1);
218 setScanCodeState(KeyState::DOWN, {BTN_LEFT});
219 args += process(EV_SYN, SYN_REPORT, 0);
220
221 args += process(EV_KEY, BTN_LEFT, 0);
222 setScanCodeState(KeyState::UP, {BTN_LEFT});
223 args += process(EV_SYN, SYN_REPORT, 0);
224 ASSERT_THAT(args,
Harry Cutts379ea422023-12-21 15:31:47 +0000225 ElementsAre(VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_ENTER)),
226 VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_MOVE)),
227 VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_EXIT)),
Byoungho Jungee6268f2023-10-30 17:27:26 +0900228 VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_DOWN)),
229 VariantWith<NotifyMotionArgs>(WithMotionAction(BUTTON_PRESS)),
230 VariantWith<NotifyMotionArgs>(WithMotionAction(BUTTON_RELEASE)),
231 VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_UP)),
Harry Cutts379ea422023-12-21 15:31:47 +0000232 VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_ENTER))));
Byoungho Jungee6268f2023-10-30 17:27:26 +0900233
234 // Liftoff
235 args.clear();
236 args += process(EV_ABS, ABS_MT_PRESSURE, 0);
237 args += process(EV_ABS, ABS_MT_TRACKING_ID, -1);
238 args += process(EV_KEY, BTN_TOUCH, 0);
239 setScanCodeState(KeyState::UP, {BTN_TOOL_FINGER});
240 args += process(EV_KEY, BTN_TOOL_FINGER, 0);
241 args += process(EV_SYN, SYN_REPORT, 0);
242 ASSERT_THAT(args, testing::IsEmpty());
243}
244
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -0700245} // namespace android