• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
23 #include "ui/Rotation.h"
24 
25 namespace android {
26 
assertInputDevicesChanged()27 void FakeInputReaderPolicy::assertInputDevicesChanged() {
28     waitForInputDevices([](bool devicesChanged) {
29         if (!devicesChanged) {
30             FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
31         }
32     });
33 }
34 
assertInputDevicesNotChanged()35 void FakeInputReaderPolicy::assertInputDevicesNotChanged() {
36     waitForInputDevices([](bool devicesChanged) {
37         if (devicesChanged) {
38             FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
39         }
40     });
41 }
42 
assertStylusGestureNotified(int32_t deviceId)43 void 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 
assertStylusGestureNotNotified()50 void FakeInputReaderPolicy::assertStylusGestureNotNotified() {
51     std::scoped_lock lock(mLock);
52     ASSERT_FALSE(mStylusGestureNotified);
53 }
54 
clearViewports()55 void FakeInputReaderPolicy::clearViewports() {
56     mViewports.clear();
57     mConfig.setDisplayViewports(mViewports);
58 }
59 
getDisplayViewportByUniqueId(const std::string & uniqueId) const60 std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByUniqueId(
61         const std::string& uniqueId) const {
62     return mConfig.getDisplayViewportByUniqueId(uniqueId);
63 }
getDisplayViewportByType(ViewportType type) const64 std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByType(
65         ViewportType type) const {
66     return mConfig.getDisplayViewportByType(type);
67 }
68 
getDisplayViewportByPort(uint8_t displayPort) const69 std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByPort(
70         uint8_t displayPort) const {
71     return mConfig.getDisplayViewportByPort(displayPort);
72 }
73 
addDisplayViewport(DisplayViewport viewport)74 void FakeInputReaderPolicy::addDisplayViewport(DisplayViewport viewport) {
75     mViewports.push_back(std::move(viewport));
76     mConfig.setDisplayViewports(mViewports);
77 }
78 
addDisplayViewport(int32_t displayId,int32_t width,int32_t height,ui::Rotation orientation,bool isActive,const std::string & uniqueId,std::optional<uint8_t> physicalPort,ViewportType type)79 void FakeInputReaderPolicy::addDisplayViewport(int32_t displayId, int32_t width, int32_t height,
80                                                ui::Rotation orientation, bool isActive,
81                                                const std::string& uniqueId,
82                                                std::optional<uint8_t> physicalPort,
83                                                ViewportType type) {
84     const bool isRotated = orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270;
85     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 
updateViewport(const DisplayViewport & viewport)106 bool 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 
addExcludedDeviceName(const std::string & deviceName)120 void FakeInputReaderPolicy::addExcludedDeviceName(const std::string& deviceName) {
121     mConfig.excludedDeviceNames.push_back(deviceName);
122 }
123 
addInputPortAssociation(const std::string & inputPort,uint8_t displayPort)124 void FakeInputReaderPolicy::addInputPortAssociation(const std::string& inputPort,
125                                                     uint8_t displayPort) {
126     mConfig.portAssociations.insert({inputPort, displayPort});
127 }
128 
addDeviceTypeAssociation(const std::string & inputPort,const std::string & type)129 void FakeInputReaderPolicy::addDeviceTypeAssociation(const std::string& inputPort,
130                                                      const std::string& type) {
131     mConfig.deviceTypeAssociations.insert({inputPort, type});
132 }
133 
addInputUniqueIdAssociation(const std::string & inputUniqueId,const std::string & displayUniqueId)134 void FakeInputReaderPolicy::addInputUniqueIdAssociation(const std::string& inputUniqueId,
135                                                         const std::string& displayUniqueId) {
136     mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
137 }
138 
addKeyboardLayoutAssociation(const std::string & inputUniqueId,const KeyboardLayoutInfo & layoutInfo)139 void FakeInputReaderPolicy::addKeyboardLayoutAssociation(const std::string& inputUniqueId,
140                                                          const KeyboardLayoutInfo& layoutInfo) {
141     mConfig.keyboardLayoutAssociations.insert({inputUniqueId, layoutInfo});
142 }
143 
addDisabledDevice(int32_t deviceId)144 void FakeInputReaderPolicy::addDisabledDevice(int32_t deviceId) {
145     mConfig.disabledDevices.insert(deviceId);
146 }
147 
removeDisabledDevice(int32_t deviceId)148 void FakeInputReaderPolicy::removeDisabledDevice(int32_t deviceId) {
149     mConfig.disabledDevices.erase(deviceId);
150 }
151 
setPointerController(std::shared_ptr<FakePointerController> controller)152 void FakeInputReaderPolicy::setPointerController(
153         std::shared_ptr<FakePointerController> controller) {
154     mPointerController = std::move(controller);
155 }
156 
getReaderConfiguration() const157 const InputReaderConfiguration& FakeInputReaderPolicy::getReaderConfiguration() const {
158     return mConfig;
159 }
160 
getInputDevices() const161 const std::vector<InputDeviceInfo>& FakeInputReaderPolicy::getInputDevices() const {
162     return mInputDevices;
163 }
164 
getTouchAffineTransformation(const std::string & inputDeviceDescriptor,ui::Rotation surfaceRotation)165 TouchAffineTransformation FakeInputReaderPolicy::getTouchAffineTransformation(
166         const std::string& inputDeviceDescriptor, ui::Rotation surfaceRotation) {
167     return transform;
168 }
169 
setTouchAffineTransformation(const TouchAffineTransformation t)170 void FakeInputReaderPolicy::setTouchAffineTransformation(const TouchAffineTransformation t) {
171     transform = t;
172 }
173 
setPointerCapture(bool enabled)174 PointerCaptureRequest FakeInputReaderPolicy::setPointerCapture(bool enabled) {
175     mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
176     return mConfig.pointerCaptureRequest;
177 }
178 
setShowTouches(bool enabled)179 void FakeInputReaderPolicy::setShowTouches(bool enabled) {
180     mConfig.showTouches = enabled;
181 }
182 
setDefaultPointerDisplayId(int32_t pointerDisplayId)183 void FakeInputReaderPolicy::setDefaultPointerDisplayId(int32_t pointerDisplayId) {
184     mConfig.defaultPointerDisplayId = pointerDisplayId;
185 }
186 
setPointerGestureEnabled(bool enabled)187 void FakeInputReaderPolicy::setPointerGestureEnabled(bool enabled) {
188     mConfig.pointerGesturesEnabled = enabled;
189 }
190 
getPointerGestureMovementSpeedRatio()191 float FakeInputReaderPolicy::getPointerGestureMovementSpeedRatio() {
192     return mConfig.pointerGestureMovementSpeedRatio;
193 }
194 
getPointerGestureZoomSpeedRatio()195 float FakeInputReaderPolicy::getPointerGestureZoomSpeedRatio() {
196     return mConfig.pointerGestureZoomSpeedRatio;
197 }
198 
setVelocityControlParams(const VelocityControlParameters & params)199 void FakeInputReaderPolicy::setVelocityControlParams(const VelocityControlParameters& params) {
200     mConfig.pointerVelocityControlParameters = params;
201     mConfig.wheelVelocityControlParameters = params;
202 }
203 
setStylusButtonMotionEventsEnabled(bool enabled)204 void FakeInputReaderPolicy::setStylusButtonMotionEventsEnabled(bool enabled) {
205     mConfig.stylusButtonMotionEventsEnabled = enabled;
206 }
207 
setStylusPointerIconEnabled(bool enabled)208 void FakeInputReaderPolicy::setStylusPointerIconEnabled(bool enabled) {
209     mConfig.stylusPointerIconEnabled = enabled;
210 }
211 
setIsInputMethodConnectionActive(bool active)212 void FakeInputReaderPolicy::setIsInputMethodConnectionActive(bool active) {
213     mIsInputMethodConnectionActive = active;
214 }
215 
isInputMethodConnectionActive()216 bool FakeInputReaderPolicy::isInputMethodConnectionActive() {
217     return mIsInputMethodConnectionActive;
218 }
219 
getReaderConfiguration(InputReaderConfiguration * outConfig)220 void FakeInputReaderPolicy::getReaderConfiguration(InputReaderConfiguration* outConfig) {
221     *outConfig = mConfig;
222 }
223 
obtainPointerController(int32_t)224 std::shared_ptr<PointerControllerInterface> FakeInputReaderPolicy::obtainPointerController(
225         int32_t /*deviceId*/) {
226     return mPointerController;
227 }
228 
notifyInputDevicesChanged(const std::vector<InputDeviceInfo> & inputDevices)229 void FakeInputReaderPolicy::notifyInputDevicesChanged(
230         const std::vector<InputDeviceInfo>& inputDevices) {
231     std::scoped_lock<std::mutex> lock(mLock);
232     mInputDevices = inputDevices;
233     mInputDevicesChanged = true;
234     mDevicesChangedCondition.notify_all();
235 }
236 
getKeyboardLayoutOverlay(const InputDeviceIdentifier &)237 std::shared_ptr<KeyCharacterMap> FakeInputReaderPolicy::getKeyboardLayoutOverlay(
238         const InputDeviceIdentifier&) {
239     return nullptr;
240 }
241 
getDeviceAlias(const InputDeviceIdentifier &)242 std::string FakeInputReaderPolicy::getDeviceAlias(const InputDeviceIdentifier&) {
243     return "";
244 }
245 
waitForInputDevices(std::function<void (bool)> processDevicesChanged)246 void 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 =
251             mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
252                 return mInputDevicesChanged;
253             });
254     ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
255     mInputDevicesChanged = false;
256 }
257 
notifyStylusGestureStarted(int32_t deviceId,nsecs_t eventTime)258 void FakeInputReaderPolicy::notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) {
259     std::scoped_lock<std::mutex> lock(mLock);
260     mStylusGestureNotified = deviceId;
261 }
262 
263 } // namespace android
264