blob: 67b1e8c249c88fb83064fe6fd147340a3d084516 [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() {
Siarhei Vishniakoubfd75112024-09-04 00:29:42 +000035 waitForInputDevices(
36 [](bool devicesChanged) {
37 if (!devicesChanged) {
38 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
39 }
40 },
41 ADD_INPUT_DEVICE_TIMEOUT);
Harry Cutts6b5fbc52022-11-28 16:37:43 +000042}
43
44void FakeInputReaderPolicy::assertInputDevicesNotChanged() {
Siarhei Vishniakoubfd75112024-09-04 00:29:42 +000045 waitForInputDevices(
46 [](bool devicesChanged) {
47 if (devicesChanged) {
48 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
49 }
50 },
51 INPUT_DEVICES_DIDNT_CHANGE_TIMEOUT);
Harry Cutts6b5fbc52022-11-28 16:37:43 +000052}
53
54void FakeInputReaderPolicy::assertStylusGestureNotified(int32_t deviceId) {
Prabir Pradhan40aee532024-02-08 00:47:23 +000055 std::unique_lock lock(mLock);
56 base::ScopedLockAssertion assumeLocked(mLock);
57
58 const bool success =
59 mStylusGestureNotifiedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
60 return mDeviceIdOfNotifiedStylusGesture.has_value();
61 });
62 ASSERT_TRUE(success) << "Timed out waiting for stylus gesture to be notified";
63 ASSERT_EQ(deviceId, *mDeviceIdOfNotifiedStylusGesture);
64 mDeviceIdOfNotifiedStylusGesture.reset();
Harry Cutts6b5fbc52022-11-28 16:37:43 +000065}
66
67void FakeInputReaderPolicy::assertStylusGestureNotNotified() {
68 std::scoped_lock lock(mLock);
Prabir Pradhan40aee532024-02-08 00:47:23 +000069 ASSERT_FALSE(mDeviceIdOfNotifiedStylusGesture);
Harry Cutts6b5fbc52022-11-28 16:37:43 +000070}
71
Abdelrahman Awadalla8c4160d2024-08-05 16:26:10 +000072void FakeInputReaderPolicy::assertTouchpadHardwareStateNotified() {
73 std::unique_lock lock(mLock);
74 base::ScopedLockAssertion assumeLocked(mLock);
75
76 const bool success =
77 mTouchpadHardwareStateNotified.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
78 return mTouchpadHardwareState.has_value();
79 });
80 ASSERT_TRUE(success) << "Timed out waiting for hardware state to be notified";
81}
82
Harry Cutts2a0210e2024-10-18 19:12:59 +000083void FakeInputReaderPolicy::assertTouchpadThreeFingerTapNotified() {
84 std::unique_lock lock(mLock);
85 base::ScopedLockAssertion assumeLocked(mLock);
86
87 const bool success =
88 mTouchpadThreeFingerTapNotified.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
89 return mTouchpadThreeFingerTapHasBeenReported;
90 });
91 ASSERT_TRUE(success) << "Timed out waiting for three-finger tap to be notified";
92}
93
Harry Cutts6b5fbc52022-11-28 16:37:43 +000094void FakeInputReaderPolicy::clearViewports() {
95 mViewports.clear();
96 mConfig.setDisplayViewports(mViewports);
97}
98
99std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByUniqueId(
100 const std::string& uniqueId) const {
101 return mConfig.getDisplayViewportByUniqueId(uniqueId);
102}
103std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByType(
104 ViewportType type) const {
105 return mConfig.getDisplayViewportByType(type);
106}
107
108std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByPort(
109 uint8_t displayPort) const {
110 return mConfig.getDisplayViewportByPort(displayPort);
111}
112
113void FakeInputReaderPolicy::addDisplayViewport(DisplayViewport viewport) {
114 mViewports.push_back(std::move(viewport));
115 mConfig.setDisplayViewports(mViewports);
116}
117
Linnan Li13bf76a2024-05-05 19:18:02 +0800118void FakeInputReaderPolicy::addDisplayViewport(ui::LogicalDisplayId displayId, int32_t width,
119 int32_t height, ui::Rotation orientation,
120 bool isActive, const std::string& uniqueId,
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000121 std::optional<uint8_t> physicalPort,
122 ViewportType type) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000123 const bool isRotated = orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270;
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000124 DisplayViewport v;
125 v.displayId = displayId;
126 v.orientation = orientation;
127 v.logicalLeft = 0;
128 v.logicalTop = 0;
129 v.logicalRight = isRotated ? height : width;
130 v.logicalBottom = isRotated ? width : height;
131 v.physicalLeft = 0;
132 v.physicalTop = 0;
133 v.physicalRight = isRotated ? height : width;
134 v.physicalBottom = isRotated ? width : height;
135 v.deviceWidth = isRotated ? height : width;
136 v.deviceHeight = isRotated ? width : height;
137 v.isActive = isActive;
138 v.uniqueId = uniqueId;
139 v.physicalPort = physicalPort;
140 v.type = type;
141
142 addDisplayViewport(v);
143}
144
145bool FakeInputReaderPolicy::updateViewport(const DisplayViewport& viewport) {
146 size_t count = mViewports.size();
147 for (size_t i = 0; i < count; i++) {
148 const DisplayViewport& currentViewport = mViewports[i];
149 if (currentViewport.displayId == viewport.displayId) {
150 mViewports[i] = viewport;
151 mConfig.setDisplayViewports(mViewports);
152 return true;
153 }
154 }
155 // no viewport found.
156 return false;
157}
158
159void FakeInputReaderPolicy::addExcludedDeviceName(const std::string& deviceName) {
160 mConfig.excludedDeviceNames.push_back(deviceName);
161}
162
163void FakeInputReaderPolicy::addInputPortAssociation(const std::string& inputPort,
164 uint8_t displayPort) {
Mayank Garg60b45622024-05-05 20:23:56 -0700165 mConfig.inputPortToDisplayPortAssociations.insert({inputPort, displayPort});
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000166}
167
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000168void FakeInputReaderPolicy::addDeviceTypeAssociation(const std::string& inputPort,
169 const std::string& type) {
170 mConfig.deviceTypeAssociations.insert({inputPort, type});
171}
172
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000173void FakeInputReaderPolicy::addInputUniqueIdAssociation(const std::string& inputUniqueId,
174 const std::string& displayUniqueId) {
Mayank Garg60b45622024-05-05 20:23:56 -0700175 mConfig.inputPortToDisplayUniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000176}
177
Zixuan Qufecb6062022-11-12 04:44:31 +0000178void FakeInputReaderPolicy::addKeyboardLayoutAssociation(const std::string& inputUniqueId,
179 const KeyboardLayoutInfo& layoutInfo) {
180 mConfig.keyboardLayoutAssociations.insert({inputUniqueId, layoutInfo});
181}
182
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000183void FakeInputReaderPolicy::addDisabledDevice(int32_t deviceId) {
184 mConfig.disabledDevices.insert(deviceId);
185}
186
187void FakeInputReaderPolicy::removeDisabledDevice(int32_t deviceId) {
188 mConfig.disabledDevices.erase(deviceId);
189}
190
Arpit Singhed6c3de2023-04-05 19:24:37 +0000191const InputReaderConfiguration& FakeInputReaderPolicy::getReaderConfiguration() const {
192 return mConfig;
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000193}
194
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700195const std::vector<InputDeviceInfo> FakeInputReaderPolicy::getInputDevices() const {
196 std::scoped_lock lock(mLock);
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000197 return mInputDevices;
198}
199
200TouchAffineTransformation FakeInputReaderPolicy::getTouchAffineTransformation(
Michael Wrighta9cf4192022-12-01 23:46:39 +0000201 const std::string& inputDeviceDescriptor, ui::Rotation surfaceRotation) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000202 return transform;
203}
204
205void FakeInputReaderPolicy::setTouchAffineTransformation(const TouchAffineTransformation t) {
206 transform = t;
207}
208
Hiroki Sato25040232024-02-22 17:21:22 +0900209PointerCaptureRequest FakeInputReaderPolicy::setPointerCapture(const sp<IBinder>& window) {
210 mConfig.pointerCaptureRequest = {window, mNextPointerCaptureSequenceNumber++};
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000211 return mConfig.pointerCaptureRequest;
212}
213
Linnan Li13bf76a2024-05-05 19:18:02 +0800214void FakeInputReaderPolicy::setDefaultPointerDisplayId(ui::LogicalDisplayId pointerDisplayId) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000215 mConfig.defaultPointerDisplayId = pointerDisplayId;
216}
217
218void FakeInputReaderPolicy::setPointerGestureEnabled(bool enabled) {
219 mConfig.pointerGesturesEnabled = enabled;
220}
221
222float FakeInputReaderPolicy::getPointerGestureMovementSpeedRatio() {
223 return mConfig.pointerGestureMovementSpeedRatio;
224}
225
226float FakeInputReaderPolicy::getPointerGestureZoomSpeedRatio() {
227 return mConfig.pointerGestureZoomSpeedRatio;
228}
229
230void FakeInputReaderPolicy::setVelocityControlParams(const VelocityControlParameters& params) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000231 mConfig.wheelVelocityControlParameters = params;
232}
233
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +0000234void FakeInputReaderPolicy::setStylusButtonMotionEventsEnabled(bool enabled) {
235 mConfig.stylusButtonMotionEventsEnabled = enabled;
236}
237
Seunghwan Choi356026c2023-02-01 14:37:25 +0900238void FakeInputReaderPolicy::setStylusPointerIconEnabled(bool enabled) {
239 mConfig.stylusPointerIconEnabled = enabled;
240}
241
Arpit Singhb3b3f732023-07-04 14:30:05 +0000242void FakeInputReaderPolicy::setIsInputMethodConnectionActive(bool active) {
243 mIsInputMethodConnectionActive = active;
244}
245
246bool FakeInputReaderPolicy::isInputMethodConnectionActive() {
247 return mIsInputMethodConnectionActive;
248}
249
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000250void FakeInputReaderPolicy::getReaderConfiguration(InputReaderConfiguration* outConfig) {
251 *outConfig = mConfig;
252}
253
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000254void FakeInputReaderPolicy::notifyInputDevicesChanged(
255 const std::vector<InputDeviceInfo>& inputDevices) {
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700256 std::scoped_lock lock(mLock);
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000257 mInputDevices = inputDevices;
258 mInputDevicesChanged = true;
259 mDevicesChangedCondition.notify_all();
260}
261
Abdelrahman Awadalla8c4160d2024-08-05 16:26:10 +0000262void FakeInputReaderPolicy::notifyTouchpadHardwareState(const SelfContainedHardwareState& schs,
263 int32_t deviceId) {
264 std::scoped_lock lock(mLock);
265 mTouchpadHardwareState = schs;
266 mTouchpadHardwareStateNotified.notify_all();
267}
268
Omar Abdelmonem5ebf21f2024-09-12 11:44:15 +0000269void FakeInputReaderPolicy::notifyTouchpadGestureInfo(GestureType type, int32_t deviceId) {
270 std::scoped_lock lock(mLock);
271}
272
Harry Cutts2a0210e2024-10-18 19:12:59 +0000273void FakeInputReaderPolicy::notifyTouchpadThreeFingerTap() {
274 std::scoped_lock lock(mLock);
275 mTouchpadThreeFingerTapHasBeenReported = true;
276 mTouchpadThreeFingerTapNotified.notify_all();
277}
278
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000279std::shared_ptr<KeyCharacterMap> FakeInputReaderPolicy::getKeyboardLayoutOverlay(
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000280 const InputDeviceIdentifier&, const std::optional<KeyboardLayoutInfo>) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000281 return nullptr;
282}
283
284std::string FakeInputReaderPolicy::getDeviceAlias(const InputDeviceIdentifier&) {
285 return "";
286}
287
Siarhei Vishniakoubfd75112024-09-04 00:29:42 +0000288void FakeInputReaderPolicy::waitForInputDevices(std::function<void(bool)> processDevicesChanged,
289 std::chrono::milliseconds timeout) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000290 std::unique_lock<std::mutex> lock(mLock);
291 base::ScopedLockAssertion assumeLocked(mLock);
292
293 const bool devicesChanged =
Siarhei Vishniakoubfd75112024-09-04 00:29:42 +0000294 mDevicesChangedCondition.wait_for(lock, timeout * HW_TIMEOUT_MULTIPLIER,
Arpit Singh42a145f2024-06-18 15:30:25 +0000295 [this]() REQUIRES(mLock) {
296 return mInputDevicesChanged;
297 });
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000298 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
299 mInputDevicesChanged = false;
300}
301
302void FakeInputReaderPolicy::notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) {
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700303 std::scoped_lock lock(mLock);
Prabir Pradhan40aee532024-02-08 00:47:23 +0000304 mDeviceIdOfNotifiedStylusGesture = deviceId;
305 mStylusGestureNotifiedCondition.notify_all();
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000306}
307
Prabir Pradhan19767602023-11-03 16:53:31 +0000308std::optional<DisplayViewport> FakeInputReaderPolicy::getPointerViewportForAssociatedDisplay(
Linnan Li13bf76a2024-05-05 19:18:02 +0800309 ui::LogicalDisplayId associatedDisplayId) {
310 if (!associatedDisplayId.isValid()) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900311 associatedDisplayId = mConfig.defaultPointerDisplayId;
312 }
313 for (auto& viewport : mViewports) {
314 if (viewport.displayId == associatedDisplayId) {
315 return std::make_optional(viewport);
316 }
317 }
318 return std::nullopt;
319}
320
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000321} // namespace android