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