Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 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 | |
Arpit Singh | 42a145f | 2024-06-18 15:30:25 +0000 | [diff] [blame] | 19 | #include <android-base/properties.h> |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 20 | #include <android-base/thread_annotations.h> |
| 21 | #include <gtest/gtest.h> |
| 22 | |
| 23 | #include "TestConstants.h" |
Michael Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame] | 24 | #include "ui/Rotation.h" |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
Arpit Singh | 42a145f | 2024-06-18 15:30:25 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | static const int HW_TIMEOUT_MULTIPLIER = base::GetIntProperty("ro.hw_timeout_multiplier", 1); |
| 31 | |
| 32 | } // namespace |
| 33 | |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 34 | void FakeInputReaderPolicy::assertInputDevicesChanged() { |
| 35 | waitForInputDevices([](bool devicesChanged) { |
| 36 | if (!devicesChanged) { |
| 37 | FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called."; |
| 38 | } |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | void FakeInputReaderPolicy::assertInputDevicesNotChanged() { |
| 43 | waitForInputDevices([](bool devicesChanged) { |
| 44 | if (devicesChanged) { |
| 45 | FAIL() << "Expected notifyInputDevicesChanged() to not be called."; |
| 46 | } |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | void FakeInputReaderPolicy::assertStylusGestureNotified(int32_t deviceId) { |
Prabir Pradhan | 40aee53 | 2024-02-08 00:47:23 +0000 | [diff] [blame] | 51 | 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 Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void FakeInputReaderPolicy::assertStylusGestureNotNotified() { |
| 64 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 40aee53 | 2024-02-08 00:47:23 +0000 | [diff] [blame] | 65 | ASSERT_FALSE(mDeviceIdOfNotifiedStylusGesture); |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Abdelrahman Awadalla | 8c4160d | 2024-08-05 16:26:10 +0000 | [diff] [blame^] | 68 | void FakeInputReaderPolicy::assertTouchpadHardwareStateNotified() { |
| 69 | std::unique_lock lock(mLock); |
| 70 | base::ScopedLockAssertion assumeLocked(mLock); |
| 71 | |
| 72 | const bool success = |
| 73 | mTouchpadHardwareStateNotified.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) { |
| 74 | return mTouchpadHardwareState.has_value(); |
| 75 | }); |
| 76 | ASSERT_TRUE(success) << "Timed out waiting for hardware state to be notified"; |
| 77 | } |
| 78 | |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 79 | void FakeInputReaderPolicy::clearViewports() { |
| 80 | mViewports.clear(); |
| 81 | mConfig.setDisplayViewports(mViewports); |
| 82 | } |
| 83 | |
| 84 | std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByUniqueId( |
| 85 | const std::string& uniqueId) const { |
| 86 | return mConfig.getDisplayViewportByUniqueId(uniqueId); |
| 87 | } |
| 88 | std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByType( |
| 89 | ViewportType type) const { |
| 90 | return mConfig.getDisplayViewportByType(type); |
| 91 | } |
| 92 | |
| 93 | std::optional<DisplayViewport> FakeInputReaderPolicy::getDisplayViewportByPort( |
| 94 | uint8_t displayPort) const { |
| 95 | return mConfig.getDisplayViewportByPort(displayPort); |
| 96 | } |
| 97 | |
| 98 | void FakeInputReaderPolicy::addDisplayViewport(DisplayViewport viewport) { |
| 99 | mViewports.push_back(std::move(viewport)); |
| 100 | mConfig.setDisplayViewports(mViewports); |
| 101 | } |
| 102 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 103 | void FakeInputReaderPolicy::addDisplayViewport(ui::LogicalDisplayId displayId, int32_t width, |
| 104 | int32_t height, ui::Rotation orientation, |
| 105 | bool isActive, const std::string& uniqueId, |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 106 | std::optional<uint8_t> physicalPort, |
| 107 | ViewportType type) { |
Michael Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame] | 108 | const bool isRotated = orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270; |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 109 | DisplayViewport v; |
| 110 | v.displayId = displayId; |
| 111 | v.orientation = orientation; |
| 112 | v.logicalLeft = 0; |
| 113 | v.logicalTop = 0; |
| 114 | v.logicalRight = isRotated ? height : width; |
| 115 | v.logicalBottom = isRotated ? width : height; |
| 116 | v.physicalLeft = 0; |
| 117 | v.physicalTop = 0; |
| 118 | v.physicalRight = isRotated ? height : width; |
| 119 | v.physicalBottom = isRotated ? width : height; |
| 120 | v.deviceWidth = isRotated ? height : width; |
| 121 | v.deviceHeight = isRotated ? width : height; |
| 122 | v.isActive = isActive; |
| 123 | v.uniqueId = uniqueId; |
| 124 | v.physicalPort = physicalPort; |
| 125 | v.type = type; |
| 126 | |
| 127 | addDisplayViewport(v); |
| 128 | } |
| 129 | |
| 130 | bool FakeInputReaderPolicy::updateViewport(const DisplayViewport& viewport) { |
| 131 | size_t count = mViewports.size(); |
| 132 | for (size_t i = 0; i < count; i++) { |
| 133 | const DisplayViewport& currentViewport = mViewports[i]; |
| 134 | if (currentViewport.displayId == viewport.displayId) { |
| 135 | mViewports[i] = viewport; |
| 136 | mConfig.setDisplayViewports(mViewports); |
| 137 | return true; |
| 138 | } |
| 139 | } |
| 140 | // no viewport found. |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | void FakeInputReaderPolicy::addExcludedDeviceName(const std::string& deviceName) { |
| 145 | mConfig.excludedDeviceNames.push_back(deviceName); |
| 146 | } |
| 147 | |
| 148 | void FakeInputReaderPolicy::addInputPortAssociation(const std::string& inputPort, |
| 149 | uint8_t displayPort) { |
Mayank Garg | 60b4562 | 2024-05-05 20:23:56 -0700 | [diff] [blame] | 150 | mConfig.inputPortToDisplayPortAssociations.insert({inputPort, displayPort}); |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Ambrus Weisz | 7bc23bf | 2022-10-04 13:13:07 +0000 | [diff] [blame] | 153 | void FakeInputReaderPolicy::addDeviceTypeAssociation(const std::string& inputPort, |
| 154 | const std::string& type) { |
| 155 | mConfig.deviceTypeAssociations.insert({inputPort, type}); |
| 156 | } |
| 157 | |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 158 | void FakeInputReaderPolicy::addInputUniqueIdAssociation(const std::string& inputUniqueId, |
| 159 | const std::string& displayUniqueId) { |
Mayank Garg | 60b4562 | 2024-05-05 20:23:56 -0700 | [diff] [blame] | 160 | mConfig.inputPortToDisplayUniqueIdAssociations.insert({inputUniqueId, displayUniqueId}); |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Zixuan Qu | fecb606 | 2022-11-12 04:44:31 +0000 | [diff] [blame] | 163 | void FakeInputReaderPolicy::addKeyboardLayoutAssociation(const std::string& inputUniqueId, |
| 164 | const KeyboardLayoutInfo& layoutInfo) { |
| 165 | mConfig.keyboardLayoutAssociations.insert({inputUniqueId, layoutInfo}); |
| 166 | } |
| 167 | |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 168 | void FakeInputReaderPolicy::addDisabledDevice(int32_t deviceId) { |
| 169 | mConfig.disabledDevices.insert(deviceId); |
| 170 | } |
| 171 | |
| 172 | void FakeInputReaderPolicy::removeDisabledDevice(int32_t deviceId) { |
| 173 | mConfig.disabledDevices.erase(deviceId); |
| 174 | } |
| 175 | |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 176 | const InputReaderConfiguration& FakeInputReaderPolicy::getReaderConfiguration() const { |
| 177 | return mConfig; |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Siarhei Vishniakou | 66b82f9 | 2023-08-16 14:42:06 -0700 | [diff] [blame] | 180 | const std::vector<InputDeviceInfo> FakeInputReaderPolicy::getInputDevices() const { |
| 181 | std::scoped_lock lock(mLock); |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 182 | return mInputDevices; |
| 183 | } |
| 184 | |
| 185 | TouchAffineTransformation FakeInputReaderPolicy::getTouchAffineTransformation( |
Michael Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame] | 186 | const std::string& inputDeviceDescriptor, ui::Rotation surfaceRotation) { |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 187 | return transform; |
| 188 | } |
| 189 | |
| 190 | void FakeInputReaderPolicy::setTouchAffineTransformation(const TouchAffineTransformation t) { |
| 191 | transform = t; |
| 192 | } |
| 193 | |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 194 | PointerCaptureRequest FakeInputReaderPolicy::setPointerCapture(const sp<IBinder>& window) { |
| 195 | mConfig.pointerCaptureRequest = {window, mNextPointerCaptureSequenceNumber++}; |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 196 | return mConfig.pointerCaptureRequest; |
| 197 | } |
| 198 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 199 | void FakeInputReaderPolicy::setDefaultPointerDisplayId(ui::LogicalDisplayId pointerDisplayId) { |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 200 | mConfig.defaultPointerDisplayId = pointerDisplayId; |
| 201 | } |
| 202 | |
| 203 | void FakeInputReaderPolicy::setPointerGestureEnabled(bool enabled) { |
| 204 | mConfig.pointerGesturesEnabled = enabled; |
| 205 | } |
| 206 | |
| 207 | float FakeInputReaderPolicy::getPointerGestureMovementSpeedRatio() { |
| 208 | return mConfig.pointerGestureMovementSpeedRatio; |
| 209 | } |
| 210 | |
| 211 | float FakeInputReaderPolicy::getPointerGestureZoomSpeedRatio() { |
| 212 | return mConfig.pointerGestureZoomSpeedRatio; |
| 213 | } |
| 214 | |
| 215 | void FakeInputReaderPolicy::setVelocityControlParams(const VelocityControlParameters& params) { |
| 216 | mConfig.pointerVelocityControlParameters = params; |
| 217 | mConfig.wheelVelocityControlParameters = params; |
| 218 | } |
| 219 | |
Prabir Pradhan | 7aa7ff0 | 2022-12-21 21:05:38 +0000 | [diff] [blame] | 220 | void FakeInputReaderPolicy::setStylusButtonMotionEventsEnabled(bool enabled) { |
| 221 | mConfig.stylusButtonMotionEventsEnabled = enabled; |
| 222 | } |
| 223 | |
Seunghwan Choi | 356026c | 2023-02-01 14:37:25 +0900 | [diff] [blame] | 224 | void FakeInputReaderPolicy::setStylusPointerIconEnabled(bool enabled) { |
| 225 | mConfig.stylusPointerIconEnabled = enabled; |
| 226 | } |
| 227 | |
Arpit Singh | b3b3f73 | 2023-07-04 14:30:05 +0000 | [diff] [blame] | 228 | void FakeInputReaderPolicy::setIsInputMethodConnectionActive(bool active) { |
| 229 | mIsInputMethodConnectionActive = active; |
| 230 | } |
| 231 | |
| 232 | bool FakeInputReaderPolicy::isInputMethodConnectionActive() { |
| 233 | return mIsInputMethodConnectionActive; |
| 234 | } |
| 235 | |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 236 | void FakeInputReaderPolicy::getReaderConfiguration(InputReaderConfiguration* outConfig) { |
| 237 | *outConfig = mConfig; |
| 238 | } |
| 239 | |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 240 | void FakeInputReaderPolicy::notifyInputDevicesChanged( |
| 241 | const std::vector<InputDeviceInfo>& inputDevices) { |
Siarhei Vishniakou | 66b82f9 | 2023-08-16 14:42:06 -0700 | [diff] [blame] | 242 | std::scoped_lock lock(mLock); |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 243 | mInputDevices = inputDevices; |
| 244 | mInputDevicesChanged = true; |
| 245 | mDevicesChangedCondition.notify_all(); |
| 246 | } |
| 247 | |
Abdelrahman Awadalla | 8c4160d | 2024-08-05 16:26:10 +0000 | [diff] [blame^] | 248 | void FakeInputReaderPolicy::notifyTouchpadHardwareState(const SelfContainedHardwareState& schs, |
| 249 | int32_t deviceId) { |
| 250 | std::scoped_lock lock(mLock); |
| 251 | mTouchpadHardwareState = schs; |
| 252 | mTouchpadHardwareStateNotified.notify_all(); |
| 253 | } |
| 254 | |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 255 | std::shared_ptr<KeyCharacterMap> FakeInputReaderPolicy::getKeyboardLayoutOverlay( |
Vaibhav Devmurari | dec3080 | 2023-07-11 15:02:03 +0000 | [diff] [blame] | 256 | const InputDeviceIdentifier&, const std::optional<KeyboardLayoutInfo>) { |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 257 | return nullptr; |
| 258 | } |
| 259 | |
| 260 | std::string FakeInputReaderPolicy::getDeviceAlias(const InputDeviceIdentifier&) { |
| 261 | return ""; |
| 262 | } |
| 263 | |
| 264 | void FakeInputReaderPolicy::waitForInputDevices(std::function<void(bool)> processDevicesChanged) { |
| 265 | std::unique_lock<std::mutex> lock(mLock); |
| 266 | base::ScopedLockAssertion assumeLocked(mLock); |
| 267 | |
| 268 | const bool devicesChanged = |
Arpit Singh | 42a145f | 2024-06-18 15:30:25 +0000 | [diff] [blame] | 269 | mDevicesChangedCondition.wait_for(lock, |
| 270 | ADD_INPUT_DEVICE_TIMEOUT * HW_TIMEOUT_MULTIPLIER, |
| 271 | [this]() REQUIRES(mLock) { |
| 272 | return mInputDevicesChanged; |
| 273 | }); |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 274 | ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged)); |
| 275 | mInputDevicesChanged = false; |
| 276 | } |
| 277 | |
| 278 | void FakeInputReaderPolicy::notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) { |
Siarhei Vishniakou | 66b82f9 | 2023-08-16 14:42:06 -0700 | [diff] [blame] | 279 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 40aee53 | 2024-02-08 00:47:23 +0000 | [diff] [blame] | 280 | mDeviceIdOfNotifiedStylusGesture = deviceId; |
| 281 | mStylusGestureNotifiedCondition.notify_all(); |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 284 | std::optional<DisplayViewport> FakeInputReaderPolicy::getPointerViewportForAssociatedDisplay( |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 285 | ui::LogicalDisplayId associatedDisplayId) { |
| 286 | if (!associatedDisplayId.isValid()) { |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 287 | associatedDisplayId = mConfig.defaultPointerDisplayId; |
| 288 | } |
| 289 | for (auto& viewport : mViewports) { |
| 290 | if (viewport.displayId == associatedDisplayId) { |
| 291 | return std::make_optional(viewport); |
| 292 | } |
| 293 | } |
| 294 | return std::nullopt; |
| 295 | } |
| 296 | |
Harry Cutts | 6b5fbc5 | 2022-11-28 16:37:43 +0000 | [diff] [blame] | 297 | } // namespace android |