blob: 41c98ef9e94faa6bf9fa0385f222cfbcc0a0b732 [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
Zixuan Qufecb6062022-11-12 04:44:31 +0000139void FakeInputReaderPolicy::addKeyboardLayoutAssociation(const std::string& inputUniqueId,
140 const KeyboardLayoutInfo& layoutInfo) {
141 mConfig.keyboardLayoutAssociations.insert({inputUniqueId, layoutInfo});
142}
143
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000144void FakeInputReaderPolicy::addDisabledDevice(int32_t deviceId) {
145 mConfig.disabledDevices.insert(deviceId);
146}
147
148void FakeInputReaderPolicy::removeDisabledDevice(int32_t deviceId) {
149 mConfig.disabledDevices.erase(deviceId);
150}
151
152void FakeInputReaderPolicy::setPointerController(
153 std::shared_ptr<FakePointerController> controller) {
154 mPointerController = std::move(controller);
155}
156
Arpit Singhed6c3de2023-04-05 19:24:37 +0000157const InputReaderConfiguration& FakeInputReaderPolicy::getReaderConfiguration() const {
158 return mConfig;
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000159}
160
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700161const std::vector<InputDeviceInfo> FakeInputReaderPolicy::getInputDevices() const {
162 std::scoped_lock lock(mLock);
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000163 return mInputDevices;
164}
165
166TouchAffineTransformation FakeInputReaderPolicy::getTouchAffineTransformation(
Michael Wrighta9cf4192022-12-01 23:46:39 +0000167 const std::string& inputDeviceDescriptor, ui::Rotation surfaceRotation) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000168 return transform;
169}
170
171void FakeInputReaderPolicy::setTouchAffineTransformation(const TouchAffineTransformation t) {
172 transform = t;
173}
174
175PointerCaptureRequest FakeInputReaderPolicy::setPointerCapture(bool enabled) {
176 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
177 return mConfig.pointerCaptureRequest;
178}
179
180void FakeInputReaderPolicy::setShowTouches(bool enabled) {
181 mConfig.showTouches = enabled;
182}
183
184void FakeInputReaderPolicy::setDefaultPointerDisplayId(int32_t pointerDisplayId) {
185 mConfig.defaultPointerDisplayId = pointerDisplayId;
186}
187
188void FakeInputReaderPolicy::setPointerGestureEnabled(bool enabled) {
189 mConfig.pointerGesturesEnabled = enabled;
190}
191
192float FakeInputReaderPolicy::getPointerGestureMovementSpeedRatio() {
193 return mConfig.pointerGestureMovementSpeedRatio;
194}
195
196float FakeInputReaderPolicy::getPointerGestureZoomSpeedRatio() {
197 return mConfig.pointerGestureZoomSpeedRatio;
198}
199
200void FakeInputReaderPolicy::setVelocityControlParams(const VelocityControlParameters& params) {
201 mConfig.pointerVelocityControlParameters = params;
202 mConfig.wheelVelocityControlParameters = params;
203}
204
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +0000205void FakeInputReaderPolicy::setStylusButtonMotionEventsEnabled(bool enabled) {
206 mConfig.stylusButtonMotionEventsEnabled = enabled;
207}
208
Seunghwan Choi356026c2023-02-01 14:37:25 +0900209void FakeInputReaderPolicy::setStylusPointerIconEnabled(bool enabled) {
210 mConfig.stylusPointerIconEnabled = enabled;
211}
212
Arpit Singhb3b3f732023-07-04 14:30:05 +0000213void FakeInputReaderPolicy::setIsInputMethodConnectionActive(bool active) {
214 mIsInputMethodConnectionActive = active;
215}
216
217bool FakeInputReaderPolicy::isInputMethodConnectionActive() {
218 return mIsInputMethodConnectionActive;
219}
220
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000221void FakeInputReaderPolicy::getReaderConfiguration(InputReaderConfiguration* outConfig) {
222 *outConfig = mConfig;
223}
224
225std::shared_ptr<PointerControllerInterface> FakeInputReaderPolicy::obtainPointerController(
226 int32_t /*deviceId*/) {
227 return mPointerController;
228}
229
230void FakeInputReaderPolicy::notifyInputDevicesChanged(
231 const std::vector<InputDeviceInfo>& inputDevices) {
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700232 std::scoped_lock lock(mLock);
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000233 mInputDevices = inputDevices;
234 mInputDevicesChanged = true;
235 mDevicesChangedCondition.notify_all();
236}
237
238std::shared_ptr<KeyCharacterMap> FakeInputReaderPolicy::getKeyboardLayoutOverlay(
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000239 const InputDeviceIdentifier&, const std::optional<KeyboardLayoutInfo>) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000240 return nullptr;
241}
242
243std::string FakeInputReaderPolicy::getDeviceAlias(const InputDeviceIdentifier&) {
244 return "";
245}
246
247void FakeInputReaderPolicy::waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
248 std::unique_lock<std::mutex> lock(mLock);
249 base::ScopedLockAssertion assumeLocked(mLock);
250
251 const bool devicesChanged =
252 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
253 return mInputDevicesChanged;
254 });
255 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
256 mInputDevicesChanged = false;
257}
258
259void FakeInputReaderPolicy::notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) {
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700260 std::scoped_lock lock(mLock);
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000261 mStylusGestureNotified = deviceId;
262}
263
264} // namespace android