blob: 30c1719c358de6353ce29ebdd801b7e4a6c5bf17 [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) {
44 std::scoped_lock lock(mLock);
45 ASSERT_TRUE(mStylusGestureNotified);
46 ASSERT_EQ(deviceId, *mStylusGestureNotified);
47 mStylusGestureNotified.reset();
48}
49
50void FakeInputReaderPolicy::assertStylusGestureNotNotified() {
51 std::scoped_lock lock(mLock);
52 ASSERT_FALSE(mStylusGestureNotified);
53}
54
55void FakeInputReaderPolicy::clearViewports() {
56 mViewports.clear();
57 mConfig.setDisplayViewports(mViewports);
58}
59
60std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByUniqueId(
61 const std::string& uniqueId) const {
62 return mConfig.getDisplayViewportByUniqueId(uniqueId);
63}
64std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByType(
65 ViewportType type) const {
66 return mConfig.getDisplayViewportByType(type);
67}
68
69std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByPort(
70 uint8_t displayPort) const {
71 return mConfig.getDisplayViewportByPort(displayPort);
72}
73
74void FakeInputReaderPolicy::addDisplayViewport(DisplayViewport viewport) {
75 mViewports.push_back(std::move(viewport));
76 mConfig.setDisplayViewports(mViewports);
77}
78
79void FakeInputReaderPolicy::addDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +000080 ui::Rotation orientation, bool isActive,
Harry Cutts6b5fbc52022-11-28 16:37:43 +000081 const std::string& uniqueId,
82 std::optional<uint8_t> physicalPort,
83 ViewportType type) {
Michael Wrighta9cf4192022-12-01 23:46:39 +000084 const bool isRotated = orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270;
Harry Cutts6b5fbc52022-11-28 16:37:43 +000085 DisplayViewport v;
86 v.displayId = displayId;
87 v.orientation = orientation;
88 v.logicalLeft = 0;
89 v.logicalTop = 0;
90 v.logicalRight = isRotated ? height : width;
91 v.logicalBottom = isRotated ? width : height;
92 v.physicalLeft = 0;
93 v.physicalTop = 0;
94 v.physicalRight = isRotated ? height : width;
95 v.physicalBottom = isRotated ? width : height;
96 v.deviceWidth = isRotated ? height : width;
97 v.deviceHeight = isRotated ? width : height;
98 v.isActive = isActive;
99 v.uniqueId = uniqueId;
100 v.physicalPort = physicalPort;
101 v.type = type;
102
103 addDisplayViewport(v);
104}
105
106bool FakeInputReaderPolicy::updateViewport(const DisplayViewport& viewport) {
107 size_t count = mViewports.size();
108 for (size_t i = 0; i < count; i++) {
109 const DisplayViewport& currentViewport = mViewports[i];
110 if (currentViewport.displayId == viewport.displayId) {
111 mViewports[i] = viewport;
112 mConfig.setDisplayViewports(mViewports);
113 return true;
114 }
115 }
116 // no viewport found.
117 return false;
118}
119
120void FakeInputReaderPolicy::addExcludedDeviceName(const std::string& deviceName) {
121 mConfig.excludedDeviceNames.push_back(deviceName);
122}
123
124void FakeInputReaderPolicy::addInputPortAssociation(const std::string& inputPort,
125 uint8_t displayPort) {
126 mConfig.portAssociations.insert({inputPort, displayPort});
127}
128
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000129void FakeInputReaderPolicy::addDeviceTypeAssociation(const std::string& inputPort,
130 const std::string& type) {
131 mConfig.deviceTypeAssociations.insert({inputPort, type});
132}
133
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000134void FakeInputReaderPolicy::addInputUniqueIdAssociation(const std::string& inputUniqueId,
135 const std::string& displayUniqueId) {
136 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
137}
138
Zixuan Qufecb6062022-11-12 04:44:31 +0000139void FakeInputReaderPolicy::addKeyboardLayoutAssociation(const std::string& inputUniqueId,
140 const KeyboardLayoutInfo& layoutInfo) {
141 mConfig.keyboardLayoutAssociations.insert({inputUniqueId, layoutInfo});
142}
143
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000144void FakeInputReaderPolicy::addDisabledDevice(int32_t deviceId) {
145 mConfig.disabledDevices.insert(deviceId);
146}
147
148void FakeInputReaderPolicy::removeDisabledDevice(int32_t deviceId) {
149 mConfig.disabledDevices.erase(deviceId);
150}
151
152void FakeInputReaderPolicy::setPointerController(
153 std::shared_ptr<FakePointerController> controller) {
154 mPointerController = std::move(controller);
155}
156
157const InputReaderConfiguration* FakeInputReaderPolicy::getReaderConfiguration() const {
158 return &mConfig;
159}
160
161const std::vector<InputDeviceInfo>& FakeInputReaderPolicy::getInputDevices() const {
162 return mInputDevices;
163}
164
165TouchAffineTransformation FakeInputReaderPolicy::getTouchAffineTransformation(
Michael Wrighta9cf4192022-12-01 23:46:39 +0000166 const std::string& inputDeviceDescriptor, ui::Rotation surfaceRotation) {
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000167 return transform;
168}
169
170void FakeInputReaderPolicy::setTouchAffineTransformation(const TouchAffineTransformation t) {
171 transform = t;
172}
173
174PointerCaptureRequest FakeInputReaderPolicy::setPointerCapture(bool enabled) {
175 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
176 return mConfig.pointerCaptureRequest;
177}
178
179void FakeInputReaderPolicy::setShowTouches(bool enabled) {
180 mConfig.showTouches = enabled;
181}
182
183void FakeInputReaderPolicy::setDefaultPointerDisplayId(int32_t pointerDisplayId) {
184 mConfig.defaultPointerDisplayId = pointerDisplayId;
185}
186
187void FakeInputReaderPolicy::setPointerGestureEnabled(bool enabled) {
188 mConfig.pointerGesturesEnabled = enabled;
189}
190
191float FakeInputReaderPolicy::getPointerGestureMovementSpeedRatio() {
192 return mConfig.pointerGestureMovementSpeedRatio;
193}
194
195float FakeInputReaderPolicy::getPointerGestureZoomSpeedRatio() {
196 return mConfig.pointerGestureZoomSpeedRatio;
197}
198
199void FakeInputReaderPolicy::setVelocityControlParams(const VelocityControlParameters& params) {
200 mConfig.pointerVelocityControlParameters = params;
201 mConfig.wheelVelocityControlParameters = params;
202}
203
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +0000204void FakeInputReaderPolicy::setStylusButtonMotionEventsEnabled(bool enabled) {
205 mConfig.stylusButtonMotionEventsEnabled = enabled;
206}
207
Seunghwan Choi356026c2023-02-01 14:37:25 +0900208void FakeInputReaderPolicy::setStylusPointerIconEnabled(bool enabled) {
209 mConfig.stylusPointerIconEnabled = enabled;
210}
211
Harry Cutts6b5fbc52022-11-28 16:37:43 +0000212void FakeInputReaderPolicy::getReaderConfiguration(InputReaderConfiguration* outConfig) {
213 *outConfig = mConfig;
214}
215
216std::shared_ptr<PointerControllerInterface> FakeInputReaderPolicy::obtainPointerController(
217 int32_t /*deviceId*/) {
218 return mPointerController;
219}
220
221void FakeInputReaderPolicy::notifyInputDevicesChanged(
222 const std::vector<InputDeviceInfo>& inputDevices) {
223 std::scoped_lock<std::mutex> lock(mLock);
224 mInputDevices = inputDevices;
225 mInputDevicesChanged = true;
226 mDevicesChangedCondition.notify_all();
227}
228
229std::shared_ptr<KeyCharacterMap> FakeInputReaderPolicy::getKeyboardLayoutOverlay(
230 const InputDeviceIdentifier&) {
231 return nullptr;
232}
233
234std::string FakeInputReaderPolicy::getDeviceAlias(const InputDeviceIdentifier&) {
235 return "";
236}
237
238void FakeInputReaderPolicy::waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
239 std::unique_lock<std::mutex> lock(mLock);
240 base::ScopedLockAssertion assumeLocked(mLock);
241
242 const bool devicesChanged =
243 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
244 return mInputDevicesChanged;
245 });
246 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
247 mInputDevicesChanged = false;
248}
249
250void FakeInputReaderPolicy::notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) {
251 std::scoped_lock<std::mutex> lock(mLock);
252 mStylusGestureNotified = deviceId;
253}
254
255} // namespace android