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; |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 40 | constexpr int32_t DISPLAY_ID = 0; |
| 41 | constexpr int32_t DISPLAY_WIDTH = 480; |
| 42 | constexpr int32_t DISPLAY_HEIGHT = 800; |
| 43 | constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified |
| 44 | |
| 45 | namespace input_flags = com::android::input::flags; |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Unit tests for TouchpadInputMapper. |
| 49 | */ |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 50 | class TouchpadInputMapperTestBase : public InputMapperUnitTest { |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 51 | protected: |
| 52 | void SetUp() override { |
| 53 | InputMapperUnitTest::SetUp(); |
| 54 | |
| 55 | // Present scan codes: BTN_TOUCH and BTN_TOOL_FINGER |
| 56 | expectScanCodes(/*present=*/true, |
| 57 | {BTN_LEFT, BTN_RIGHT, BTN_TOOL_FINGER, BTN_TOOL_QUINTTAP, BTN_TOUCH, |
| 58 | BTN_TOOL_DOUBLETAP, BTN_TOOL_TRIPLETAP, BTN_TOOL_QUADTAP}); |
| 59 | // Missing scan codes that the mapper checks for. |
| 60 | expectScanCodes(/*present=*/false, |
| 61 | {BTN_TOOL_PEN, BTN_TOOL_RUBBER, BTN_TOOL_BRUSH, BTN_TOOL_PENCIL, |
| 62 | BTN_TOOL_AIRBRUSH}); |
| 63 | |
| 64 | // Current scan code state - all keys are UP by default |
| 65 | setScanCodeState(KeyState::UP, {BTN_TOUCH, BTN_STYLUS, |
| 66 | BTN_STYLUS2, BTN_0, |
| 67 | BTN_TOOL_FINGER, BTN_TOOL_PEN, |
| 68 | BTN_TOOL_RUBBER, BTN_TOOL_BRUSH, |
| 69 | BTN_TOOL_PENCIL, BTN_TOOL_AIRBRUSH, |
| 70 | BTN_TOOL_MOUSE, BTN_TOOL_LENS, |
| 71 | BTN_TOOL_DOUBLETAP, BTN_TOOL_TRIPLETAP, |
| 72 | BTN_TOOL_QUADTAP, BTN_TOOL_QUINTTAP, |
| 73 | BTN_LEFT, BTN_RIGHT, |
| 74 | BTN_MIDDLE, BTN_BACK, |
| 75 | BTN_SIDE, BTN_FORWARD, |
| 76 | BTN_EXTRA, BTN_TASK}); |
| 77 | |
| 78 | setKeyCodeState(KeyState::UP, |
| 79 | {AKEYCODE_STYLUS_BUTTON_PRIMARY, AKEYCODE_STYLUS_BUTTON_SECONDARY}); |
| 80 | |
| 81 | // Key mappings |
| 82 | EXPECT_CALL(mMockEventHub, |
| 83 | mapKey(EVENTHUB_ID, BTN_LEFT, /*usageCode=*/0, /*metaState=*/0, testing::_, |
| 84 | testing::_, testing::_)) |
| 85 | .WillRepeatedly(Return(NAME_NOT_FOUND)); |
| 86 | |
| 87 | // Input properties - only INPUT_PROP_BUTTONPAD |
| 88 | EXPECT_CALL(mMockEventHub, hasInputProperty(EVENTHUB_ID, INPUT_PROP_BUTTONPAD)) |
| 89 | .WillRepeatedly(Return(true)); |
| 90 | EXPECT_CALL(mMockEventHub, hasInputProperty(EVENTHUB_ID, INPUT_PROP_SEMI_MT)) |
| 91 | .WillRepeatedly(Return(false)); |
| 92 | |
| 93 | // Axes that the device has |
| 94 | setupAxis(ABS_MT_SLOT, /*valid=*/true, /*min=*/0, /*max=*/4, /*resolution=*/0); |
| 95 | setupAxis(ABS_MT_POSITION_X, /*valid=*/true, /*min=*/0, /*max=*/2000, /*resolution=*/24); |
| 96 | setupAxis(ABS_MT_POSITION_Y, /*valid=*/true, /*min=*/0, /*max=*/1000, /*resolution=*/24); |
| 97 | setupAxis(ABS_MT_PRESSURE, /*valid=*/true, /*min*/ 0, /*max=*/255, /*resolution=*/0); |
| 98 | // Axes that the device does not have |
| 99 | setupAxis(ABS_MT_ORIENTATION, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0); |
| 100 | setupAxis(ABS_MT_TOUCH_MAJOR, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0); |
| 101 | setupAxis(ABS_MT_TOUCH_MINOR, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0); |
| 102 | setupAxis(ABS_MT_WIDTH_MAJOR, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0); |
| 103 | setupAxis(ABS_MT_WIDTH_MINOR, /*valid=*/false, /*min=*/0, /*max=*/0, /*resolution=*/0); |
| 104 | |
| 105 | EXPECT_CALL(mMockEventHub, getAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, testing::_)) |
| 106 | .WillRepeatedly([](int32_t eventHubId, int32_t, int32_t* outValue) { |
| 107 | *outValue = 0; |
| 108 | return OK; |
| 109 | }); |
Harry Cutts | 1b5bde4 | 2023-12-18 16:08:29 +0000 | [diff] [blame^] | 110 | createDevice(); |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 111 | mMapper = createInputMapper<TouchpadInputMapper>(*mDeviceContext, mReaderConfiguration); |
| 112 | } |
| 113 | }; |
| 114 | |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 115 | class TouchpadInputMapperTest : public TouchpadInputMapperTestBase { |
| 116 | protected: |
| 117 | void SetUp() override { |
| 118 | input_flags::enable_pointer_choreographer(false); |
| 119 | TouchpadInputMapperTestBase::SetUp(); |
| 120 | } |
| 121 | }; |
| 122 | |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 123 | /** |
| 124 | * Start moving the finger and then click the left touchpad button. Check whether HOVER_EXIT is |
| 125 | * generated when hovering stops. Currently, it is not. |
| 126 | * In the current implementation, HOVER_MOVE and ACTION_DOWN events are not sent out right away, |
| 127 | * but only after the button is released. |
| 128 | */ |
| 129 | TEST_F(TouchpadInputMapperTest, HoverAndLeftButtonPress) { |
| 130 | std::list<NotifyArgs> args; |
| 131 | |
| 132 | args += process(EV_ABS, ABS_MT_TRACKING_ID, 1); |
| 133 | args += process(EV_KEY, BTN_TOUCH, 1); |
| 134 | setScanCodeState(KeyState::DOWN, {BTN_TOOL_FINGER}); |
| 135 | args += process(EV_KEY, BTN_TOOL_FINGER, 1); |
| 136 | args += process(EV_ABS, ABS_MT_POSITION_X, 50); |
| 137 | args += process(EV_ABS, ABS_MT_POSITION_Y, 50); |
| 138 | args += process(EV_ABS, ABS_MT_PRESSURE, 1); |
| 139 | args += process(EV_SYN, SYN_REPORT, 0); |
| 140 | ASSERT_THAT(args, testing::IsEmpty()); |
| 141 | |
| 142 | // Without this sleep, the test fails. |
| 143 | // TODO(b/284133337): Figure out whether this can be removed |
| 144 | std::this_thread::sleep_for(std::chrono::milliseconds(20)); |
| 145 | |
| 146 | args += process(EV_KEY, BTN_LEFT, 1); |
| 147 | setScanCodeState(KeyState::DOWN, {BTN_LEFT}); |
| 148 | args += process(EV_SYN, SYN_REPORT, 0); |
| 149 | |
| 150 | args += process(EV_KEY, BTN_LEFT, 0); |
| 151 | setScanCodeState(KeyState::UP, {BTN_LEFT}); |
| 152 | args += process(EV_SYN, SYN_REPORT, 0); |
| 153 | ASSERT_THAT(args, |
| 154 | ElementsAre(VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_MOVE)), |
| 155 | VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_DOWN)), |
| 156 | VariantWith<NotifyMotionArgs>(WithMotionAction(BUTTON_PRESS)), |
| 157 | VariantWith<NotifyMotionArgs>(WithMotionAction(BUTTON_RELEASE)), |
Harry Cutts | 48e7a40 | 2023-07-07 15:22:40 +0000 | [diff] [blame] | 158 | VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_UP)), |
| 159 | VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_MOVE)))); |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 160 | |
| 161 | // Liftoff |
| 162 | args.clear(); |
| 163 | args += process(EV_ABS, ABS_MT_PRESSURE, 0); |
| 164 | args += process(EV_ABS, ABS_MT_TRACKING_ID, -1); |
| 165 | args += process(EV_KEY, BTN_TOUCH, 0); |
| 166 | setScanCodeState(KeyState::UP, {BTN_TOOL_FINGER}); |
| 167 | args += process(EV_KEY, BTN_TOOL_FINGER, 0); |
| 168 | args += process(EV_SYN, SYN_REPORT, 0); |
| 169 | ASSERT_THAT(args, testing::IsEmpty()); |
| 170 | } |
| 171 | |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 172 | class TouchpadInputMapperTestWithChoreographer : public TouchpadInputMapperTestBase { |
| 173 | protected: |
| 174 | void SetUp() override { |
| 175 | input_flags::enable_pointer_choreographer(true); |
| 176 | TouchpadInputMapperTestBase::SetUp(); |
| 177 | } |
| 178 | }; |
| 179 | |
Prabir Pradhan | cc7268a | 2023-11-16 18:54:13 +0000 | [diff] [blame] | 180 | // TODO(b/311416205): De-duplicate the test cases after the refactoring is complete and the flagging |
| 181 | // logic can be removed. |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 182 | /** |
| 183 | * Start moving the finger and then click the left touchpad button. Check whether HOVER_EXIT is |
| 184 | * generated when hovering stops. Currently, it is not. |
| 185 | * In the current implementation, HOVER_MOVE and ACTION_DOWN events are not sent out right away, |
| 186 | * but only after the button is released. |
| 187 | */ |
| 188 | TEST_F(TouchpadInputMapperTestWithChoreographer, HoverAndLeftButtonPress) { |
| 189 | mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID); |
| 190 | mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0, |
| 191 | /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL); |
| 192 | |
| 193 | std::list<NotifyArgs> args; |
| 194 | |
| 195 | args += mMapper->reconfigure(systemTime(SYSTEM_TIME_MONOTONIC), mReaderConfiguration, |
| 196 | InputReaderConfiguration::Change::DISPLAY_INFO); |
| 197 | ASSERT_THAT(args, testing::IsEmpty()); |
| 198 | |
| 199 | args += process(EV_ABS, ABS_MT_TRACKING_ID, 1); |
| 200 | args += process(EV_KEY, BTN_TOUCH, 1); |
| 201 | setScanCodeState(KeyState::DOWN, {BTN_TOOL_FINGER}); |
| 202 | args += process(EV_KEY, BTN_TOOL_FINGER, 1); |
| 203 | args += process(EV_ABS, ABS_MT_POSITION_X, 50); |
| 204 | args += process(EV_ABS, ABS_MT_POSITION_Y, 50); |
| 205 | args += process(EV_ABS, ABS_MT_PRESSURE, 1); |
| 206 | args += process(EV_SYN, SYN_REPORT, 0); |
| 207 | ASSERT_THAT(args, testing::IsEmpty()); |
| 208 | |
| 209 | // Without this sleep, the test fails. |
| 210 | // TODO(b/284133337): Figure out whether this can be removed |
| 211 | std::this_thread::sleep_for(std::chrono::milliseconds(20)); |
| 212 | |
| 213 | args += process(EV_KEY, BTN_LEFT, 1); |
| 214 | setScanCodeState(KeyState::DOWN, {BTN_LEFT}); |
| 215 | args += process(EV_SYN, SYN_REPORT, 0); |
| 216 | |
| 217 | args += process(EV_KEY, BTN_LEFT, 0); |
| 218 | setScanCodeState(KeyState::UP, {BTN_LEFT}); |
| 219 | args += process(EV_SYN, SYN_REPORT, 0); |
| 220 | ASSERT_THAT(args, |
| 221 | ElementsAre(VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_MOVE)), |
| 222 | VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_DOWN)), |
| 223 | VariantWith<NotifyMotionArgs>(WithMotionAction(BUTTON_PRESS)), |
| 224 | VariantWith<NotifyMotionArgs>(WithMotionAction(BUTTON_RELEASE)), |
| 225 | VariantWith<NotifyMotionArgs>(WithMotionAction(ACTION_UP)), |
| 226 | VariantWith<NotifyMotionArgs>(WithMotionAction(HOVER_MOVE)))); |
| 227 | |
| 228 | // Liftoff |
| 229 | args.clear(); |
| 230 | args += process(EV_ABS, ABS_MT_PRESSURE, 0); |
| 231 | args += process(EV_ABS, ABS_MT_TRACKING_ID, -1); |
| 232 | args += process(EV_KEY, BTN_TOUCH, 0); |
| 233 | setScanCodeState(KeyState::UP, {BTN_TOOL_FINGER}); |
| 234 | args += process(EV_KEY, BTN_TOOL_FINGER, 0); |
| 235 | args += process(EV_SYN, SYN_REPORT, 0); |
| 236 | ASSERT_THAT(args, testing::IsEmpty()); |
| 237 | } |
| 238 | |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 239 | } // namespace android |