blob: 5c6a1b8f6d3cbe2421160f30db0844bb2c05be0f [file] [log] [blame]
Harry Cutts6b5fbc52022-11-28 16:37:43 +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 "FakeInputReaderPolicy.h"
18
19#include <android-base/thread_annotations.h>
20#include <gtest/gtest.h>
21
22#include "TestConstants.h"
23
24namespace android {
25
26void FakeInputReaderPolicy::assertInputDevicesChanged() {
27 waitForInputDevices([](bool devicesChanged) {
28 if (!devicesChanged) {
29 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
30 }
31 });
32}
33
34void FakeInputReaderPolicy::assertInputDevicesNotChanged() {
35 waitForInputDevices([](bool devicesChanged) {
36 if (devicesChanged) {
37 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
38 }
39 });
40}
41
42void FakeInputReaderPolicy::assertStylusGestureNotified(int32_t deviceId) {
43 std::scoped_lock lock(mLock);
44 ASSERT_TRUE(mStylusGestureNotified);
45 ASSERT_EQ(deviceId, *mStylusGestureNotified);
46 mStylusGestureNotified.reset();
47}
48
49void FakeInputReaderPolicy::assertStylusGestureNotNotified() {
50 std::scoped_lock lock(mLock);
51 ASSERT_FALSE(mStylusGestureNotified);
52}
53
54void FakeInputReaderPolicy::clearViewports() {
55 mViewports.clear();
56 mConfig.setDisplayViewports(mViewports);
57}
58
59std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByUniqueId(
60 const std::string& uniqueId) const {
61 return mConfig.getDisplayViewportByUniqueId(uniqueId);
62}
63std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByType(
64 ViewportType type) const {
65 return mConfig.getDisplayViewportByType(type);
66}
67
68std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByPort(
69 uint8_t displayPort) const {
70 return mConfig.getDisplayViewportByPort(displayPort);
71}
72
73void FakeInputReaderPolicy::addDisplayViewport(DisplayViewport viewport) {
74 mViewports.push_back(std::move(viewport));
75 mConfig.setDisplayViewports(mViewports);
76}
77
78void FakeInputReaderPolicy::addDisplayViewport(int32_t displayId, int32_t width, int32_t height,
79 int32_t orientation, bool isActive,
80 const std::string& uniqueId,
81 std::optional<uint8_t> physicalPort,
82 ViewportType type) {
83 const bool isRotated =
84 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
85 DisplayViewport v;
86 v.displayId = displayId;
87 v.orientation = orientation;
88 v.logicalLeft = 0;
89 v.logicalTop = 0;
90 v.logicalRight = isRotated ? height : width;
91 v.logicalBottom = isRotated ? width : height;
92 v.physicalLeft = 0;
93 v.physicalTop = 0;
94 v.physicalRight = isRotated ? height : width;
95 v.physicalBottom = isRotated ? width : height;
96 v.deviceWidth = isRotated ? height : width;
97 v.deviceHeight = isRotated ? width : height;
98 v.isActive = isActive;
99 v.uniqueId = uniqueId;
100 v.physicalPort = physicalPort;
101 v.type = type;
102
103 addDisplayViewport(v);
104}
105
106bool FakeInputReaderPolicy::updateViewport(const DisplayViewport& viewport) {
107 size_t count = mViewports.size();
108 for (size_t i = 0; i < count; i++) {
109 const DisplayViewport& currentViewport = mViewports[i];
110 if (currentViewport.displayId == viewport.displayId) {
111 mViewports[i] = viewport;
112 mConfig.setDisplayViewports(mViewports);
113 return true;
114 }
115 }
116 // no viewport found.
117 return false;
118}
119
120void FakeInputReaderPolicy::addExcludedDeviceName(const std::string& deviceName) {
121 mConfig.excludedDeviceNames.push_back(deviceName);
122}
123
124void FakeInputReaderPolicy::addInputPortAssociation(const std::string& inputPort,
125 uint8_t displayPort) {
126 mConfig.portAssociations.insert({inputPort, displayPort});
127}
128
129void FakeInputReaderPolicy::addInputUniqueIdAssociation(const std::string& inputUniqueId,
130 const std::string& displayUniqueId) {
131 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
132}
133
134void FakeInputReaderPolicy::addDisabledDevice(int32_t deviceId) {
135 mConfig.disabledDevices.insert(deviceId);
136}
137
138void FakeInputReaderPolicy::removeDisabledDevice(int32_t deviceId) {
139 mConfig.disabledDevices.erase(deviceId);
140}
141
142void FakeInputReaderPolicy::setPointerController(
143 std::shared_ptr<FakePointerController> controller) {
144 mPointerController = std::move(controller);
145}
146
147const InputReaderConfiguration* FakeInputReaderPolicy::getReaderConfiguration() const {
148 return &mConfig;
149}
150
151const std::vector<InputDeviceInfo>& FakeInputReaderPolicy::getInputDevices() const {
152 return mInputDevices;
153}
154
155TouchAffineTransformation FakeInputReaderPolicy::getTouchAffineTransformation(
156 const std::string& inputDeviceDescriptor, int32_t surfaceRotation) {
157 return transform;
158}
159
160void FakeInputReaderPolicy::setTouchAffineTransformation(const TouchAffineTransformation t) {
161 transform = t;
162}
163
164PointerCaptureRequest FakeInputReaderPolicy::setPointerCapture(bool enabled) {
165 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
166 return mConfig.pointerCaptureRequest;
167}
168
169void FakeInputReaderPolicy::setShowTouches(bool enabled) {
170 mConfig.showTouches = enabled;
171}
172
173void FakeInputReaderPolicy::setDefaultPointerDisplayId(int32_t pointerDisplayId) {
174 mConfig.defaultPointerDisplayId = pointerDisplayId;
175}
176
177void FakeInputReaderPolicy::setPointerGestureEnabled(bool enabled) {
178 mConfig.pointerGesturesEnabled = enabled;
179}
180
181float FakeInputReaderPolicy::getPointerGestureMovementSpeedRatio() {
182 return mConfig.pointerGestureMovementSpeedRatio;
183}
184
185float FakeInputReaderPolicy::getPointerGestureZoomSpeedRatio() {
186 return mConfig.pointerGestureZoomSpeedRatio;
187}
188
189void FakeInputReaderPolicy::setVelocityControlParams(const VelocityControlParameters& params) {
190 mConfig.pointerVelocityControlParameters = params;
191 mConfig.wheelVelocityControlParameters = params;
192}
193
194void FakeInputReaderPolicy::getReaderConfiguration(InputReaderConfiguration* outConfig) {
195 *outConfig = mConfig;
196}
197
198std::shared_ptr<PointerControllerInterface> FakeInputReaderPolicy::obtainPointerController(
199 int32_t /*deviceId*/) {
200 return mPointerController;
201}
202
203void FakeInputReaderPolicy::notifyInputDevicesChanged(
204 const std::vector<InputDeviceInfo>& inputDevices) {
205 std::scoped_lock<std::mutex> lock(mLock);
206 mInputDevices = inputDevices;
207 mInputDevicesChanged = true;
208 mDevicesChangedCondition.notify_all();
209}
210
211std::shared_ptr<KeyCharacterMap> FakeInputReaderPolicy::getKeyboardLayoutOverlay(
212 const InputDeviceIdentifier&) {
213 return nullptr;
214}
215
216std::string FakeInputReaderPolicy::getDeviceAlias(const InputDeviceIdentifier&) {
217 return "";
218}
219
220void FakeInputReaderPolicy::waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
221 std::unique_lock<std::mutex> lock(mLock);
222 base::ScopedLockAssertion assumeLocked(mLock);
223
224 const bool devicesChanged =
225 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
226 return mInputDevicesChanged;
227 });
228 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
229 mInputDevicesChanged = false;
230}
231
232void FakeInputReaderPolicy::notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) {
233 std::scoped_lock<std::mutex> lock(mLock);
234 mStylusGestureNotified = deviceId;
235}
236
237} // namespace android