blob: dc7e581abda6acfadc4d96d9bf49e1331242f520 [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"
Michael Wrighta9cf4192022-12-01 23:46:39 +000023#include "ui/Rotation.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000024
25namespace android {
26
27void FakeInputReaderPolicy::assertInputDevicesChanged() {
28 waitForInputDevices([](bool devicesChanged) {
29 if (!devicesChanged) {
30 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
31 }
32 });
33}
34
35void FakeInputReaderPolicy::assertInputDevicesNotChanged() {
36 waitForInputDevices([](bool devicesChanged) {
37 if (devicesChanged) {
38 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
39 }
40 });
41}
42
43void FakeInputReaderPolicy::assertStylusGestureNotified(int32_t deviceId) {
44 std::scoped_lock lock(mLock);
45 ASSERT_TRUE(mStylusGestureNotified);
46 ASSERT_EQ(deviceId, *mStylusGestureNotified);
47 mStylusGestureNotified.reset();
48}
49
50void FakeInputReaderPolicy::assertStylusGestureNotNotified() {
51 std::scoped_lock lock(mLock);
52 ASSERT_FALSE(mStylusGestureNotified);
53}
54
55void FakeInputReaderPolicy::clearViewports() {
56 mViewports.clear();
57 mConfig.setDisplayViewports(mViewports);
58}
59
60std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByUniqueId(
61 const std::string& uniqueId) const {
62 return mConfig.getDisplayViewportByUniqueId(uniqueId);
63}
64std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByType(
65 ViewportType type) const {
66 return mConfig.getDisplayViewportByType(type);
67}
68
69std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByPort(
70 uint8_t displayPort) const {
71 return mConfig.getDisplayViewportByPort(displayPort);
72}
73
74void FakeInputReaderPolicy::addDisplayViewport(DisplayViewport viewport) {
75 mViewports.push_back(std::move(viewport));
76 mConfig.setDisplayViewports(mViewports);
77}
78
79void FakeInputReaderPolicy::addDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +000080 ui::Rotation orientation, bool isActive,
Harry Cutts6b5fbc52022-11-28 16:37:43 +000081 const std::string& uniqueId,
82 std::optional<uint8_t> physicalPort,
83 ViewportType type) {
Michael Wrighta9cf4192022-12-01 23:46:39 +000084 const bool isRotated = orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270;
Harry Cutts6b5fbc52022-11-28 16:37:43 +000085 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
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000129void FakeInputReaderPolicy::addDeviceTypeAssociation(const std::string& inputPort,
130 const std::string& type) {
131 mConfig.deviceTypeAssociations.insert({inputPort, type});
132}
133
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000134void FakeInputReaderPolicy::addInputUniqueIdAssociation(const std::string& inputUniqueId,
135 const std::string& displayUniqueId) {
136 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
137}
138
139void FakeInputReaderPolicy::addDisabledDevice(int32_t deviceId) {
140 mConfig.disabledDevices.insert(deviceId);
141}
142
143void FakeInputReaderPolicy::removeDisabledDevice(int32_t deviceId) {
144 mConfig.disabledDevices.erase(deviceId);
145}
146
147void FakeInputReaderPolicy::setPointerController(
148 std::shared_ptr<FakePointerController> controller) {
149 mPointerController = std::move(controller);
150}
151
152const InputReaderConfiguration* FakeInputReaderPolicy::getReaderConfiguration() const {
153 return &mConfig;
154}
155
156const std::vector<InputDeviceInfo>& FakeInputReaderPolicy::getInputDevices() const {
157 return mInputDevices;
158}
159
160TouchAffineTransformation FakeInputReaderPolicy::getTouchAffineTransformation(
Michael Wrighta9cf4192022-12-01 23:46:39 +0000161 const std::string& inputDeviceDescriptor, ui::Rotation surfaceRotation) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000162 return transform;
163}
164
165void FakeInputReaderPolicy::setTouchAffineTransformation(const TouchAffineTransformation t) {
166 transform = t;
167}
168
169PointerCaptureRequest FakeInputReaderPolicy::setPointerCapture(bool enabled) {
170 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
171 return mConfig.pointerCaptureRequest;
172}
173
174void FakeInputReaderPolicy::setShowTouches(bool enabled) {
175 mConfig.showTouches = enabled;
176}
177
178void FakeInputReaderPolicy::setDefaultPointerDisplayId(int32_t pointerDisplayId) {
179 mConfig.defaultPointerDisplayId = pointerDisplayId;
180}
181
182void FakeInputReaderPolicy::setPointerGestureEnabled(bool enabled) {
183 mConfig.pointerGesturesEnabled = enabled;
184}
185
186float FakeInputReaderPolicy::getPointerGestureMovementSpeedRatio() {
187 return mConfig.pointerGestureMovementSpeedRatio;
188}
189
190float FakeInputReaderPolicy::getPointerGestureZoomSpeedRatio() {
191 return mConfig.pointerGestureZoomSpeedRatio;
192}
193
194void FakeInputReaderPolicy::setVelocityControlParams(const VelocityControlParameters& params) {
195 mConfig.pointerVelocityControlParameters = params;
196 mConfig.wheelVelocityControlParameters = params;
197}
198
199void FakeInputReaderPolicy::getReaderConfiguration(InputReaderConfiguration* outConfig) {
200 *outConfig = mConfig;
201}
202
203std::shared_ptr<PointerControllerInterface> FakeInputReaderPolicy::obtainPointerController(
204 int32_t /*deviceId*/) {
205 return mPointerController;
206}
207
208void FakeInputReaderPolicy::notifyInputDevicesChanged(
209 const std::vector<InputDeviceInfo>& inputDevices) {
210 std::scoped_lock<std::mutex> lock(mLock);
211 mInputDevices = inputDevices;
212 mInputDevicesChanged = true;
213 mDevicesChangedCondition.notify_all();
214}
215
216std::shared_ptr<KeyCharacterMap> FakeInputReaderPolicy::getKeyboardLayoutOverlay(
217 const InputDeviceIdentifier&) {
218 return nullptr;
219}
220
221std::string FakeInputReaderPolicy::getDeviceAlias(const InputDeviceIdentifier&) {
222 return "";
223}
224
225void FakeInputReaderPolicy::waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
226 std::unique_lock<std::mutex> lock(mLock);
227 base::ScopedLockAssertion assumeLocked(mLock);
228
229 const bool devicesChanged =
230 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
231 return mInputDevicesChanged;
232 });
233 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
234 mInputDevicesChanged = false;
235}
236
237void FakeInputReaderPolicy::notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) {
238 std::scoped_lock<std::mutex> lock(mLock);
239 mStylusGestureNotified = deviceId;
240}
241
242} // namespace android