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