blob: d77d539f74aa22802d477cea62f19ce381d79a53 [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
Abdelrahman Awadalla8c4160d2024-08-05 16:26:10 +000068void FakeInputReaderPolicy::assertTouchpadHardwareStateNotified() {
69 std::unique_lock lock(mLock);
70 base::ScopedLockAssertion assumeLocked(mLock);
71
72 const bool success =
73 mTouchpadHardwareStateNotified.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
74 return mTouchpadHardwareState.has_value();
75 });
76 ASSERT_TRUE(success) << "Timed out waiting for hardware state to be notified";
77}
78
Harry Cutts6b5fbc52022-11-28 16:37:43 +000079void FakeInputReaderPolicy::clearViewports() {
80 mViewports.clear();
81 mConfig.setDisplayViewports(mViewports);
82}
83
84std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByUniqueId(
85 const std::string& uniqueId) const {
86 return mConfig.getDisplayViewportByUniqueId(uniqueId);
87}
88std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByType(
89 ViewportType type) const {
90 return mConfig.getDisplayViewportByType(type);
91}
92
93std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByPort(
94 uint8_t displayPort) const {
95 return mConfig.getDisplayViewportByPort(displayPort);
96}
97
98void FakeInputReaderPolicy::addDisplayViewport(DisplayViewport viewport) {
99 mViewports.push_back(std::move(viewport));
100 mConfig.setDisplayViewports(mViewports);
101}
102
Linnan Li13bf76a2024-05-05 19:18:02 +0800103void FakeInputReaderPolicy::addDisplayViewport(ui::LogicalDisplayId displayId, int32_t width,
104 int32_t height, ui::Rotation orientation,
105 bool isActive, const std::string& uniqueId,
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000106 std::optional<uint8_t> physicalPort,
107 ViewportType type) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000108 const bool isRotated = orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270;
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000109 DisplayViewport v;
110 v.displayId = displayId;
111 v.orientation = orientation;
112 v.logicalLeft = 0;
113 v.logicalTop = 0;
114 v.logicalRight = isRotated ? height : width;
115 v.logicalBottom = isRotated ? width : height;
116 v.physicalLeft = 0;
117 v.physicalTop = 0;
118 v.physicalRight = isRotated ? height : width;
119 v.physicalBottom = isRotated ? width : height;
120 v.deviceWidth = isRotated ? height : width;
121 v.deviceHeight = isRotated ? width : height;
122 v.isActive = isActive;
123 v.uniqueId = uniqueId;
124 v.physicalPort = physicalPort;
125 v.type = type;
126
127 addDisplayViewport(v);
128}
129
130bool FakeInputReaderPolicy::updateViewport(const DisplayViewport& viewport) {
131 size_t count = mViewports.size();
132 for (size_t i = 0; i < count; i++) {
133 const DisplayViewport& currentViewport = mViewports[i];
134 if (currentViewport.displayId == viewport.displayId) {
135 mViewports[i] = viewport;
136 mConfig.setDisplayViewports(mViewports);
137 return true;
138 }
139 }
140 // no viewport found.
141 return false;
142}
143
144void FakeInputReaderPolicy::addExcludedDeviceName(const std::string& deviceName) {
145 mConfig.excludedDeviceNames.push_back(deviceName);
146}
147
148void FakeInputReaderPolicy::addInputPortAssociation(const std::string& inputPort,
149 uint8_t displayPort) {
Mayank Garg60b45622024-05-05 20:23:56 -0700150 mConfig.inputPortToDisplayPortAssociations.insert({inputPort, displayPort});
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000151}
152
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000153void FakeInputReaderPolicy::addDeviceTypeAssociation(const std::string& inputPort,
154 const std::string& type) {
155 mConfig.deviceTypeAssociations.insert({inputPort, type});
156}
157
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000158void FakeInputReaderPolicy::addInputUniqueIdAssociation(const std::string& inputUniqueId,
159 const std::string& displayUniqueId) {
Mayank Garg60b45622024-05-05 20:23:56 -0700160 mConfig.inputPortToDisplayUniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000161}
162
Zixuan Qufecb6062022-11-12 04:44:31 +0000163void FakeInputReaderPolicy::addKeyboardLayoutAssociation(const std::string& inputUniqueId,
164 const KeyboardLayoutInfo& layoutInfo) {
165 mConfig.keyboardLayoutAssociations.insert({inputUniqueId, layoutInfo});
166}
167
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000168void FakeInputReaderPolicy::addDisabledDevice(int32_t deviceId) {
169 mConfig.disabledDevices.insert(deviceId);
170}
171
172void FakeInputReaderPolicy::removeDisabledDevice(int32_t deviceId) {
173 mConfig.disabledDevices.erase(deviceId);
174}
175
Arpit Singhed6c3de2023-04-05 19:24:37 +0000176const InputReaderConfiguration& FakeInputReaderPolicy::getReaderConfiguration() const {
177 return mConfig;
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000178}
179
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700180const std::vector<InputDeviceInfo> FakeInputReaderPolicy::getInputDevices() const {
181 std::scoped_lock lock(mLock);
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000182 return mInputDevices;
183}
184
185TouchAffineTransformation FakeInputReaderPolicy::getTouchAffineTransformation(
Michael Wrighta9cf4192022-12-01 23:46:39 +0000186 const std::string& inputDeviceDescriptor, ui::Rotation surfaceRotation) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000187 return transform;
188}
189
190void FakeInputReaderPolicy::setTouchAffineTransformation(const TouchAffineTransformation t) {
191 transform = t;
192}
193
Hiroki Sato25040232024-02-22 17:21:22 +0900194PointerCaptureRequest FakeInputReaderPolicy::setPointerCapture(const sp<IBinder>& window) {
195 mConfig.pointerCaptureRequest = {window, mNextPointerCaptureSequenceNumber++};
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000196 return mConfig.pointerCaptureRequest;
197}
198
Linnan Li13bf76a2024-05-05 19:18:02 +0800199void FakeInputReaderPolicy::setDefaultPointerDisplayId(ui::LogicalDisplayId pointerDisplayId) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000200 mConfig.defaultPointerDisplayId = pointerDisplayId;
201}
202
203void FakeInputReaderPolicy::setPointerGestureEnabled(bool enabled) {
204 mConfig.pointerGesturesEnabled = enabled;
205}
206
207float FakeInputReaderPolicy::getPointerGestureMovementSpeedRatio() {
208 return mConfig.pointerGestureMovementSpeedRatio;
209}
210
211float FakeInputReaderPolicy::getPointerGestureZoomSpeedRatio() {
212 return mConfig.pointerGestureZoomSpeedRatio;
213}
214
215void FakeInputReaderPolicy::setVelocityControlParams(const VelocityControlParameters& params) {
216 mConfig.pointerVelocityControlParameters = params;
217 mConfig.wheelVelocityControlParameters = params;
218}
219
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +0000220void FakeInputReaderPolicy::setStylusButtonMotionEventsEnabled(bool enabled) {
221 mConfig.stylusButtonMotionEventsEnabled = enabled;
222}
223
Seunghwan Choi356026c2023-02-01 14:37:25 +0900224void FakeInputReaderPolicy::setStylusPointerIconEnabled(bool enabled) {
225 mConfig.stylusPointerIconEnabled = enabled;
226}
227
Arpit Singhb3b3f732023-07-04 14:30:05 +0000228void FakeInputReaderPolicy::setIsInputMethodConnectionActive(bool active) {
229 mIsInputMethodConnectionActive = active;
230}
231
232bool FakeInputReaderPolicy::isInputMethodConnectionActive() {
233 return mIsInputMethodConnectionActive;
234}
235
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000236void FakeInputReaderPolicy::getReaderConfiguration(InputReaderConfiguration* outConfig) {
237 *outConfig = mConfig;
238}
239
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000240void FakeInputReaderPolicy::notifyInputDevicesChanged(
241 const std::vector<InputDeviceInfo>& inputDevices) {
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700242 std::scoped_lock lock(mLock);
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000243 mInputDevices = inputDevices;
244 mInputDevicesChanged = true;
245 mDevicesChangedCondition.notify_all();
246}
247
Abdelrahman Awadalla8c4160d2024-08-05 16:26:10 +0000248void FakeInputReaderPolicy::notifyTouchpadHardwareState(const SelfContainedHardwareState& schs,
249 int32_t deviceId) {
250 std::scoped_lock lock(mLock);
251 mTouchpadHardwareState = schs;
252 mTouchpadHardwareStateNotified.notify_all();
253}
254
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000255std::shared_ptr<KeyCharacterMap> FakeInputReaderPolicy::getKeyboardLayoutOverlay(
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000256 const InputDeviceIdentifier&, const std::optional<KeyboardLayoutInfo>) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000257 return nullptr;
258}
259
260std::string FakeInputReaderPolicy::getDeviceAlias(const InputDeviceIdentifier&) {
261 return "";
262}
263
264void FakeInputReaderPolicy::waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
265 std::unique_lock<std::mutex> lock(mLock);
266 base::ScopedLockAssertion assumeLocked(mLock);
267
268 const bool devicesChanged =
Arpit Singh42a145f2024-06-18 15:30:25 +0000269 mDevicesChangedCondition.wait_for(lock,
270 ADD_INPUT_DEVICE_TIMEOUT * HW_TIMEOUT_MULTIPLIER,
271 [this]() REQUIRES(mLock) {
272 return mInputDevicesChanged;
273 });
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000274 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
275 mInputDevicesChanged = false;
276}
277
278void FakeInputReaderPolicy::notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) {
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700279 std::scoped_lock lock(mLock);
Prabir Pradhan40aee532024-02-08 00:47:23 +0000280 mDeviceIdOfNotifiedStylusGesture = deviceId;
281 mStylusGestureNotifiedCondition.notify_all();
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000282}
283
Prabir Pradhan19767602023-11-03 16:53:31 +0000284std::optional<DisplayViewport> FakeInputReaderPolicy::getPointerViewportForAssociatedDisplay(
Linnan Li13bf76a2024-05-05 19:18:02 +0800285 ui::LogicalDisplayId associatedDisplayId) {
286 if (!associatedDisplayId.isValid()) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900287 associatedDisplayId = mConfig.defaultPointerDisplayId;
288 }
289 for (auto& viewport : mViewports) {
290 if (viewport.displayId == associatedDisplayId) {
291 return std::make_optional(viewport);
292 }
293 }
294 return std::nullopt;
295}
296
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000297} // namespace android