blob: 6099c91dafb71610291aab8cccc768fac91cb58b [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
68void FakeInputReaderPolicy::clearViewports() {
69 mViewports.clear();
70 mConfig.setDisplayViewports(mViewports);
71}
72
73std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByUniqueId(
74 const std::string& uniqueId) const {
75 return mConfig.getDisplayViewportByUniqueId(uniqueId);
76}
77std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByType(
78 ViewportType type) const {
79 return mConfig.getDisplayViewportByType(type);
80}
81
82std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByPort(
83 uint8_t displayPort) const {
84 return mConfig.getDisplayViewportByPort(displayPort);
85}
86
87void FakeInputReaderPolicy::addDisplayViewport(DisplayViewport viewport) {
88 mViewports.push_back(std::move(viewport));
89 mConfig.setDisplayViewports(mViewports);
90}
91
Linnan Li13bf76a2024-05-05 19:18:02 +080092void FakeInputReaderPolicy::addDisplayViewport(ui::LogicalDisplayId displayId, int32_t width,
93 int32_t height, ui::Rotation orientation,
94 bool isActive, const std::string& uniqueId,
Harry Cutts6b5fbc52022-11-28 16:37:43 +000095 std::optional<uint8_t> physicalPort,
96 ViewportType type) {
Michael Wrighta9cf4192022-12-01 23:46:39 +000097 const bool isRotated = orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270;
Harry Cutts6b5fbc52022-11-28 16:37:43 +000098 DisplayViewport v;
99 v.displayId = displayId;
100 v.orientation = orientation;
101 v.logicalLeft = 0;
102 v.logicalTop = 0;
103 v.logicalRight = isRotated ? height : width;
104 v.logicalBottom = isRotated ? width : height;
105 v.physicalLeft = 0;
106 v.physicalTop = 0;
107 v.physicalRight = isRotated ? height : width;
108 v.physicalBottom = isRotated ? width : height;
109 v.deviceWidth = isRotated ? height : width;
110 v.deviceHeight = isRotated ? width : height;
111 v.isActive = isActive;
112 v.uniqueId = uniqueId;
113 v.physicalPort = physicalPort;
114 v.type = type;
115
116 addDisplayViewport(v);
117}
118
119bool FakeInputReaderPolicy::updateViewport(const DisplayViewport& viewport) {
120 size_t count = mViewports.size();
121 for (size_t i = 0; i < count; i++) {
122 const DisplayViewport& currentViewport = mViewports[i];
123 if (currentViewport.displayId == viewport.displayId) {
124 mViewports[i] = viewport;
125 mConfig.setDisplayViewports(mViewports);
126 return true;
127 }
128 }
129 // no viewport found.
130 return false;
131}
132
133void FakeInputReaderPolicy::addExcludedDeviceName(const std::string& deviceName) {
134 mConfig.excludedDeviceNames.push_back(deviceName);
135}
136
137void FakeInputReaderPolicy::addInputPortAssociation(const std::string& inputPort,
138 uint8_t displayPort) {
Mayank Garg60b45622024-05-05 20:23:56 -0700139 mConfig.inputPortToDisplayPortAssociations.insert({inputPort, displayPort});
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000140}
141
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000142void FakeInputReaderPolicy::addDeviceTypeAssociation(const std::string& inputPort,
143 const std::string& type) {
144 mConfig.deviceTypeAssociations.insert({inputPort, type});
145}
146
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000147void FakeInputReaderPolicy::addInputUniqueIdAssociation(const std::string& inputUniqueId,
148 const std::string& displayUniqueId) {
Mayank Garg60b45622024-05-05 20:23:56 -0700149 mConfig.inputPortToDisplayUniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000150}
151
Zixuan Qufecb6062022-11-12 04:44:31 +0000152void FakeInputReaderPolicy::addKeyboardLayoutAssociation(const std::string& inputUniqueId,
153 const KeyboardLayoutInfo& layoutInfo) {
154 mConfig.keyboardLayoutAssociations.insert({inputUniqueId, layoutInfo});
155}
156
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000157void FakeInputReaderPolicy::addDisabledDevice(int32_t deviceId) {
158 mConfig.disabledDevices.insert(deviceId);
159}
160
161void FakeInputReaderPolicy::removeDisabledDevice(int32_t deviceId) {
162 mConfig.disabledDevices.erase(deviceId);
163}
164
Arpit Singhed6c3de2023-04-05 19:24:37 +0000165const InputReaderConfiguration& FakeInputReaderPolicy::getReaderConfiguration() const {
166 return mConfig;
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000167}
168
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700169const std::vector<InputDeviceInfo> FakeInputReaderPolicy::getInputDevices() const {
170 std::scoped_lock lock(mLock);
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000171 return mInputDevices;
172}
173
174TouchAffineTransformation FakeInputReaderPolicy::getTouchAffineTransformation(
Michael Wrighta9cf4192022-12-01 23:46:39 +0000175 const std::string& inputDeviceDescriptor, ui::Rotation surfaceRotation) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000176 return transform;
177}
178
179void FakeInputReaderPolicy::setTouchAffineTransformation(const TouchAffineTransformation t) {
180 transform = t;
181}
182
Hiroki Sato25040232024-02-22 17:21:22 +0900183PointerCaptureRequest FakeInputReaderPolicy::setPointerCapture(const sp<IBinder>& window) {
184 mConfig.pointerCaptureRequest = {window, mNextPointerCaptureSequenceNumber++};
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000185 return mConfig.pointerCaptureRequest;
186}
187
Linnan Li13bf76a2024-05-05 19:18:02 +0800188void FakeInputReaderPolicy::setDefaultPointerDisplayId(ui::LogicalDisplayId pointerDisplayId) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000189 mConfig.defaultPointerDisplayId = pointerDisplayId;
190}
191
192void FakeInputReaderPolicy::setPointerGestureEnabled(bool enabled) {
193 mConfig.pointerGesturesEnabled = enabled;
194}
195
196float FakeInputReaderPolicy::getPointerGestureMovementSpeedRatio() {
197 return mConfig.pointerGestureMovementSpeedRatio;
198}
199
200float FakeInputReaderPolicy::getPointerGestureZoomSpeedRatio() {
201 return mConfig.pointerGestureZoomSpeedRatio;
202}
203
204void FakeInputReaderPolicy::setVelocityControlParams(const VelocityControlParameters& params) {
205 mConfig.pointerVelocityControlParameters = params;
206 mConfig.wheelVelocityControlParameters = params;
207}
208
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +0000209void FakeInputReaderPolicy::setStylusButtonMotionEventsEnabled(bool enabled) {
210 mConfig.stylusButtonMotionEventsEnabled = enabled;
211}
212
Seunghwan Choi356026c2023-02-01 14:37:25 +0900213void FakeInputReaderPolicy::setStylusPointerIconEnabled(bool enabled) {
214 mConfig.stylusPointerIconEnabled = enabled;
215}
216
Arpit Singhb3b3f732023-07-04 14:30:05 +0000217void FakeInputReaderPolicy::setIsInputMethodConnectionActive(bool active) {
218 mIsInputMethodConnectionActive = active;
219}
220
221bool FakeInputReaderPolicy::isInputMethodConnectionActive() {
222 return mIsInputMethodConnectionActive;
223}
224
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000225void FakeInputReaderPolicy::getReaderConfiguration(InputReaderConfiguration* outConfig) {
226 *outConfig = mConfig;
227}
228
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000229void FakeInputReaderPolicy::notifyInputDevicesChanged(
230 const std::vector<InputDeviceInfo>& inputDevices) {
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700231 std::scoped_lock lock(mLock);
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000232 mInputDevices = inputDevices;
233 mInputDevicesChanged = true;
234 mDevicesChangedCondition.notify_all();
235}
236
237std::shared_ptr<KeyCharacterMap> FakeInputReaderPolicy::getKeyboardLayoutOverlay(
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000238 const InputDeviceIdentifier&, const std::optional<KeyboardLayoutInfo>) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000239 return nullptr;
240}
241
242std::string FakeInputReaderPolicy::getDeviceAlias(const InputDeviceIdentifier&) {
243 return "";
244}
245
246void FakeInputReaderPolicy::waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
247 std::unique_lock<std::mutex> lock(mLock);
248 base::ScopedLockAssertion assumeLocked(mLock);
249
250 const bool devicesChanged =
Arpit Singh42a145f2024-06-18 15:30:25 +0000251 mDevicesChangedCondition.wait_for(lock,
252 ADD_INPUT_DEVICE_TIMEOUT * HW_TIMEOUT_MULTIPLIER,
253 [this]() REQUIRES(mLock) {
254 return mInputDevicesChanged;
255 });
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000256 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
257 mInputDevicesChanged = false;
258}
259
260void FakeInputReaderPolicy::notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) {
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700261 std::scoped_lock lock(mLock);
Prabir Pradhan40aee532024-02-08 00:47:23 +0000262 mDeviceIdOfNotifiedStylusGesture = deviceId;
263 mStylusGestureNotifiedCondition.notify_all();
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000264}
265
Prabir Pradhan19767602023-11-03 16:53:31 +0000266std::optional<DisplayViewport> FakeInputReaderPolicy::getPointerViewportForAssociatedDisplay(
Linnan Li13bf76a2024-05-05 19:18:02 +0800267 ui::LogicalDisplayId associatedDisplayId) {
268 if (!associatedDisplayId.isValid()) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900269 associatedDisplayId = mConfig.defaultPointerDisplayId;
270 }
271 for (auto& viewport : mViewports) {
272 if (viewport.displayId == associatedDisplayId) {
273 return std::make_optional(viewport);
274 }
275 }
276 return std::nullopt;
277}
278
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000279} // namespace android