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