Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 1 | /* |
| 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 Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 22 | #include <com_android_input_flags.h> |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 23 | #include <thread> |
| 24 | #include "FakePointerController.h" |
| 25 | #include "InputMapperTest.h" |
| 26 | #include "InterfaceMocks.h" |
Prabir Pradhan | e3b28dd | 2023-10-06 04:19:29 +0000 | [diff] [blame] | 27 | #include "TestEventMatchers.h" |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 28 | |
| 29 | #define TAG "TouchpadInputMapper_test" |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | using testing::Return; |
| 34 | using testing::VariantWith; |
| 35 | constexpr auto ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN; |
| 36 | constexpr auto ACTION_UP = AMOTION_EVENT_ACTION_UP; |
| 37 | constexpr auto BUTTON_PRESS = AMOTION_EVENT_ACTION_BUTTON_PRESS; |
| 38 | constexpr auto BUTTON_RELEASE = AMOTION_EVENT_ACTION_BUTTON_RELEASE; |
| 39 | constexpr auto HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE; |
Harry Cutts | 379ea42 | 2023-12-21 15:31:47 +0000 | [diff] [blame] | 40 | constexpr auto HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 41 | constexpr auto HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 42 | constexpr int32_t DISPLAY_ID = 0; |
| 43 | constexpr int32_t DISPLAY_WIDTH = 480; |
| 44 | constexpr int32_t DISPLAY_HEIGHT = 800; |
| 45 | constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified |
| 46 | |
| 47 | namespace input_flags = com::android::input::flags; |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Unit tests for TouchpadInputMapper. |
| 51 | */ |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 52 | class TouchpadInputMapperTestBase : public InputMapperUnitTest { |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 53 | protected: |
| 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 Singh | 4b4a457 | 2023-11-24 18:19:56 +0000 | [diff] [blame] | 106 | 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 Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 109 | |
| 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 Singh | 4b4a457 | 2023-11-24 18:19:56 +0000 | [diff] [blame] | 115 | 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 Cutts | 1b5bde4 | 2023-12-18 16:08:29 +0000 | [diff] [blame] | 119 | createDevice(); |
Prabir Pradhan | c8377b0 | 2024-02-26 21:20:30 +0000 | [diff] [blame] | 120 | mMapper = createInputMapper<TouchpadInputMapper>(*mDeviceContext, mReaderConfiguration, |
| 121 | isPointerChoreographerEnabled()); |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 122 | } |
Prabir Pradhan | c8377b0 | 2024-02-26 21:20:30 +0000 | [diff] [blame] | 123 | |
| 124 | virtual bool isPointerChoreographerEnabled() { return false; } |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 127 | class TouchpadInputMapperTest : public TouchpadInputMapperTestBase { |
| 128 | protected: |
| 129 | void SetUp() override { |
| 130 | input_flags::enable_pointer_choreographer(false); |
| 131 | TouchpadInputMapperTestBase::SetUp(); |
| 132 | } |
| 133 | }; |
| 134 | |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 135 | /** |
| 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 | */ |
| 141 | TEST_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 Cutts | 379ea42 | 2023-12-21 15:31:47 +0000 | [diff] [blame] | 166 | ElementsAre(VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_ENTER)), |
| 167 | VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_MOVE)), |
| 168 | VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_EXIT)), |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 169 | VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_DOWN)), |
| 170 | VariantWith<NotifyMotionArgs>(WithMotionAction(BUTTON_PRESS)), |
| 171 | VariantWith<NotifyMotionArgs>(WithMotionAction(BUTTON_RELEASE)), |
Harry Cutts | 48e7a40 | 2023-07-07 15:22:40 +0000 | [diff] [blame] | 172 | VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_UP)), |
Harry Cutts | 379ea42 | 2023-12-21 15:31:47 +0000 | [diff] [blame] | 173 | VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_ENTER)))); |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 174 | |
| 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 Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 186 | class TouchpadInputMapperTestWithChoreographer : public TouchpadInputMapperTestBase { |
| 187 | protected: |
Prabir Pradhan | c8377b0 | 2024-02-26 21:20:30 +0000 | [diff] [blame] | 188 | void SetUp() override { TouchpadInputMapperTestBase::SetUp(); } |
| 189 | |
| 190 | bool isPointerChoreographerEnabled() override { return true; } |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 191 | }; |
| 192 | |
Prabir Pradhan | cc7268a | 2023-11-16 18:54:13 +0000 | [diff] [blame] | 193 | // TODO(b/311416205): De-duplicate the test cases after the refactoring is complete and the flagging |
| 194 | // logic can be removed. |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 195 | /** |
| 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 | */ |
| 201 | TEST_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 Cutts | 379ea42 | 2023-12-21 15:31:47 +0000 | [diff] [blame] | 234 | ElementsAre(VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_ENTER)), |
| 235 | VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_MOVE)), |
| 236 | VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_EXIT)), |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 237 | VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_DOWN)), |
| 238 | VariantWith<NotifyMotionArgs>(WithMotionAction(BUTTON_PRESS)), |
| 239 | VariantWith<NotifyMotionArgs>(WithMotionAction(BUTTON_RELEASE)), |
| 240 | VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_UP)), |
Harry Cutts | 379ea42 | 2023-12-21 15:31:47 +0000 | [diff] [blame] | 241 | VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_ENTER)))); |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 242 | |
| 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 Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 254 | } // namespace android |