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