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