blob: 9e9371248e44939408b4333fd3cdc81b69f89ebd [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) {
Prabir Pradhan40aee532024-02-08 00:47:23 +000044 std::unique_lock lock(mLock);
45 base::ScopedLockAssertion assumeLocked(mLock);
46
47 const bool success =
48 mStylusGestureNotifiedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
49 return mDeviceIdOfNotifiedStylusGesture.has_value();
50 });
51 ASSERT_TRUE(success) << "Timed out waiting for stylus gesture to be notified";
52 ASSERT_EQ(deviceId, *mDeviceIdOfNotifiedStylusGesture);
53 mDeviceIdOfNotifiedStylusGesture.reset();
Harry Cutts6b5fbc52022-11-28 16:37:43 +000054}
55
56void FakeInputReaderPolicy::assertStylusGestureNotNotified() {
57 std::scoped_lock lock(mLock);
Prabir Pradhan40aee532024-02-08 00:47:23 +000058 ASSERT_FALSE(mDeviceIdOfNotifiedStylusGesture);
Harry Cutts6b5fbc52022-11-28 16:37:43 +000059}
60
61void FakeInputReaderPolicy::clearViewports() {
62 mViewports.clear();
63 mConfig.setDisplayViewports(mViewports);
64}
65
66std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByUniqueId(
67 const std::string& uniqueId) const {
68 return mConfig.getDisplayViewportByUniqueId(uniqueId);
69}
70std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByType(
71 ViewportType type) const {
72 return mConfig.getDisplayViewportByType(type);
73}
74
75std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByPort(
76 uint8_t displayPort) const {
77 return mConfig.getDisplayViewportByPort(displayPort);
78}
79
80void FakeInputReaderPolicy::addDisplayViewport(DisplayViewport viewport) {
81 mViewports.push_back(std::move(viewport));
82 mConfig.setDisplayViewports(mViewports);
83}
84
85void FakeInputReaderPolicy::addDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +000086 ui::Rotation orientation, bool isActive,
Harry Cutts6b5fbc52022-11-28 16:37:43 +000087 const std::string& uniqueId,
88 std::optional<uint8_t> physicalPort,
89 ViewportType type) {
Michael Wrighta9cf4192022-12-01 23:46:39 +000090 const bool isRotated = orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270;
Harry Cutts6b5fbc52022-11-28 16:37:43 +000091 DisplayViewport v;
92 v.displayId = displayId;
93 v.orientation = orientation;
94 v.logicalLeft = 0;
95 v.logicalTop = 0;
96 v.logicalRight = isRotated ? height : width;
97 v.logicalBottom = isRotated ? width : height;
98 v.physicalLeft = 0;
99 v.physicalTop = 0;
100 v.physicalRight = isRotated ? height : width;
101 v.physicalBottom = isRotated ? width : height;
102 v.deviceWidth = isRotated ? height : width;
103 v.deviceHeight = isRotated ? width : height;
104 v.isActive = isActive;
105 v.uniqueId = uniqueId;
106 v.physicalPort = physicalPort;
107 v.type = type;
108
109 addDisplayViewport(v);
110}
111
112bool FakeInputReaderPolicy::updateViewport(const DisplayViewport& viewport) {
113 size_t count = mViewports.size();
114 for (size_t i = 0; i < count; i++) {
115 const DisplayViewport& currentViewport = mViewports[i];
116 if (currentViewport.displayId == viewport.displayId) {
117 mViewports[i] = viewport;
118 mConfig.setDisplayViewports(mViewports);
119 return true;
120 }
121 }
122 // no viewport found.
123 return false;
124}
125
126void FakeInputReaderPolicy::addExcludedDeviceName(const std::string& deviceName) {
127 mConfig.excludedDeviceNames.push_back(deviceName);
128}
129
130void FakeInputReaderPolicy::addInputPortAssociation(const std::string& inputPort,
131 uint8_t displayPort) {
132 mConfig.portAssociations.insert({inputPort, displayPort});
133}
134
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000135void FakeInputReaderPolicy::addDeviceTypeAssociation(const std::string& inputPort,
136 const std::string& type) {
137 mConfig.deviceTypeAssociations.insert({inputPort, type});
138}
139
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000140void FakeInputReaderPolicy::addInputUniqueIdAssociation(const std::string& inputUniqueId,
141 const std::string& displayUniqueId) {
142 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
143}
144
Zixuan Qufecb6062022-11-12 04:44:31 +0000145void FakeInputReaderPolicy::addKeyboardLayoutAssociation(const std::string& inputUniqueId,
146 const KeyboardLayoutInfo& layoutInfo) {
147 mConfig.keyboardLayoutAssociations.insert({inputUniqueId, layoutInfo});
148}
149
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000150void FakeInputReaderPolicy::addDisabledDevice(int32_t deviceId) {
151 mConfig.disabledDevices.insert(deviceId);
152}
153
154void FakeInputReaderPolicy::removeDisabledDevice(int32_t deviceId) {
155 mConfig.disabledDevices.erase(deviceId);
156}
157
158void FakeInputReaderPolicy::setPointerController(
159 std::shared_ptr<FakePointerController> controller) {
160 mPointerController = std::move(controller);
161}
162
Arpit Singhed6c3de2023-04-05 19:24:37 +0000163const InputReaderConfiguration& FakeInputReaderPolicy::getReaderConfiguration() const {
164 return mConfig;
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000165}
166
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700167const std::vector<InputDeviceInfo> FakeInputReaderPolicy::getInputDevices() const {
168 std::scoped_lock lock(mLock);
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000169 return mInputDevices;
170}
171
172TouchAffineTransformation FakeInputReaderPolicy::getTouchAffineTransformation(
Michael Wrighta9cf4192022-12-01 23:46:39 +0000173 const std::string& inputDeviceDescriptor, ui::Rotation surfaceRotation) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000174 return transform;
175}
176
177void FakeInputReaderPolicy::setTouchAffineTransformation(const TouchAffineTransformation t) {
178 transform = t;
179}
180
181PointerCaptureRequest FakeInputReaderPolicy::setPointerCapture(bool enabled) {
182 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
183 return mConfig.pointerCaptureRequest;
184}
185
186void FakeInputReaderPolicy::setShowTouches(bool enabled) {
187 mConfig.showTouches = enabled;
188}
189
190void FakeInputReaderPolicy::setDefaultPointerDisplayId(int32_t pointerDisplayId) {
191 mConfig.defaultPointerDisplayId = pointerDisplayId;
192}
193
194void FakeInputReaderPolicy::setPointerGestureEnabled(bool enabled) {
195 mConfig.pointerGesturesEnabled = enabled;
196}
197
198float FakeInputReaderPolicy::getPointerGestureMovementSpeedRatio() {
199 return mConfig.pointerGestureMovementSpeedRatio;
200}
201
202float FakeInputReaderPolicy::getPointerGestureZoomSpeedRatio() {
203 return mConfig.pointerGestureZoomSpeedRatio;
204}
205
206void FakeInputReaderPolicy::setVelocityControlParams(const VelocityControlParameters& params) {
207 mConfig.pointerVelocityControlParameters = params;
208 mConfig.wheelVelocityControlParameters = params;
209}
210
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +0000211void FakeInputReaderPolicy::setStylusButtonMotionEventsEnabled(bool enabled) {
212 mConfig.stylusButtonMotionEventsEnabled = enabled;
213}
214
Seunghwan Choi356026c2023-02-01 14:37:25 +0900215void FakeInputReaderPolicy::setStylusPointerIconEnabled(bool enabled) {
216 mConfig.stylusPointerIconEnabled = enabled;
217}
218
Arpit Singhb3b3f732023-07-04 14:30:05 +0000219void FakeInputReaderPolicy::setIsInputMethodConnectionActive(bool active) {
220 mIsInputMethodConnectionActive = active;
221}
222
223bool FakeInputReaderPolicy::isInputMethodConnectionActive() {
224 return mIsInputMethodConnectionActive;
225}
226
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000227void FakeInputReaderPolicy::getReaderConfiguration(InputReaderConfiguration* outConfig) {
228 *outConfig = mConfig;
229}
230
231std::shared_ptr<PointerControllerInterface> FakeInputReaderPolicy::obtainPointerController(
232 int32_t /*deviceId*/) {
233 return mPointerController;
234}
235
236void FakeInputReaderPolicy::notifyInputDevicesChanged(
237 const std::vector<InputDeviceInfo>& inputDevices) {
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700238 std::scoped_lock lock(mLock);
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000239 mInputDevices = inputDevices;
240 mInputDevicesChanged = true;
241 mDevicesChangedCondition.notify_all();
242}
243
244std::shared_ptr<KeyCharacterMap> FakeInputReaderPolicy::getKeyboardLayoutOverlay(
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000245 const InputDeviceIdentifier&, const std::optional<KeyboardLayoutInfo>) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000246 return nullptr;
247}
248
249std::string FakeInputReaderPolicy::getDeviceAlias(const InputDeviceIdentifier&) {
250 return "";
251}
252
253void FakeInputReaderPolicy::waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
254 std::unique_lock<std::mutex> lock(mLock);
255 base::ScopedLockAssertion assumeLocked(mLock);
256
257 const bool devicesChanged =
258 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
259 return mInputDevicesChanged;
260 });
261 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
262 mInputDevicesChanged = false;
263}
264
265void FakeInputReaderPolicy::notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) {
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700266 std::scoped_lock lock(mLock);
Prabir Pradhan40aee532024-02-08 00:47:23 +0000267 mDeviceIdOfNotifiedStylusGesture = deviceId;
268 mStylusGestureNotifiedCondition.notify_all();
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000269}
270
Prabir Pradhan19767602023-11-03 16:53:31 +0000271std::optional<DisplayViewport> FakeInputReaderPolicy::getPointerViewportForAssociatedDisplay(
Byoungho Jungda10dd32023-10-06 17:03:45 +0900272 int32_t associatedDisplayId) {
273 if (associatedDisplayId == ADISPLAY_ID_NONE) {
274 associatedDisplayId = mConfig.defaultPointerDisplayId;
275 }
276 for (auto& viewport : mViewports) {
277 if (viewport.displayId == associatedDisplayId) {
278 return std::make_optional(viewport);
279 }
280 }
281 return std::nullopt;
282}
283
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000284} // namespace android