blob: d2cb0ac3dfdf068945fa1f753101f3c67cf8f688 [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
19#include <android-base/thread_annotations.h>
20#include <gtest/gtest.h>
21
22#include "TestConstants.h"
Michael Wrighta9cf4192022-12-01 23:46:39 +000023#include "ui/Rotation.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000024
25namespace android {
26
27void FakeInputReaderPolicy::assertInputDevicesChanged() {
28 waitForInputDevices([](bool devicesChanged) {
29 if (!devicesChanged) {
30 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
31 }
32 });
33}
34
35void FakeInputReaderPolicy::assertInputDevicesNotChanged() {
36 waitForInputDevices([](bool devicesChanged) {
37 if (devicesChanged) {
38 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
39 }
40 });
41}
42
43void FakeInputReaderPolicy::assertStylusGestureNotified(int32_t deviceId) {
Prabir Pradhan40aee532024-02-08 00:47:23 +000044 std::unique_lock lock(mLock);
45 base::ScopedLockAssertion assumeLocked(mLock);
46
47 const bool success =
48 mStylusGestureNotifiedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
49 return mDeviceIdOfNotifiedStylusGesture.has_value();
50 });
51 ASSERT_TRUE(success) << "Timed out waiting for stylus gesture to be notified";
52 ASSERT_EQ(deviceId, *mDeviceIdOfNotifiedStylusGesture);
53 mDeviceIdOfNotifiedStylusGesture.reset();
Harry Cutts6b5fbc52022-11-28 16:37:43 +000054}
55
56void FakeInputReaderPolicy::assertStylusGestureNotNotified() {
57 std::scoped_lock lock(mLock);
Prabir Pradhan40aee532024-02-08 00:47:23 +000058 ASSERT_FALSE(mDeviceIdOfNotifiedStylusGesture);
Harry Cutts6b5fbc52022-11-28 16:37:43 +000059}
60
61void FakeInputReaderPolicy::clearViewports() {
62 mViewports.clear();
63 mConfig.setDisplayViewports(mViewports);
64}
65
66std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByUniqueId(
67 const std::string& uniqueId) const {
68 return mConfig.getDisplayViewportByUniqueId(uniqueId);
69}
70std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByType(
71 ViewportType type) const {
72 return mConfig.getDisplayViewportByType(type);
73}
74
75std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByPort(
76 uint8_t displayPort) const {
77 return mConfig.getDisplayViewportByPort(displayPort);
78}
79
80void FakeInputReaderPolicy::addDisplayViewport(DisplayViewport viewport) {
81 mViewports.push_back(std::move(viewport));
82 mConfig.setDisplayViewports(mViewports);
83}
84
Linnan Li13bf76a2024-05-05 19:18:02 +080085void FakeInputReaderPolicy::addDisplayViewport(ui::LogicalDisplayId displayId, int32_t width,
86 int32_t height, ui::Rotation orientation,
87 bool isActive, const std::string& uniqueId,
Harry Cutts6b5fbc52022-11-28 16:37:43 +000088 std::optional<uint8_t> physicalPort,
89 ViewportType type) {
Michael Wrighta9cf4192022-12-01 23:46:39 +000090 const bool isRotated = orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270;
Harry Cutts6b5fbc52022-11-28 16:37:43 +000091 DisplayViewport v;
92 v.displayId = displayId;
93 v.orientation = orientation;
94 v.logicalLeft = 0;
95 v.logicalTop = 0;
96 v.logicalRight = isRotated ? height : width;
97 v.logicalBottom = isRotated ? width : height;
98 v.physicalLeft = 0;
99 v.physicalTop = 0;
100 v.physicalRight = isRotated ? height : width;
101 v.physicalBottom = isRotated ? width : height;
102 v.deviceWidth = isRotated ? height : width;
103 v.deviceHeight = isRotated ? width : height;
104 v.isActive = isActive;
105 v.uniqueId = uniqueId;
106 v.physicalPort = physicalPort;
107 v.type = type;
108
109 addDisplayViewport(v);
110}
111
112bool FakeInputReaderPolicy::updateViewport(const DisplayViewport& viewport) {
113 size_t count = mViewports.size();
114 for (size_t i = 0; i < count; i++) {
115 const DisplayViewport& currentViewport = mViewports[i];
116 if (currentViewport.displayId == viewport.displayId) {
117 mViewports[i] = viewport;
118 mConfig.setDisplayViewports(mViewports);
119 return true;
120 }
121 }
122 // no viewport found.
123 return false;
124}
125
126void FakeInputReaderPolicy::addExcludedDeviceName(const std::string& deviceName) {
127 mConfig.excludedDeviceNames.push_back(deviceName);
128}
129
130void FakeInputReaderPolicy::addInputPortAssociation(const std::string& inputPort,
131 uint8_t displayPort) {
Mayank Garg60b45622024-05-05 20:23:56 -0700132 mConfig.inputPortToDisplayPortAssociations.insert({inputPort, displayPort});
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000133}
134
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000135void FakeInputReaderPolicy::addDeviceTypeAssociation(const std::string& inputPort,
136 const std::string& type) {
137 mConfig.deviceTypeAssociations.insert({inputPort, type});
138}
139
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000140void FakeInputReaderPolicy::addInputUniqueIdAssociation(const std::string& inputUniqueId,
141 const std::string& displayUniqueId) {
Mayank Garg60b45622024-05-05 20:23:56 -0700142 mConfig.inputPortToDisplayUniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000143}
144
Zixuan Qufecb6062022-11-12 04:44:31 +0000145void FakeInputReaderPolicy::addKeyboardLayoutAssociation(const std::string& inputUniqueId,
146 const KeyboardLayoutInfo& layoutInfo) {
147 mConfig.keyboardLayoutAssociations.insert({inputUniqueId, layoutInfo});
148}
149
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000150void FakeInputReaderPolicy::addDisabledDevice(int32_t deviceId) {
151 mConfig.disabledDevices.insert(deviceId);
152}
153
154void FakeInputReaderPolicy::removeDisabledDevice(int32_t deviceId) {
155 mConfig.disabledDevices.erase(deviceId);
156}
157
Arpit Singhed6c3de2023-04-05 19:24:37 +0000158const InputReaderConfiguration& FakeInputReaderPolicy::getReaderConfiguration() const {
159 return mConfig;
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000160}
161
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700162const std::vector<InputDeviceInfo> FakeInputReaderPolicy::getInputDevices() const {
163 std::scoped_lock lock(mLock);
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000164 return mInputDevices;
165}
166
167TouchAffineTransformation FakeInputReaderPolicy::getTouchAffineTransformation(
Michael Wrighta9cf4192022-12-01 23:46:39 +0000168 const std::string& inputDeviceDescriptor, ui::Rotation surfaceRotation) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000169 return transform;
170}
171
172void FakeInputReaderPolicy::setTouchAffineTransformation(const TouchAffineTransformation t) {
173 transform = t;
174}
175
Hiroki Sato25040232024-02-22 17:21:22 +0900176PointerCaptureRequest FakeInputReaderPolicy::setPointerCapture(const sp<IBinder>& window) {
177 mConfig.pointerCaptureRequest = {window, mNextPointerCaptureSequenceNumber++};
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000178 return mConfig.pointerCaptureRequest;
179}
180
Linnan Li13bf76a2024-05-05 19:18:02 +0800181void FakeInputReaderPolicy::setDefaultPointerDisplayId(ui::LogicalDisplayId pointerDisplayId) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000182 mConfig.defaultPointerDisplayId = pointerDisplayId;
183}
184
185void FakeInputReaderPolicy::setPointerGestureEnabled(bool enabled) {
186 mConfig.pointerGesturesEnabled = enabled;
187}
188
189float FakeInputReaderPolicy::getPointerGestureMovementSpeedRatio() {
190 return mConfig.pointerGestureMovementSpeedRatio;
191}
192
193float FakeInputReaderPolicy::getPointerGestureZoomSpeedRatio() {
194 return mConfig.pointerGestureZoomSpeedRatio;
195}
196
197void FakeInputReaderPolicy::setVelocityControlParams(const VelocityControlParameters& params) {
198 mConfig.pointerVelocityControlParameters = params;
199 mConfig.wheelVelocityControlParameters = params;
200}
201
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +0000202void FakeInputReaderPolicy::setStylusButtonMotionEventsEnabled(bool enabled) {
203 mConfig.stylusButtonMotionEventsEnabled = enabled;
204}
205
Seunghwan Choi356026c2023-02-01 14:37:25 +0900206void FakeInputReaderPolicy::setStylusPointerIconEnabled(bool enabled) {
207 mConfig.stylusPointerIconEnabled = enabled;
208}
209
Arpit Singhb3b3f732023-07-04 14:30:05 +0000210void FakeInputReaderPolicy::setIsInputMethodConnectionActive(bool active) {
211 mIsInputMethodConnectionActive = active;
212}
213
214bool FakeInputReaderPolicy::isInputMethodConnectionActive() {
215 return mIsInputMethodConnectionActive;
216}
217
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000218void FakeInputReaderPolicy::getReaderConfiguration(InputReaderConfiguration* outConfig) {
219 *outConfig = mConfig;
220}
221
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000222void FakeInputReaderPolicy::notifyInputDevicesChanged(
223 const std::vector<InputDeviceInfo>& inputDevices) {
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700224 std::scoped_lock lock(mLock);
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000225 mInputDevices = inputDevices;
226 mInputDevicesChanged = true;
227 mDevicesChangedCondition.notify_all();
228}
229
230std::shared_ptr<KeyCharacterMap> FakeInputReaderPolicy::getKeyboardLayoutOverlay(
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000231 const InputDeviceIdentifier&, const std::optional<KeyboardLayoutInfo>) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000232 return nullptr;
233}
234
235std::string FakeInputReaderPolicy::getDeviceAlias(const InputDeviceIdentifier&) {
236 return "";
237}
238
239void FakeInputReaderPolicy::waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
240 std::unique_lock<std::mutex> lock(mLock);
241 base::ScopedLockAssertion assumeLocked(mLock);
242
243 const bool devicesChanged =
244 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
245 return mInputDevicesChanged;
246 });
247 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
248 mInputDevicesChanged = false;
249}
250
251void FakeInputReaderPolicy::notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) {
Siarhei Vishniakou66b82f92023-08-16 14:42:06 -0700252 std::scoped_lock lock(mLock);
Prabir Pradhan40aee532024-02-08 00:47:23 +0000253 mDeviceIdOfNotifiedStylusGesture = deviceId;
254 mStylusGestureNotifiedCondition.notify_all();
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000255}
256
Prabir Pradhan19767602023-11-03 16:53:31 +0000257std::optional<DisplayViewport> FakeInputReaderPolicy::getPointerViewportForAssociatedDisplay(
Linnan Li13bf76a2024-05-05 19:18:02 +0800258 ui::LogicalDisplayId associatedDisplayId) {
259 if (!associatedDisplayId.isValid()) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900260 associatedDisplayId = mConfig.defaultPointerDisplayId;
261 }
262 for (auto& viewport : mViewports) {
263 if (viewport.displayId == associatedDisplayId) {
264 return std::make_optional(viewport);
265 }
266 }
267 return std::nullopt;
268}
269
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000270} // namespace android