blob: 56a8918b96481e278527362998f954343e2651ea [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
Arpit Singh42a145f2024-06-18 15:30:25 +000019#include <android-base/properties.h>
Harry Cutts6b5fbc52022-11-28 16:37:43 +000020#include <android-base/thread_annotations.h>
21#include <gtest/gtest.h>
22
23#include "TestConstants.h"
Michael Wrighta9cf4192022-12-01 23:46:39 +000024#include "ui/Rotation.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000025
26namespace android {
27
Arpit Singh42a145f2024-06-18 15:30:25 +000028namespace {
29
30static const int HW_TIMEOUT_MULTIPLIER = base::GetIntProperty("ro.hw_timeout_multiplier", 1);
31
32} // namespace
33
Harry Cutts6b5fbc52022-11-28 16:37:43 +000034void FakeInputReaderPolicy::assertInputDevicesChanged() {
35 waitForInputDevices([](bool devicesChanged) {
36 if (!devicesChanged) {
37 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
38 }
39 });
40}
41
42void FakeInputReaderPolicy::assertInputDevicesNotChanged() {
43 waitForInputDevices([](bool devicesChanged) {
44 if (devicesChanged) {
45 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
46 }
47 });
48}
49
50void FakeInputReaderPolicy::assertStylusGestureNotified(int32_t deviceId) {
Prabir Pradhan40aee532024-02-08 00:47:23 +000051 std::unique_lock lock(mLock);
52 base::ScopedLockAssertion assumeLocked(mLock);
53
54 const bool success =
55 mStylusGestureNotifiedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
56 return mDeviceIdOfNotifiedStylusGesture.has_value();
57 });
58 ASSERT_TRUE(success) << "Timed out waiting for stylus gesture to be notified";
59 ASSERT_EQ(deviceId, *mDeviceIdOfNotifiedStylusGesture);
60 mDeviceIdOfNotifiedStylusGesture.reset();
Harry Cutts6b5fbc52022-11-28 16:37:43 +000061}
62
63void FakeInputReaderPolicy::assertStylusGestureNotNotified() {
64 std::scoped_lock lock(mLock);
Prabir Pradhan40aee532024-02-08 00:47:23 +000065 ASSERT_FALSE(mDeviceIdOfNotifiedStylusGesture);
Harry Cutts6b5fbc52022-11-28 16:37:43 +000066}
67
Vaibhav Devmuraric109d812024-07-10 14:21:27 +000068void FakeInputReaderPolicy::assertConfigurationChanged() {
69 std::unique_lock lock(mLock);
70 base::ScopedLockAssertion assumeLocked(mLock);
71
72 const bool configurationChanged =
73 mConfigurationChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
74 return mConfigurationChanged;
75 });
76 ASSERT_TRUE(configurationChanged) << "Timed out waiting for configuration change";
77 mConfigurationChanged = false;
78}
79
80void FakeInputReaderPolicy::assertConfigurationNotChanged() {
81 std::scoped_lock lock(mLock);
82 ASSERT_FALSE(mConfigurationChanged);
83}
84
Harry Cutts6b5fbc52022-11-28 16:37:43 +000085void FakeInputReaderPolicy::clearViewports() {
86 mViewports.clear();
87 mConfig.setDisplayViewports(mViewports);
88}
89
90std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByUniqueId(
91 const std::string& uniqueId) const {
92 return mConfig.getDisplayViewportByUniqueId(uniqueId);
93}
94std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByType(
95 ViewportType type) const {
96 return mConfig.getDisplayViewportByType(type);
97}
98
99std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByPort(
100 uint8_t displayPort) const {
101 return mConfig.getDisplayViewportByPort(displayPort);
102}
103
104void FakeInputReaderPolicy::addDisplayViewport(DisplayViewport viewport) {
105 mViewports.push_back(std::move(viewport));
106 mConfig.setDisplayViewports(mViewports);
107}
108
Linnan Li13bf76a2024-05-05 19:18:02 +0800109void FakeInputReaderPolicy::addDisplayViewport(ui::LogicalDisplayId displayId, int32_t width,
110 int32_t height, ui::Rotation orientation,
111 bool isActive, const std::string& uniqueId,
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000112 std::optional<uint8_t> physicalPort,
113 ViewportType type) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000114 const bool isRotated = orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270;
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000115 DisplayViewport v;
116 v.displayId = displayId;
117 v.orientation = orientation;
118 v.logicalLeft = 0;
119 v.logicalTop = 0;
120 v.logicalRight = isRotated ? height : width;
121 v.logicalBottom = isRotated ? width : height;
122 v.physicalLeft = 0;
123 v.physicalTop = 0;
124 v.physicalRight = isRotated ? height : width;
125 v.physicalBottom = isRotated ? width : height;
126 v.deviceWidth = isRotated ? height : width;
127 v.deviceHeight = isRotated ? width : height;
128 v.isActive = isActive;
129 v.uniqueId = uniqueId;
130 v.physicalPort = physicalPort;
131 v.type = type;
132
133 addDisplayViewport(v);
134}
135
136bool FakeInputReaderPolicy::updateViewport(const DisplayViewport& viewport) {
137 size_t count = mViewports.size();
138 for (size_t i = 0; i < count; i++) {
139 const DisplayViewport& currentViewport = mViewports[i];
140 if (currentViewport.displayId == viewport.displayId) {
141 mViewports[i] = viewport;
142 mConfig.setDisplayViewports(mViewports);
143 return true;
144 }
145 }
146 // no viewport found.
147 return false;
148}
149
150void FakeInputReaderPolicy::addExcludedDeviceName(const std::string& deviceName) {
151 mConfig.excludedDeviceNames.push_back(deviceName);
152}
153
154void FakeInputReaderPolicy::addInputPortAssociation(const std::string& inputPort,
155 uint8_t displayPort) {
Mayank Garg60b45622024-05-05 20:23:56 -0700156 mConfig.inputPortToDisplayPortAssociations.insert({inputPort, displayPort});
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000157}
158
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000159void FakeInputReaderPolicy::addDeviceTypeAssociation(const std::string& inputPort,
160 const std::string& type) {
161 mConfig.deviceTypeAssociations.insert({inputPort, type});
162}
163
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000164void FakeInputReaderPolicy::addInputUniqueIdAssociation(const std::string& inputUniqueId,
165 const std::string& displayUniqueId) {
Mayank Garg60b45622024-05-05 20:23:56 -0700166 mConfig.inputPortToDisplayUniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000167}
168
Zixuan Qufecb6062022-11-12 04:44:31 +0000169void FakeInputReaderPolicy::addKeyboardLayoutAssociation(const std::string& inputUniqueId,
170 const KeyboardLayoutInfo& layoutInfo) {
171 mConfig.keyboardLayoutAssociations.insert({inputUniqueId, layoutInfo});
172}
173
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000174void FakeInputReaderPolicy::addDisabledDevice(int32_t deviceId) {
175 mConfig.disabledDevices.insert(deviceId);
176}
177
178void FakeInputReaderPolicy::removeDisabledDevice(int32_t deviceId) {
179 mConfig.disabledDevices.erase(deviceId);
180}
181
Arpit Singhed6c3de2023-04-05 19:24:37 +0000182const InputReaderConfiguration& FakeInputReaderPolicy::getReaderConfiguration() const {
183 return mConfig;
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000184}
185
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700186const std::vector<InputDeviceInfo> FakeInputReaderPolicy::getInputDevices() const {
187 std::scoped_lock lock(mLock);
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000188 return mInputDevices;
189}
190
191TouchAffineTransformation FakeInputReaderPolicy::getTouchAffineTransformation(
Michael Wrighta9cf4192022-12-01 23:46:39 +0000192 const std::string& inputDeviceDescriptor, ui::Rotation surfaceRotation) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000193 return transform;
194}
195
196void FakeInputReaderPolicy::setTouchAffineTransformation(const TouchAffineTransformation t) {
197 transform = t;
198}
199
Hiroki Sato25040232024-02-22 17:21:22 +0900200PointerCaptureRequest FakeInputReaderPolicy::setPointerCapture(const sp<IBinder>& window) {
201 mConfig.pointerCaptureRequest = {window, mNextPointerCaptureSequenceNumber++};
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000202 return mConfig.pointerCaptureRequest;
203}
204
Linnan Li13bf76a2024-05-05 19:18:02 +0800205void FakeInputReaderPolicy::setDefaultPointerDisplayId(ui::LogicalDisplayId pointerDisplayId) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000206 mConfig.defaultPointerDisplayId = pointerDisplayId;
207}
208
209void FakeInputReaderPolicy::setPointerGestureEnabled(bool enabled) {
210 mConfig.pointerGesturesEnabled = enabled;
211}
212
213float FakeInputReaderPolicy::getPointerGestureMovementSpeedRatio() {
214 return mConfig.pointerGestureMovementSpeedRatio;
215}
216
217float FakeInputReaderPolicy::getPointerGestureZoomSpeedRatio() {
218 return mConfig.pointerGestureZoomSpeedRatio;
219}
220
221void FakeInputReaderPolicy::setVelocityControlParams(const VelocityControlParameters& params) {
222 mConfig.pointerVelocityControlParameters = params;
223 mConfig.wheelVelocityControlParameters = params;
224}
225
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +0000226void FakeInputReaderPolicy::setStylusButtonMotionEventsEnabled(bool enabled) {
227 mConfig.stylusButtonMotionEventsEnabled = enabled;
228}
229
Seunghwan Choi356026c2023-02-01 14:37:25 +0900230void FakeInputReaderPolicy::setStylusPointerIconEnabled(bool enabled) {
231 mConfig.stylusPointerIconEnabled = enabled;
232}
233
Arpit Singhb3b3f732023-07-04 14:30:05 +0000234void FakeInputReaderPolicy::setIsInputMethodConnectionActive(bool active) {
235 mIsInputMethodConnectionActive = active;
236}
237
238bool FakeInputReaderPolicy::isInputMethodConnectionActive() {
239 return mIsInputMethodConnectionActive;
240}
241
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000242void FakeInputReaderPolicy::getReaderConfiguration(InputReaderConfiguration* outConfig) {
243 *outConfig = mConfig;
244}
245
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000246void FakeInputReaderPolicy::notifyInputDevicesChanged(
247 const std::vector<InputDeviceInfo>& inputDevices) {
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700248 std::scoped_lock lock(mLock);
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000249 mInputDevices = inputDevices;
250 mInputDevicesChanged = true;
251 mDevicesChangedCondition.notify_all();
252}
253
Vaibhav Devmuraric109d812024-07-10 14:21:27 +0000254void FakeInputReaderPolicy::notifyConfigurationChanged(nsecs_t when) {
255 std::scoped_lock lock(mLock);
256 mConfigurationChanged = true;
257 mConfigurationChangedCondition.notify_all();
258}
259
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000260std::shared_ptr<KeyCharacterMap> FakeInputReaderPolicy::getKeyboardLayoutOverlay(
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000261 const InputDeviceIdentifier&, const std::optional<KeyboardLayoutInfo>) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000262 return nullptr;
263}
264
265std::string FakeInputReaderPolicy::getDeviceAlias(const InputDeviceIdentifier&) {
266 return "";
267}
268
269void FakeInputReaderPolicy::waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
270 std::unique_lock<std::mutex> lock(mLock);
271 base::ScopedLockAssertion assumeLocked(mLock);
272
273 const bool devicesChanged =
Arpit Singh42a145f2024-06-18 15:30:25 +0000274 mDevicesChangedCondition.wait_for(lock,
275 ADD_INPUT_DEVICE_TIMEOUT * HW_TIMEOUT_MULTIPLIER,
276 [this]() REQUIRES(mLock) {
277 return mInputDevicesChanged;
278 });
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000279 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
280 mInputDevicesChanged = false;
281}
282
283void FakeInputReaderPolicy::notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) {
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700284 std::scoped_lock lock(mLock);
Prabir Pradhan40aee532024-02-08 00:47:23 +0000285 mDeviceIdOfNotifiedStylusGesture = deviceId;
286 mStylusGestureNotifiedCondition.notify_all();
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000287}
288
Prabir Pradhan19767602023-11-03 16:53:31 +0000289std::optional<DisplayViewport> FakeInputReaderPolicy::getPointerViewportForAssociatedDisplay(
Linnan Li13bf76a2024-05-05 19:18:02 +0800290 ui::LogicalDisplayId associatedDisplayId) {
291 if (!associatedDisplayId.isValid()) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900292 associatedDisplayId = mConfig.defaultPointerDisplayId;
293 }
294 for (auto& viewport : mViewports) {
295 if (viewport.displayId == associatedDisplayId) {
296 return std::make_optional(viewport);
297 }
298 }
299 return std::nullopt;
300}
301
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000302} // namespace android