Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 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 "InputMapperTest.h" |
| 18 | |
| 19 | #include <InputReaderBase.h> |
| 20 | #include <gtest/gtest.h> |
| 21 | #include <ui/Rotation.h> |
Harry Cutts | 1b5bde4 | 2023-12-18 16:08:29 +0000 | [diff] [blame] | 22 | #include <utils/Timers.h> |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 23 | |
Harry Cutts | d256daa | 2024-01-05 11:47:36 +0000 | [diff] [blame] | 24 | #include "NotifyArgs.h" |
| 25 | |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 26 | namespace android { |
| 27 | |
Harry Cutts | d256daa | 2024-01-05 11:47:36 +0000 | [diff] [blame] | 28 | using testing::_; |
Prabir Pradhan | 31d05c4 | 2024-07-24 21:19:08 +0000 | [diff] [blame] | 29 | using testing::NiceMock; |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 30 | using testing::Return; |
Prabir Pradhan | 31d05c4 | 2024-07-24 21:19:08 +0000 | [diff] [blame] | 31 | using testing::ReturnRef; |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 32 | |
Harry Cutts | 7ecbb99 | 2023-12-18 14:45:09 +0000 | [diff] [blame] | 33 | void InputMapperUnitTest::SetUpWithBus(int bus) { |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 34 | mFakePolicy = sp<FakeInputReaderPolicy>::make(); |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 35 | |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 36 | EXPECT_CALL(mMockInputReaderContext, getPolicy()).WillRepeatedly(Return(mFakePolicy.get())); |
| 37 | |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 38 | EXPECT_CALL(mMockInputReaderContext, getEventHub()).WillRepeatedly(Return(&mMockEventHub)); |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 39 | |
Harry Cutts | 1b5bde4 | 2023-12-18 16:08:29 +0000 | [diff] [blame] | 40 | mIdentifier.name = "device"; |
| 41 | mIdentifier.location = "USB1"; |
Harry Cutts | 7ecbb99 | 2023-12-18 14:45:09 +0000 | [diff] [blame] | 42 | mIdentifier.bus = bus; |
Harry Cutts | 1b5bde4 | 2023-12-18 16:08:29 +0000 | [diff] [blame] | 43 | EXPECT_CALL(mMockEventHub, getDeviceIdentifier(EVENTHUB_ID)) |
| 44 | .WillRepeatedly(Return(mIdentifier)); |
Harry Cutts | 7ecbb99 | 2023-12-18 14:45:09 +0000 | [diff] [blame] | 45 | EXPECT_CALL(mMockEventHub, getConfiguration(EVENTHUB_ID)).WillRepeatedly([&](int32_t) { |
| 46 | return mPropertyMap; |
| 47 | }); |
Harry Cutts | 1b5bde4 | 2023-12-18 16:08:29 +0000 | [diff] [blame] | 48 | |
Prabir Pradhan | 31d05c4 | 2024-07-24 21:19:08 +0000 | [diff] [blame] | 49 | mDevice = std::make_unique<NiceMock<MockInputDevice>>(&mMockInputReaderContext, DEVICE_ID, |
| 50 | /*generation=*/2, mIdentifier); |
| 51 | ON_CALL((*mDevice), getConfiguration).WillByDefault(ReturnRef(mPropertyMap)); |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 52 | mDeviceContext = std::make_unique<InputDeviceContext>(*mDevice, EVENTHUB_ID); |
| 53 | } |
| 54 | |
| 55 | void InputMapperUnitTest::setupAxis(int axis, bool valid, int32_t min, int32_t max, |
| 56 | int32_t resolution) { |
Harry Cutts | 207674d | 2024-06-06 18:53:41 +0000 | [diff] [blame] | 57 | EXPECT_CALL(mMockEventHub, getAbsoluteAxisInfo(EVENTHUB_ID, axis)) |
| 58 | .WillRepeatedly(Return(valid ? std::optional<RawAbsoluteAxisInfo>{{ |
Harry Cutts | 207674d | 2024-06-06 18:53:41 +0000 | [diff] [blame] | 59 | .minValue = min, |
| 60 | .maxValue = max, |
| 61 | .flat = 0, |
| 62 | .fuzz = 0, |
| 63 | .resolution = resolution, |
| 64 | }} |
| 65 | : std::nullopt)); |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void InputMapperUnitTest::expectScanCodes(bool present, std::set<int> scanCodes) { |
| 69 | for (const auto& scanCode : scanCodes) { |
| 70 | EXPECT_CALL(mMockEventHub, hasScanCode(EVENTHUB_ID, scanCode)) |
| 71 | .WillRepeatedly(testing::Return(present)); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void InputMapperUnitTest::setScanCodeState(KeyState state, std::set<int> scanCodes) { |
| 76 | for (const auto& scanCode : scanCodes) { |
| 77 | EXPECT_CALL(mMockEventHub, getScanCodeState(EVENTHUB_ID, scanCode)) |
| 78 | .WillRepeatedly(testing::Return(static_cast<int>(state))); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void InputMapperUnitTest::setKeyCodeState(KeyState state, std::set<int> keyCodes) { |
| 83 | for (const auto& keyCode : keyCodes) { |
| 84 | EXPECT_CALL(mMockEventHub, getKeyCodeState(EVENTHUB_ID, keyCode)) |
| 85 | .WillRepeatedly(testing::Return(static_cast<int>(state))); |
| 86 | } |
| 87 | } |
| 88 | |
Harry Cutts | aa931df | 2024-07-15 14:54:36 +0000 | [diff] [blame] | 89 | void InputMapperUnitTest::setSwitchState(int32_t state, std::set<int32_t> switchCodes) { |
| 90 | for (const auto& switchCode : switchCodes) { |
| 91 | EXPECT_CALL(mMockEventHub, getSwitchState(EVENTHUB_ID, switchCode)) |
| 92 | .WillRepeatedly(testing::Return(static_cast<int>(state))); |
| 93 | } |
| 94 | } |
| 95 | |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 96 | std::list<NotifyArgs> InputMapperUnitTest::process(int32_t type, int32_t code, int32_t value) { |
Arpit Singh | 82e413e | 2023-10-10 19:30:58 +0000 | [diff] [blame] | 97 | nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
| 98 | return process(when, type, code, value); |
| 99 | } |
| 100 | |
| 101 | std::list<NotifyArgs> InputMapperUnitTest::process(nsecs_t when, int32_t type, int32_t code, |
| 102 | int32_t value) { |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 103 | RawEvent event; |
Arpit Singh | 82e413e | 2023-10-10 19:30:58 +0000 | [diff] [blame] | 104 | event.when = when; |
| 105 | event.readTime = when; |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 106 | event.deviceId = mMapper->getDeviceContext().getEventHubId(); |
| 107 | event.type = type; |
| 108 | event.code = code; |
| 109 | event.value = value; |
Harry Cutts | a32a119 | 2024-06-04 15:10:31 +0000 | [diff] [blame] | 110 | return mMapper->process(event); |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 113 | const char* InputMapperTest::DEVICE_NAME = "device"; |
| 114 | const char* InputMapperTest::DEVICE_LOCATION = "USB1"; |
| 115 | const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES = |
| 116 | ftl::Flags<InputDeviceClass>(0); // not needed for current tests |
| 117 | |
| 118 | void InputMapperTest::SetUp(ftl::Flags<InputDeviceClass> classes, int bus) { |
| 119 | mFakeEventHub = std::make_unique<FakeEventHub>(); |
| 120 | mFakePolicy = sp<FakeInputReaderPolicy>::make(); |
| 121 | mFakeListener = std::make_unique<TestInputListener>(); |
| 122 | mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy, *mFakeListener); |
| 123 | mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes, bus); |
| 124 | // Consume the device reset notification generated when adding a new device. |
| 125 | mFakeListener->assertNotifyDeviceResetWasCalled(); |
| 126 | } |
| 127 | |
| 128 | void InputMapperTest::SetUp() { |
| 129 | SetUp(DEVICE_CLASSES); |
| 130 | } |
| 131 | |
| 132 | void InputMapperTest::TearDown() { |
| 133 | mFakeListener.reset(); |
| 134 | mFakePolicy.clear(); |
| 135 | } |
| 136 | |
| 137 | void InputMapperTest::addConfigurationProperty(const char* key, const char* value) { |
| 138 | mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value); |
| 139 | } |
| 140 | |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 141 | std::list<NotifyArgs> InputMapperTest::configureDevice(ConfigurationChanges changes) { |
| 142 | using namespace ftl::flag_operators; |
| 143 | if (!changes.any() || |
| 144 | (changes.any(InputReaderConfiguration::Change::DISPLAY_INFO | |
| 145 | InputReaderConfiguration::Change::POINTER_CAPTURE | |
| 146 | InputReaderConfiguration::Change::DEVICE_TYPE))) { |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 147 | mReader->requestRefreshConfiguration(changes); |
| 148 | mReader->loopOnce(); |
| 149 | } |
| 150 | std::list<NotifyArgs> out = |
| 151 | mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes); |
| 152 | // Loop the reader to flush the input listener queue. |
| 153 | for (const NotifyArgs& args : out) { |
| 154 | mFakeListener->notify(args); |
| 155 | } |
| 156 | mReader->loopOnce(); |
| 157 | return out; |
| 158 | } |
| 159 | |
| 160 | std::shared_ptr<InputDevice> InputMapperTest::newDevice(int32_t deviceId, const std::string& name, |
| 161 | const std::string& location, |
| 162 | int32_t eventHubId, |
| 163 | ftl::Flags<InputDeviceClass> classes, |
| 164 | int bus) { |
| 165 | InputDeviceIdentifier identifier; |
| 166 | identifier.name = name; |
| 167 | identifier.location = location; |
| 168 | identifier.bus = bus; |
| 169 | std::shared_ptr<InputDevice> device = |
| 170 | std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION, |
| 171 | identifier); |
| 172 | mReader->pushNextDevice(device); |
| 173 | mFakeEventHub->addDevice(eventHubId, name, classes, bus); |
| 174 | mReader->loopOnce(); |
| 175 | return device; |
| 176 | } |
| 177 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 178 | void InputMapperTest::setDisplayInfoAndReconfigure(ui::LogicalDisplayId displayId, int32_t width, |
| 179 | int32_t height, ui::Rotation orientation, |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 180 | const std::string& uniqueId, |
| 181 | std::optional<uint8_t> physicalPort, |
| 182 | ViewportType viewportType) { |
| 183 | mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /* isActive= */ true, |
| 184 | uniqueId, physicalPort, viewportType); |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 185 | configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO); |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void InputMapperTest::clearViewports() { |
| 189 | mFakePolicy->clearViewports(); |
| 190 | } |
| 191 | |
| 192 | std::list<NotifyArgs> InputMapperTest::process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, |
| 193 | int32_t type, int32_t code, int32_t value) { |
| 194 | RawEvent event; |
| 195 | event.when = when; |
| 196 | event.readTime = readTime; |
| 197 | event.deviceId = mapper.getDeviceContext().getEventHubId(); |
| 198 | event.type = type; |
| 199 | event.code = code; |
| 200 | event.value = value; |
Harry Cutts | a32a119 | 2024-06-04 15:10:31 +0000 | [diff] [blame] | 201 | std::list<NotifyArgs> processArgList = mapper.process(event); |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 202 | for (const NotifyArgs& args : processArgList) { |
| 203 | mFakeListener->notify(args); |
| 204 | } |
| 205 | // Loop the reader to flush the input listener queue. |
| 206 | mReader->loopOnce(); |
| 207 | return processArgList; |
| 208 | } |
| 209 | |
| 210 | void InputMapperTest::resetMapper(InputMapper& mapper, nsecs_t when) { |
| 211 | const auto resetArgs = mapper.reset(when); |
| 212 | for (const auto args : resetArgs) { |
| 213 | mFakeListener->notify(args); |
| 214 | } |
| 215 | // Loop the reader to flush the input listener queue. |
| 216 | mReader->loopOnce(); |
| 217 | } |
| 218 | |
| 219 | std::list<NotifyArgs> InputMapperTest::handleTimeout(InputMapper& mapper, nsecs_t when) { |
| 220 | std::list<NotifyArgs> generatedArgs = mapper.timeoutExpired(when); |
| 221 | for (const NotifyArgs& args : generatedArgs) { |
| 222 | mFakeListener->notify(args); |
| 223 | } |
| 224 | // Loop the reader to flush the input listener queue. |
| 225 | mReader->loopOnce(); |
| 226 | return generatedArgs; |
| 227 | } |
| 228 | |
Harry Cutts | 7ecbb99 | 2023-12-18 14:45:09 +0000 | [diff] [blame] | 229 | void assertMotionRange(const InputDeviceInfo& info, int32_t axis, uint32_t source, float min, |
| 230 | float max, float flat, float fuzz) { |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 231 | const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source); |
| 232 | ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source; |
| 233 | ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source; |
| 234 | ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source; |
| 235 | ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source; |
| 236 | ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source; |
| 237 | ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source; |
| 238 | ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source; |
| 239 | } |
| 240 | |
Harry Cutts | 7ecbb99 | 2023-12-18 14:45:09 +0000 | [diff] [blame] | 241 | void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure, float size, |
| 242 | float touchMajor, float touchMinor, float toolMajor, float toolMinor, |
| 243 | float orientation, float distance, float scaledAxisEpsilon) { |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 244 | ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon); |
| 245 | ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon); |
| 246 | ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON); |
| 247 | ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON); |
| 248 | ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), scaledAxisEpsilon); |
| 249 | ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), scaledAxisEpsilon); |
| 250 | ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), scaledAxisEpsilon); |
| 251 | ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), scaledAxisEpsilon); |
| 252 | ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON); |
| 253 | ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON); |
| 254 | } |
| 255 | |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 256 | } // namespace android |