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