blob: ae300066d2083cb9bba9b960195e596fb6cbbb59 [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 |
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +000057 InputReaderConfiguration::CHANGE_POINTER_CAPTURE |
58 InputReaderConfiguration::CHANGE_DEVICE_TYPE))) {
Harry Cuttse6512e12022-11-28 18:44:01 +000059 mReader->requestRefreshConfiguration(changes);
60 mReader->loopOnce();
61 }
62 std::list<NotifyArgs> out =
63 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
64 // Loop the reader to flush the input listener queue.
65 for (const NotifyArgs& args : out) {
66 mFakeListener->notify(args);
67 }
68 mReader->loopOnce();
69 return out;
70}
71
72std::shared_ptr<InputDevice> InputMapperTest::newDevice(int32_t deviceId, const std::string& name,
73 const std::string& location,
74 int32_t eventHubId,
75 ftl::Flags<InputDeviceClass> classes,
76 int bus) {
77 InputDeviceIdentifier identifier;
78 identifier.name = name;
79 identifier.location = location;
80 identifier.bus = bus;
81 std::shared_ptr<InputDevice> device =
82 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
83 identifier);
84 mReader->pushNextDevice(device);
85 mFakeEventHub->addDevice(eventHubId, name, classes, bus);
86 mReader->loopOnce();
87 return device;
88}
89
90void InputMapperTest::setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
91 ui::Rotation orientation,
92 const std::string& uniqueId,
93 std::optional<uint8_t> physicalPort,
94 ViewportType viewportType) {
95 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /* isActive= */ true,
96 uniqueId, physicalPort, viewportType);
97 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
98}
99
100void InputMapperTest::clearViewports() {
101 mFakePolicy->clearViewports();
102}
103
104std::list<NotifyArgs> InputMapperTest::process(InputMapper& mapper, nsecs_t when, nsecs_t readTime,
105 int32_t type, int32_t code, int32_t value) {
106 RawEvent event;
107 event.when = when;
108 event.readTime = readTime;
109 event.deviceId = mapper.getDeviceContext().getEventHubId();
110 event.type = type;
111 event.code = code;
112 event.value = value;
113 std::list<NotifyArgs> processArgList = mapper.process(&event);
114 for (const NotifyArgs& args : processArgList) {
115 mFakeListener->notify(args);
116 }
117 // Loop the reader to flush the input listener queue.
118 mReader->loopOnce();
119 return processArgList;
120}
121
122void InputMapperTest::resetMapper(InputMapper& mapper, nsecs_t when) {
123 const auto resetArgs = mapper.reset(when);
124 for (const auto args : resetArgs) {
125 mFakeListener->notify(args);
126 }
127 // Loop the reader to flush the input listener queue.
128 mReader->loopOnce();
129}
130
131std::list<NotifyArgs> InputMapperTest::handleTimeout(InputMapper& mapper, nsecs_t when) {
132 std::list<NotifyArgs> generatedArgs = mapper.timeoutExpired(when);
133 for (const NotifyArgs& args : generatedArgs) {
134 mFakeListener->notify(args);
135 }
136 // Loop the reader to flush the input listener queue.
137 mReader->loopOnce();
138 return generatedArgs;
139}
140
141void InputMapperTest::assertMotionRange(const InputDeviceInfo& info, int32_t axis, uint32_t source,
142 float min, float max, float flat, float fuzz) {
143 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
144 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
145 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
146 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
147 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
148 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
149 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
150 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
151}
152
153void InputMapperTest::assertPointerCoords(const PointerCoords& coords, float x, float y,
154 float pressure, float size, float touchMajor,
155 float touchMinor, float toolMajor, float toolMinor,
156 float orientation, float distance,
157 float scaledAxisEpsilon) {
158 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
159 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
160 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
161 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
162 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), scaledAxisEpsilon);
163 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), scaledAxisEpsilon);
164 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), scaledAxisEpsilon);
165 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), scaledAxisEpsilon);
166 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
167 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
168}
169
Harry Cuttse6512e12022-11-28 18:44:01 +0000170} // namespace android