blob: af1f377287e7a5bf96f6c150ca349b2a41cde154 [file] [log] [blame]
Harry Cuttse6512e12022-11-28 18:44:01 +00001/*
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 Cutts1b5bde42023-12-18 16:08:29 +000022#include <utils/Timers.h>
Harry Cuttse6512e12022-11-28 18:44:01 +000023
Harry Cuttsd256daa2024-01-05 11:47:36 +000024#include "NotifyArgs.h"
25
Harry Cuttse6512e12022-11-28 18:44:01 +000026namespace android {
27
Harry Cuttsd256daa2024-01-05 11:47:36 +000028using testing::_;
Prabir Pradhan31d05c42024-07-24 21:19:08 +000029using testing::NiceMock;
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070030using testing::Return;
Prabir Pradhan31d05c42024-07-24 21:19:08 +000031using testing::ReturnRef;
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070032
Harry Cutts7ecbb992023-12-18 14:45:09 +000033void InputMapperUnitTest::SetUpWithBus(int bus) {
Byoungho Jungee6268f2023-10-30 17:27:26 +090034 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070035
Byoungho Jungee6268f2023-10-30 17:27:26 +090036 EXPECT_CALL(mMockInputReaderContext, getPolicy()).WillRepeatedly(Return(mFakePolicy.get()));
37
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070038 EXPECT_CALL(mMockInputReaderContext, getEventHub()).WillRepeatedly(Return(&mMockEventHub));
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070039
Harry Cutts1b5bde42023-12-18 16:08:29 +000040 mIdentifier.name = "device";
41 mIdentifier.location = "USB1";
Harry Cutts7ecbb992023-12-18 14:45:09 +000042 mIdentifier.bus = bus;
Harry Cutts1b5bde42023-12-18 16:08:29 +000043 EXPECT_CALL(mMockEventHub, getDeviceIdentifier(EVENTHUB_ID))
44 .WillRepeatedly(Return(mIdentifier));
Harry Cutts7ecbb992023-12-18 14:45:09 +000045 EXPECT_CALL(mMockEventHub, getConfiguration(EVENTHUB_ID)).WillRepeatedly([&](int32_t) {
46 return mPropertyMap;
47 });
Harry Cutts1b5bde42023-12-18 16:08:29 +000048
Prabir Pradhan31d05c42024-07-24 21:19:08 +000049 mDevice = std::make_unique<NiceMock<MockInputDevice>>(&mMockInputReaderContext, DEVICE_ID,
50 /*generation=*/2, mIdentifier);
51 ON_CALL((*mDevice), getConfiguration).WillByDefault(ReturnRef(mPropertyMap));
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070052 mDeviceContext = std::make_unique<InputDeviceContext>(*mDevice, EVENTHUB_ID);
53}
54
55void InputMapperUnitTest::setupAxis(int axis, bool valid, int32_t min, int32_t max,
56 int32_t resolution) {
Harry Cutts207674d2024-06-06 18:53:41 +000057 EXPECT_CALL(mMockEventHub, getAbsoluteAxisInfo(EVENTHUB_ID, axis))
58 .WillRepeatedly(Return(valid ? std::optional<RawAbsoluteAxisInfo>{{
Prabir Pradhan09163b72024-07-23 17:23:06 +000059 .valid = true,
Harry Cutts207674d2024-06-06 18:53:41 +000060 .minValue = min,
61 .maxValue = max,
62 .flat = 0,
63 .fuzz = 0,
64 .resolution = resolution,
65 }}
66 : std::nullopt));
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070067}
68
69void InputMapperUnitTest::expectScanCodes(bool present, std::set<int> scanCodes) {
70 for (const auto& scanCode : scanCodes) {
71 EXPECT_CALL(mMockEventHub, hasScanCode(EVENTHUB_ID, scanCode))
72 .WillRepeatedly(testing::Return(present));
73 }
74}
75
76void InputMapperUnitTest::setScanCodeState(KeyState state, std::set<int> scanCodes) {
77 for (const auto& scanCode : scanCodes) {
78 EXPECT_CALL(mMockEventHub, getScanCodeState(EVENTHUB_ID, scanCode))
79 .WillRepeatedly(testing::Return(static_cast<int>(state)));
80 }
81}
82
83void InputMapperUnitTest::setKeyCodeState(KeyState state, std::set<int> keyCodes) {
84 for (const auto& keyCode : keyCodes) {
85 EXPECT_CALL(mMockEventHub, getKeyCodeState(EVENTHUB_ID, keyCode))
86 .WillRepeatedly(testing::Return(static_cast<int>(state)));
87 }
88}
89
Harry Cuttsaa931df2024-07-15 14:54:36 +000090void InputMapperUnitTest::setSwitchState(int32_t state, std::set<int32_t> switchCodes) {
91 for (const auto& switchCode : switchCodes) {
92 EXPECT_CALL(mMockEventHub, getSwitchState(EVENTHUB_ID, switchCode))
93 .WillRepeatedly(testing::Return(static_cast<int>(state)));
94 }
95}
96
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070097std::list<NotifyArgs> InputMapperUnitTest::process(int32_t type, int32_t code, int32_t value) {
Arpit Singh82e413e2023-10-10 19:30:58 +000098 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
99 return process(when, type, code, value);
100}
101
102std::list<NotifyArgs> InputMapperUnitTest::process(nsecs_t when, int32_t type, int32_t code,
103 int32_t value) {
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -0700104 RawEvent event;
Arpit Singh82e413e2023-10-10 19:30:58 +0000105 event.when = when;
106 event.readTime = when;
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -0700107 event.deviceId = mMapper->getDeviceContext().getEventHubId();
108 event.type = type;
109 event.code = code;
110 event.value = value;
Harry Cuttsa32a1192024-06-04 15:10:31 +0000111 return mMapper->process(event);
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -0700112}
113
Harry Cuttse6512e12022-11-28 18:44:01 +0000114const char* InputMapperTest::DEVICE_NAME = "device";
115const char* InputMapperTest::DEVICE_LOCATION = "USB1";
116const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
117 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
118
119void InputMapperTest::SetUp(ftl::Flags<InputDeviceClass> classes, int bus) {
120 mFakeEventHub = std::make_unique<FakeEventHub>();
121 mFakePolicy = sp<FakeInputReaderPolicy>::make();
122 mFakeListener = std::make_unique<TestInputListener>();
123 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy, *mFakeListener);
124 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes, bus);
125 // Consume the device reset notification generated when adding a new device.
126 mFakeListener->assertNotifyDeviceResetWasCalled();
127}
128
129void InputMapperTest::SetUp() {
130 SetUp(DEVICE_CLASSES);
131}
132
133void InputMapperTest::TearDown() {
134 mFakeListener.reset();
135 mFakePolicy.clear();
136}
137
138void InputMapperTest::addConfigurationProperty(const char* key, const char* value) {
139 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
140}
141
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000142std::list<NotifyArgs> InputMapperTest::configureDevice(ConfigurationChanges changes) {
143 using namespace ftl::flag_operators;
144 if (!changes.any() ||
145 (changes.any(InputReaderConfiguration::Change::DISPLAY_INFO |
146 InputReaderConfiguration::Change::POINTER_CAPTURE |
147 InputReaderConfiguration::Change::DEVICE_TYPE))) {
Harry Cuttse6512e12022-11-28 18:44:01 +0000148 mReader->requestRefreshConfiguration(changes);
149 mReader->loopOnce();
150 }
151 std::list<NotifyArgs> out =
152 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
153 // Loop the reader to flush the input listener queue.
154 for (const NotifyArgs& args : out) {
155 mFakeListener->notify(args);
156 }
157 mReader->loopOnce();
158 return out;
159}
160
161std::shared_ptr<InputDevice> InputMapperTest::newDevice(int32_t deviceId, const std::string& name,
162 const std::string& location,
163 int32_t eventHubId,
164 ftl::Flags<InputDeviceClass> classes,
165 int bus) {
166 InputDeviceIdentifier identifier;
167 identifier.name = name;
168 identifier.location = location;
169 identifier.bus = bus;
170 std::shared_ptr<InputDevice> device =
171 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
172 identifier);
173 mReader->pushNextDevice(device);
174 mFakeEventHub->addDevice(eventHubId, name, classes, bus);
175 mReader->loopOnce();
176 return device;
177}
178
Linnan Li13bf76a2024-05-05 19:18:02 +0800179void InputMapperTest::setDisplayInfoAndReconfigure(ui::LogicalDisplayId displayId, int32_t width,
180 int32_t height, ui::Rotation orientation,
Harry Cuttse6512e12022-11-28 18:44:01 +0000181 const std::string& uniqueId,
182 std::optional<uint8_t> physicalPort,
183 ViewportType viewportType) {
184 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /* isActive= */ true,
185 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000186 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Harry Cuttse6512e12022-11-28 18:44:01 +0000187}
188
189void InputMapperTest::clearViewports() {
190 mFakePolicy->clearViewports();
191}
192
193std::list<NotifyArgs> InputMapperTest::process(InputMapper& mapper, nsecs_t when, nsecs_t readTime,
194 int32_t type, int32_t code, int32_t value) {
195 RawEvent event;
196 event.when = when;
197 event.readTime = readTime;
198 event.deviceId = mapper.getDeviceContext().getEventHubId();
199 event.type = type;
200 event.code = code;
201 event.value = value;
Harry Cuttsa32a1192024-06-04 15:10:31 +0000202 std::list<NotifyArgs> processArgList = mapper.process(event);
Harry Cuttse6512e12022-11-28 18:44:01 +0000203 for (const NotifyArgs& args : processArgList) {
204 mFakeListener->notify(args);
205 }
206 // Loop the reader to flush the input listener queue.
207 mReader->loopOnce();
208 return processArgList;
209}
210
211void InputMapperTest::resetMapper(InputMapper& mapper, nsecs_t when) {
212 const auto resetArgs = mapper.reset(when);
213 for (const auto args : resetArgs) {
214 mFakeListener->notify(args);
215 }
216 // Loop the reader to flush the input listener queue.
217 mReader->loopOnce();
218}
219
220std::list<NotifyArgs> InputMapperTest::handleTimeout(InputMapper& mapper, nsecs_t when) {
221 std::list<NotifyArgs> generatedArgs = mapper.timeoutExpired(when);
222 for (const NotifyArgs& args : generatedArgs) {
223 mFakeListener->notify(args);
224 }
225 // Loop the reader to flush the input listener queue.
226 mReader->loopOnce();
227 return generatedArgs;
228}
229
Harry Cutts7ecbb992023-12-18 14:45:09 +0000230void assertMotionRange(const InputDeviceInfo& info, int32_t axis, uint32_t source, float min,
231 float max, float flat, float fuzz) {
Harry Cuttse6512e12022-11-28 18:44:01 +0000232 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
233 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
234 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
235 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
236 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
237 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
238 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
239 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
240}
241
Harry Cutts7ecbb992023-12-18 14:45:09 +0000242void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure, float size,
243 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
244 float orientation, float distance, float scaledAxisEpsilon) {
Harry Cuttse6512e12022-11-28 18:44:01 +0000245 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
246 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
247 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
248 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
249 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), scaledAxisEpsilon);
250 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), scaledAxisEpsilon);
251 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), scaledAxisEpsilon);
252 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), scaledAxisEpsilon);
253 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
254 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
255}
256
Harry Cuttse6512e12022-11-28 18:44:01 +0000257} // namespace android