blob: 3cd7c1b0f2088db9c1ca8bbb792393d1a06cd9bc [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
25const char* InputMapperTest::DEVICE_NAME = "device";
26const char* InputMapperTest::DEVICE_LOCATION = "USB1";
27const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
28 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
29
30void InputMapperTest::SetUp(ftl::Flags<InputDeviceClass> classes, int bus) {
31 mFakeEventHub = std::make_unique<FakeEventHub>();
32 mFakePolicy = sp<FakeInputReaderPolicy>::make();
33 mFakeListener = std::make_unique<TestInputListener>();
34 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy, *mFakeListener);
35 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes, bus);
36 // Consume the device reset notification generated when adding a new device.
37 mFakeListener->assertNotifyDeviceResetWasCalled();
38}
39
40void InputMapperTest::SetUp() {
41 SetUp(DEVICE_CLASSES);
42}
43
44void InputMapperTest::TearDown() {
45 mFakeListener.reset();
46 mFakePolicy.clear();
47}
48
49void InputMapperTest::addConfigurationProperty(const char* key, const char* value) {
50 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
51}
52
53std::list<NotifyArgs> InputMapperTest::configureDevice(uint32_t changes) {
54 if (!changes ||
55 (changes &
56 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
57 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
58 mReader->requestRefreshConfiguration(changes);
59 mReader->loopOnce();
60 }
61 std::list<NotifyArgs> out =
62 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
63 // Loop the reader to flush the input listener queue.
64 for (const NotifyArgs& args : out) {
65 mFakeListener->notify(args);
66 }
67 mReader->loopOnce();
68 return out;
69}
70
71std::shared_ptr<InputDevice> InputMapperTest::newDevice(int32_t deviceId, const std::string& name,
72 const std::string& location,
73 int32_t eventHubId,
74 ftl::Flags<InputDeviceClass> classes,
75 int bus) {
76 InputDeviceIdentifier identifier;
77 identifier.name = name;
78 identifier.location = location;
79 identifier.bus = bus;
80 std::shared_ptr<InputDevice> device =
81 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
82 identifier);
83 mReader->pushNextDevice(device);
84 mFakeEventHub->addDevice(eventHubId, name, classes, bus);
85 mReader->loopOnce();
86 return device;
87}
88
89void InputMapperTest::setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
90 ui::Rotation orientation,
91 const std::string& uniqueId,
92 std::optional<uint8_t> physicalPort,
93 ViewportType viewportType) {
94 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /* isActive= */ true,
95 uniqueId, physicalPort, viewportType);
96 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
97}
98
99void InputMapperTest::clearViewports() {
100 mFakePolicy->clearViewports();
101}
102
103std::list<NotifyArgs> InputMapperTest::process(InputMapper& mapper, nsecs_t when, nsecs_t readTime,
104 int32_t type, int32_t code, int32_t value) {
105 RawEvent event;
106 event.when = when;
107 event.readTime = readTime;
108 event.deviceId = mapper.getDeviceContext().getEventHubId();
109 event.type = type;
110 event.code = code;
111 event.value = value;
112 std::list<NotifyArgs> processArgList = mapper.process(&event);
113 for (const NotifyArgs& args : processArgList) {
114 mFakeListener->notify(args);
115 }
116 // Loop the reader to flush the input listener queue.
117 mReader->loopOnce();
118 return processArgList;
119}
120
121void InputMapperTest::resetMapper(InputMapper& mapper, nsecs_t when) {
122 const auto resetArgs = mapper.reset(when);
123 for (const auto args : resetArgs) {
124 mFakeListener->notify(args);
125 }
126 // Loop the reader to flush the input listener queue.
127 mReader->loopOnce();
128}
129
130std::list<NotifyArgs> InputMapperTest::handleTimeout(InputMapper& mapper, nsecs_t when) {
131 std::list<NotifyArgs> generatedArgs = mapper.timeoutExpired(when);
132 for (const NotifyArgs& args : generatedArgs) {
133 mFakeListener->notify(args);
134 }
135 // Loop the reader to flush the input listener queue.
136 mReader->loopOnce();
137 return generatedArgs;
138}
139
140void InputMapperTest::assertMotionRange(const InputDeviceInfo& info, int32_t axis, uint32_t source,
141 float min, float max, float flat, float fuzz) {
142 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
143 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
144 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
145 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
146 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
147 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
148 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
149 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
150}
151
152void InputMapperTest::assertPointerCoords(const PointerCoords& coords, float x, float y,
153 float pressure, float size, float touchMajor,
154 float touchMinor, float toolMajor, float toolMinor,
155 float orientation, float distance,
156 float scaledAxisEpsilon) {
157 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
158 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
159 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
160 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
161 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), scaledAxisEpsilon);
162 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), scaledAxisEpsilon);
163 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), scaledAxisEpsilon);
164 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), scaledAxisEpsilon);
165 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
166 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
167}
168
169void InputMapperTest::assertPosition(const FakePointerController& controller, float x, float y) {
170 float actualX, actualY;
171 controller.getPosition(&actualX, &actualY);
172 ASSERT_NEAR(x, actualX, 1);
173 ASSERT_NEAR(y, actualY, 1);
174}
175
176} // namespace android