blob: f373cac085bee58013bdd39196b2785d551a39c9 [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 Cutts6b5fbc52022-11-28 16:37:43 +000083void FakeInputReaderPolicy::clearViewports() {
84 mViewports.clear();
85 mConfig.setDisplayViewports(mViewports);
86}
87
88std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByUniqueId(
89 const std::string& uniqueId) const {
90 return mConfig.getDisplayViewportByUniqueId(uniqueId);
91}
92std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByType(
93 ViewportType type) const {
94 return mConfig.getDisplayViewportByType(type);
95}
96
97std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByPort(
98 uint8_t displayPort) const {
99 return mConfig.getDisplayViewportByPort(displayPort);
100}
101
102void FakeInputReaderPolicy::addDisplayViewport(DisplayViewport viewport) {
103 mViewports.push_back(std::move(viewport));
104 mConfig.setDisplayViewports(mViewports);
105}
106
Linnan Li13bf76a2024-05-05 19:18:02 +0800107void FakeInputReaderPolicy::addDisplayViewport(ui::LogicalDisplayId displayId, int32_t width,
108 int32_t height, ui::Rotation orientation,
109 bool isActive, const std::string& uniqueId,
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000110 std::optional<uint8_t> physicalPort,
111 ViewportType type) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000112 const bool isRotated = orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270;
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000113 DisplayViewport v;
114 v.displayId = displayId;
115 v.orientation = orientation;
116 v.logicalLeft = 0;
117 v.logicalTop = 0;
118 v.logicalRight = isRotated ? height : width;
119 v.logicalBottom = isRotated ? width : height;
120 v.physicalLeft = 0;
121 v.physicalTop = 0;
122 v.physicalRight = isRotated ? height : width;
123 v.physicalBottom = isRotated ? width : height;
124 v.deviceWidth = isRotated ? height : width;
125 v.deviceHeight = isRotated ? width : height;
126 v.isActive = isActive;
127 v.uniqueId = uniqueId;
128 v.physicalPort = physicalPort;
129 v.type = type;
130
131 addDisplayViewport(v);
132}
133
134bool FakeInputReaderPolicy::updateViewport(const DisplayViewport& viewport) {
135 size_t count = mViewports.size();
136 for (size_t i = 0; i < count; i++) {
137 const DisplayViewport& currentViewport = mViewports[i];
138 if (currentViewport.displayId == viewport.displayId) {
139 mViewports[i] = viewport;
140 mConfig.setDisplayViewports(mViewports);
141 return true;
142 }
143 }
144 // no viewport found.
145 return false;
146}
147
148void FakeInputReaderPolicy::addExcludedDeviceName(const std::string& deviceName) {
149 mConfig.excludedDeviceNames.push_back(deviceName);
150}
151
152void FakeInputReaderPolicy::addInputPortAssociation(const std::string& inputPort,
153 uint8_t displayPort) {
Mayank Garg60b45622024-05-05 20:23:56 -0700154 mConfig.inputPortToDisplayPortAssociations.insert({inputPort, displayPort});
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000155}
156
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000157void FakeInputReaderPolicy::addDeviceTypeAssociation(const std::string& inputPort,
158 const std::string& type) {
159 mConfig.deviceTypeAssociations.insert({inputPort, type});
160}
161
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000162void FakeInputReaderPolicy::addInputUniqueIdAssociation(const std::string& inputUniqueId,
163 const std::string& displayUniqueId) {
Mayank Garg60b45622024-05-05 20:23:56 -0700164 mConfig.inputPortToDisplayUniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000165}
166
Zixuan Qufecb6062022-11-12 04:44:31 +0000167void FakeInputReaderPolicy::addKeyboardLayoutAssociation(const std::string& inputUniqueId,
168 const KeyboardLayoutInfo& layoutInfo) {
169 mConfig.keyboardLayoutAssociations.insert({inputUniqueId, layoutInfo});
170}
171
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000172void FakeInputReaderPolicy::addDisabledDevice(int32_t deviceId) {
173 mConfig.disabledDevices.insert(deviceId);
174}
175
176void FakeInputReaderPolicy::removeDisabledDevice(int32_t deviceId) {
177 mConfig.disabledDevices.erase(deviceId);
178}
179
Arpit Singhed6c3de2023-04-05 19:24:37 +0000180const InputReaderConfiguration& FakeInputReaderPolicy::getReaderConfiguration() const {
181 return mConfig;
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000182}
183
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700184const std::vector<InputDeviceInfo> FakeInputReaderPolicy::getInputDevices() const {
185 std::scoped_lock lock(mLock);
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000186 return mInputDevices;
187}
188
189TouchAffineTransformation FakeInputReaderPolicy::getTouchAffineTransformation(
Michael Wrighta9cf4192022-12-01 23:46:39 +0000190 const std::string& inputDeviceDescriptor, ui::Rotation surfaceRotation) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000191 return transform;
192}
193
194void FakeInputReaderPolicy::setTouchAffineTransformation(const TouchAffineTransformation t) {
195 transform = t;
196}
197
Hiroki Sato25040232024-02-22 17:21:22 +0900198PointerCaptureRequest FakeInputReaderPolicy::setPointerCapture(const sp<IBinder>& window) {
199 mConfig.pointerCaptureRequest = {window, mNextPointerCaptureSequenceNumber++};
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000200 return mConfig.pointerCaptureRequest;
201}
202
Linnan Li13bf76a2024-05-05 19:18:02 +0800203void FakeInputReaderPolicy::setDefaultPointerDisplayId(ui::LogicalDisplayId pointerDisplayId) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000204 mConfig.defaultPointerDisplayId = pointerDisplayId;
205}
206
207void FakeInputReaderPolicy::setPointerGestureEnabled(bool enabled) {
208 mConfig.pointerGesturesEnabled = enabled;
209}
210
211float FakeInputReaderPolicy::getPointerGestureMovementSpeedRatio() {
212 return mConfig.pointerGestureMovementSpeedRatio;
213}
214
215float FakeInputReaderPolicy::getPointerGestureZoomSpeedRatio() {
216 return mConfig.pointerGestureZoomSpeedRatio;
217}
218
219void FakeInputReaderPolicy::setVelocityControlParams(const VelocityControlParameters& params) {
220 mConfig.pointerVelocityControlParameters = params;
221 mConfig.wheelVelocityControlParameters = params;
222}
223
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +0000224void FakeInputReaderPolicy::setStylusButtonMotionEventsEnabled(bool enabled) {
225 mConfig.stylusButtonMotionEventsEnabled = enabled;
226}
227
Seunghwan Choi356026c2023-02-01 14:37:25 +0900228void FakeInputReaderPolicy::setStylusPointerIconEnabled(bool enabled) {
229 mConfig.stylusPointerIconEnabled = enabled;
230}
231
Arpit Singhb3b3f732023-07-04 14:30:05 +0000232void FakeInputReaderPolicy::setIsInputMethodConnectionActive(bool active) {
233 mIsInputMethodConnectionActive = active;
234}
235
236bool FakeInputReaderPolicy::isInputMethodConnectionActive() {
237 return mIsInputMethodConnectionActive;
238}
239
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000240void FakeInputReaderPolicy::getReaderConfiguration(InputReaderConfiguration* outConfig) {
241 *outConfig = mConfig;
242}
243
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000244void FakeInputReaderPolicy::notifyInputDevicesChanged(
245 const std::vector<InputDeviceInfo>& inputDevices) {
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700246 std::scoped_lock lock(mLock);
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000247 mInputDevices = inputDevices;
248 mInputDevicesChanged = true;
249 mDevicesChangedCondition.notify_all();
250}
251
Abdelrahman Awadalla8c4160d2024-08-05 16:26:10 +0000252void FakeInputReaderPolicy::notifyTouchpadHardwareState(const SelfContainedHardwareState& schs,
253 int32_t deviceId) {
254 std::scoped_lock lock(mLock);
255 mTouchpadHardwareState = schs;
256 mTouchpadHardwareStateNotified.notify_all();
257}
258
Omar Abdelmonem5ebf21f2024-09-12 11:44:15 +0000259void FakeInputReaderPolicy::notifyTouchpadGestureInfo(GestureType type, int32_t deviceId) {
260 std::scoped_lock lock(mLock);
261}
262
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000263std::shared_ptr<KeyCharacterMap> FakeInputReaderPolicy::getKeyboardLayoutOverlay(
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000264 const InputDeviceIdentifier&, const std::optional<KeyboardLayoutInfo>) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000265 return nullptr;
266}
267
268std::string FakeInputReaderPolicy::getDeviceAlias(const InputDeviceIdentifier&) {
269 return "";
270}
271
Siarhei Vishniakoubfd75112024-09-04 00:29:42 +0000272void FakeInputReaderPolicy::waitForInputDevices(std::function<void(bool)> processDevicesChanged,
273 std::chrono::milliseconds timeout) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000274 std::unique_lock<std::mutex> lock(mLock);
275 base::ScopedLockAssertion assumeLocked(mLock);
276
277 const bool devicesChanged =
Siarhei Vishniakoubfd75112024-09-04 00:29:42 +0000278 mDevicesChangedCondition.wait_for(lock, timeout * HW_TIMEOUT_MULTIPLIER,
Arpit Singh42a145f2024-06-18 15:30:25 +0000279 [this]() REQUIRES(mLock) {
280 return mInputDevicesChanged;
281 });
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000282 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
283 mInputDevicesChanged = false;
284}
285
286void FakeInputReaderPolicy::notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) {
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700287 std::scoped_lock lock(mLock);
Prabir Pradhan40aee532024-02-08 00:47:23 +0000288 mDeviceIdOfNotifiedStylusGesture = deviceId;
289 mStylusGestureNotifiedCondition.notify_all();
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000290}
291
Prabir Pradhan19767602023-11-03 16:53:31 +0000292std::optional<DisplayViewport> FakeInputReaderPolicy::getPointerViewportForAssociatedDisplay(
Linnan Li13bf76a2024-05-05 19:18:02 +0800293 ui::LogicalDisplayId associatedDisplayId) {
294 if (!associatedDisplayId.isValid()) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900295 associatedDisplayId = mConfig.defaultPointerDisplayId;
296 }
297 for (auto& viewport : mViewports) {
298 if (viewport.displayId == associatedDisplayId) {
299 return std::make_optional(viewport);
300 }
301 }
302 return std::nullopt;
303}
304
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000305} // namespace android