Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2024 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 | #pragma once |
| 18 | |
| 19 | #include "../dispatcher/InputDispatcher.h" |
| 20 | #include "TestEventMatchers.h" |
| 21 | |
| 22 | #include <android-base/logging.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | #include <input/Input.h> |
| 25 | #include <input/InputConsumer.h> |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | /** |
| 30 | * If we expect to receive the event, the timeout can be made very long. When the test are running |
| 31 | * correctly, we will actually never wait until the end of the timeout because the wait will end |
| 32 | * when the event comes in. Still, this value shouldn't be infinite. During development, a local |
| 33 | * change may cause the test to fail. This timeout should be short enough to not annoy so that the |
| 34 | * developer can see the failure quickly (on human scale). |
| 35 | */ |
| 36 | static constexpr std::chrono::duration CONSUME_TIMEOUT_EVENT_EXPECTED = 1000ms; |
| 37 | |
| 38 | /** |
| 39 | * When no event is expected, we can have a very short timeout. A large value here would slow down |
| 40 | * the tests. In the unlikely event of system being too slow, the event may still be present but the |
| 41 | * timeout would complete before it is consumed. This would result in test flakiness. If this |
| 42 | * occurs, the flakiness rate would be high. Since the flakes are treated with high priority, this |
| 43 | * would get noticed and addressed quickly. |
| 44 | */ |
| 45 | static constexpr std::chrono::duration CONSUME_TIMEOUT_NO_EVENT_EXPECTED = 10ms; |
| 46 | |
| 47 | /** |
| 48 | * The default pid and uid for windows created on the primary display by the test. |
| 49 | */ |
| 50 | static constexpr gui::Pid WINDOW_PID{999}; |
| 51 | static constexpr gui::Uid WINDOW_UID{1001}; |
| 52 | |
| 53 | /** |
| 54 | * Default input dispatching timeout if there is no focused application or paused window |
| 55 | * from which to determine an appropriate dispatching timeout. |
| 56 | */ |
| 57 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 58 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 59 | android::base::HwTimeoutMultiplier()); |
| 60 | |
| 61 | // --- FakeInputReceiver --- |
| 62 | |
| 63 | class FakeInputReceiver { |
| 64 | public: |
| 65 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name); |
| 66 | |
| 67 | std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = false); |
| 68 | /** |
| 69 | * Receive an event without acknowledging it. |
| 70 | * Return the sequence number that could later be used to send finished signal. |
| 71 | */ |
| 72 | std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent( |
| 73 | std::chrono::milliseconds timeout); |
| 74 | /** |
| 75 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 76 | */ |
| 77 | void finishEvent(uint32_t consumeSeq, bool handled = true); |
| 78 | |
| 79 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline); |
| 80 | |
| 81 | void consumeEvent(android::InputEventType expectedEventType, int32_t expectedAction, |
| 82 | std::optional<int32_t> expectedDisplayId, |
| 83 | std::optional<int32_t> expectedFlags); |
| 84 | |
| 85 | std::unique_ptr<MotionEvent> consumeMotion(); |
| 86 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher); |
| 87 | |
| 88 | void consumeFocusEvent(bool hasFocus, bool inTouchMode); |
| 89 | void consumeCaptureEvent(bool hasCapture); |
| 90 | void consumeDragEvent(bool isExiting, float x, float y); |
| 91 | void consumeTouchModeEvent(bool inTouchMode); |
| 92 | |
| 93 | void assertNoEvents(std::chrono::milliseconds timeout); |
| 94 | |
| 95 | sp<IBinder> getToken(); |
| 96 | int getChannelFd(); |
| 97 | |
| 98 | private: |
| 99 | InputConsumer mConsumer; |
| 100 | DynamicInputEventFactory mEventFactory; |
| 101 | std::string mName; |
| 102 | }; |
| 103 | |
| 104 | // --- FakeWindowHandle --- |
| 105 | |
| 106 | class FakeWindowHandle : public gui::WindowInfoHandle { |
| 107 | public: |
| 108 | static const int32_t WIDTH = 600; |
| 109 | static const int32_t HEIGHT = 800; |
| 110 | using InputConfig = gui::WindowInfo::InputConfig; |
| 111 | |
| 112 | // This is a callback that is fired when an event is received by the window. |
| 113 | // It is static to avoid having to pass it individually into all of the FakeWindowHandles |
| 114 | // created by tests. |
| 115 | // TODO(b/210460522): Update the tests to use a factory pattern so that we can avoid |
| 116 | // the need to make this static. |
| 117 | static std::function<void(const std::unique_ptr<InputEvent>&, const gui::WindowInfo&)> |
| 118 | sOnEventReceivedCallback; |
| 119 | |
| 120 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
| 121 | const std::unique_ptr<inputdispatcher::InputDispatcher>& dispatcher, |
| 122 | const std::string name, int32_t displayId, bool createInputChannel = true); |
| 123 | |
| 124 | sp<FakeWindowHandle> clone(int32_t displayId); |
| 125 | |
| 126 | inline void setTouchable(bool touchable) { |
| 127 | mInfo.setInputConfig(InputConfig::NOT_TOUCHABLE, !touchable); |
| 128 | } |
| 129 | |
| 130 | inline void setFocusable(bool focusable) { |
| 131 | mInfo.setInputConfig(InputConfig::NOT_FOCUSABLE, !focusable); |
| 132 | } |
| 133 | |
| 134 | inline void setVisible(bool visible) { |
| 135 | mInfo.setInputConfig(InputConfig::NOT_VISIBLE, !visible); |
| 136 | } |
| 137 | |
| 138 | inline void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
| 139 | mInfo.dispatchingTimeout = timeout; |
| 140 | } |
| 141 | |
| 142 | inline void setPaused(bool paused) { |
| 143 | mInfo.setInputConfig(InputConfig::PAUSE_DISPATCHING, paused); |
| 144 | } |
| 145 | |
| 146 | inline void setPreventSplitting(bool preventSplitting) { |
| 147 | mInfo.setInputConfig(InputConfig::PREVENT_SPLITTING, preventSplitting); |
| 148 | } |
| 149 | |
| 150 | inline void setSlippery(bool slippery) { |
| 151 | mInfo.setInputConfig(InputConfig::SLIPPERY, slippery); |
| 152 | } |
| 153 | |
| 154 | inline void setWatchOutsideTouch(bool watchOutside) { |
| 155 | mInfo.setInputConfig(InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 156 | } |
| 157 | |
| 158 | inline void setSpy(bool spy) { mInfo.setInputConfig(InputConfig::SPY, spy); } |
| 159 | |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 160 | inline void setSecure(bool secure) { |
| 161 | if (secure) { |
| 162 | mInfo.layoutParamsFlags |= gui::WindowInfo::Flag::SECURE; |
| 163 | } else { |
| 164 | using namespace ftl::flag_operators; |
| 165 | mInfo.layoutParamsFlags &= ~gui::WindowInfo::Flag::SECURE; |
| 166 | } |
Arpit Singh | 490ccc9 | 2024-04-30 14:26:21 +0000 | [diff] [blame] | 167 | mInfo.setInputConfig(InputConfig::SENSITIVE_FOR_PRIVACY, secure); |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 170 | inline void setInterceptsStylus(bool interceptsStylus) { |
| 171 | mInfo.setInputConfig(InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 172 | } |
| 173 | |
| 174 | inline void setDropInput(bool dropInput) { |
| 175 | mInfo.setInputConfig(InputConfig::DROP_INPUT, dropInput); |
| 176 | } |
| 177 | |
| 178 | inline void setDropInputIfObscured(bool dropInputIfObscured) { |
| 179 | mInfo.setInputConfig(InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 180 | } |
| 181 | |
| 182 | inline void setNoInputChannel(bool noInputChannel) { |
| 183 | mInfo.setInputConfig(InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 184 | } |
| 185 | |
| 186 | inline void setDisableUserActivity(bool disableUserActivity) { |
| 187 | mInfo.setInputConfig(InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity); |
| 188 | } |
| 189 | |
| 190 | inline void setGlobalStylusBlocksTouch(bool shouldGlobalStylusBlockTouch) { |
| 191 | mInfo.setInputConfig(InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH, shouldGlobalStylusBlockTouch); |
| 192 | } |
| 193 | |
| 194 | inline void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 195 | |
| 196 | inline void setTouchOcclusionMode(gui::TouchOcclusionMode mode) { |
| 197 | mInfo.touchOcclusionMode = mode; |
| 198 | } |
| 199 | |
| 200 | inline void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 201 | |
| 202 | inline void setFrame(const Rect& frame, |
| 203 | const ui::Transform& displayTransform = ui::Transform()) { |
| 204 | mInfo.frame = frame; |
| 205 | mInfo.touchableRegion.clear(); |
| 206 | mInfo.addTouchableRegion(frame); |
| 207 | |
| 208 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 209 | ui::Transform translate; |
| 210 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 211 | mInfo.transform = translate * displayTransform; |
| 212 | } |
| 213 | |
| 214 | inline void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 215 | |
| 216 | inline void setIsWallpaper(bool isWallpaper) { |
| 217 | mInfo.setInputConfig(InputConfig::IS_WALLPAPER, isWallpaper); |
| 218 | } |
| 219 | |
| 220 | inline void setDupTouchToWallpaper(bool hasWallpaper) { |
| 221 | mInfo.setInputConfig(InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 222 | } |
| 223 | |
| 224 | inline void setTrustedOverlay(bool trustedOverlay) { |
| 225 | mInfo.setInputConfig(InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 226 | } |
| 227 | |
| 228 | inline void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 229 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 230 | } |
| 231 | |
| 232 | inline void setWindowScale(float xScale, float yScale) { |
| 233 | setWindowTransform(xScale, 0, 0, yScale); |
| 234 | } |
| 235 | |
| 236 | inline void setWindowOffset(float offsetX, float offsetY) { |
| 237 | mInfo.transform.set(offsetX, offsetY); |
| 238 | } |
| 239 | |
| 240 | std::unique_ptr<KeyEvent> consumeKey(bool handled = true); |
| 241 | |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 242 | inline std::unique_ptr<KeyEvent> consumeKeyEvent(const ::testing::Matcher<KeyEvent>& matcher) { |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 243 | std::unique_ptr<KeyEvent> keyEvent = consumeKey(); |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 244 | EXPECT_NE(nullptr, keyEvent); |
| 245 | if (!keyEvent) { |
| 246 | return nullptr; |
| 247 | } |
| 248 | EXPECT_THAT(*keyEvent, matcher); |
| 249 | return keyEvent; |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | inline void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 253 | consumeKeyEvent(testing::AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), |
| 254 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
| 255 | } |
| 256 | |
| 257 | inline void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 258 | consumeKeyEvent(testing::AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), |
| 259 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
| 260 | } |
| 261 | |
| 262 | inline void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 263 | int32_t expectedFlags = 0) { |
| 264 | consumeMotionEvent(testing::AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 265 | WithDisplayId(expectedDisplayId), |
| 266 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
| 267 | } |
| 268 | |
| 269 | inline void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 270 | int32_t expectedFlags = 0) { |
| 271 | consumeMotionEvent(testing::AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 272 | WithDisplayId(expectedDisplayId), |
| 273 | WithFlags(expectedFlags))); |
| 274 | } |
| 275 | |
| 276 | inline void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 277 | int32_t expectedFlags = 0) { |
| 278 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 279 | } |
| 280 | |
| 281 | inline void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 282 | std::optional<int32_t> expectedFlags = std::nullopt) { |
| 283 | consumeMotionEvent( |
| 284 | testing::AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 285 | testing::Conditional(expectedDisplayId.has_value(), |
| 286 | WithDisplayId(*expectedDisplayId), testing::_), |
| 287 | testing::Conditional(expectedFlags.has_value(), |
| 288 | WithFlags(*expectedFlags), testing::_))); |
| 289 | } |
| 290 | |
| 291 | inline void consumeMotionPointerDown(int32_t pointerIdx, |
| 292 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 293 | int32_t expectedFlags = 0) { |
| 294 | const int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 295 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 296 | consumeMotionEvent(testing::AllOf(WithMotionAction(action), |
| 297 | WithDisplayId(expectedDisplayId), |
| 298 | WithFlags(expectedFlags))); |
| 299 | } |
| 300 | |
| 301 | inline void consumeMotionPointerUp(int32_t pointerIdx, |
| 302 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 303 | int32_t expectedFlags = 0) { |
| 304 | const int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 305 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 306 | consumeMotionEvent(testing::AllOf(WithMotionAction(action), |
| 307 | WithDisplayId(expectedDisplayId), |
| 308 | WithFlags(expectedFlags))); |
| 309 | } |
| 310 | |
| 311 | inline void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 312 | int32_t expectedFlags = 0) { |
| 313 | consumeMotionEvent(testing::AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 314 | WithDisplayId(expectedDisplayId), |
| 315 | WithFlags(expectedFlags))); |
| 316 | } |
| 317 | |
| 318 | inline void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 319 | int32_t expectedFlags = 0) { |
| 320 | consumeMotionEvent(testing::AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE), |
| 321 | WithDisplayId(expectedDisplayId), |
| 322 | WithFlags(expectedFlags))); |
| 323 | } |
| 324 | |
| 325 | inline void consumeMotionOutsideWithZeroedCoords() { |
| 326 | consumeMotionEvent(testing::AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE), |
| 327 | WithRawCoords(0, 0))); |
| 328 | } |
| 329 | |
| 330 | inline void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 331 | ASSERT_NE(mInputReceiver, nullptr) |
| 332 | << "Cannot consume events from a window with no receiver"; |
| 333 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 334 | } |
| 335 | |
| 336 | inline void consumeCaptureEvent(bool hasCapture) { |
| 337 | ASSERT_NE(mInputReceiver, nullptr) |
| 338 | << "Cannot consume events from a window with no receiver"; |
| 339 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 340 | } |
| 341 | |
| 342 | std::unique_ptr<MotionEvent> consumeMotionEvent( |
| 343 | const ::testing::Matcher<MotionEvent>& matcher = testing::_); |
| 344 | |
| 345 | inline void consumeDragEvent(bool isExiting, float x, float y) { |
| 346 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 347 | } |
| 348 | |
| 349 | inline void consumeTouchModeEvent(bool inTouchMode) { |
| 350 | ASSERT_NE(mInputReceiver, nullptr) |
| 351 | << "Cannot consume events from a window with no receiver"; |
| 352 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 353 | } |
| 354 | |
| 355 | inline std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent() { |
| 356 | return receive(); |
| 357 | } |
| 358 | |
| 359 | inline void finishEvent(uint32_t sequenceNum) { |
| 360 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 361 | mInputReceiver->finishEvent(sequenceNum); |
| 362 | } |
| 363 | |
| 364 | inline void sendTimeline(int32_t inputEventId, |
| 365 | std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 366 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 367 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 368 | } |
| 369 | |
| 370 | void assertNoEvents(std::optional<std::chrono::milliseconds> timeout = {}); |
| 371 | |
| 372 | inline sp<IBinder> getToken() { return mInfo.token; } |
| 373 | |
| 374 | inline const std::string& getName() { return mName; } |
| 375 | |
| 376 | inline void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) { |
| 377 | mInfo.ownerPid = ownerPid; |
| 378 | mInfo.ownerUid = ownerUid; |
| 379 | } |
| 380 | |
| 381 | inline gui::Pid getPid() const { return mInfo.ownerPid; } |
| 382 | |
| 383 | inline void destroyReceiver() { mInputReceiver = nullptr; } |
| 384 | |
| 385 | inline int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 386 | |
| 387 | // FakeWindowHandle uses this consume method to ensure received events are added to the trace. |
| 388 | std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = true); |
| 389 | |
| 390 | private: |
| 391 | FakeWindowHandle(std::string name) : mName(name){}; |
| 392 | const std::string mName; |
| 393 | std::shared_ptr<FakeInputReceiver> mInputReceiver; |
| 394 | static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger |
| 395 | friend class sp<FakeWindowHandle>; |
| 396 | |
| 397 | // FakeWindowHandle uses this receive method to ensure received events are added to the trace. |
| 398 | std::pair<std::optional<uint32_t /*seq*/>, std::unique_ptr<InputEvent>> receive(); |
| 399 | }; |
| 400 | |
| 401 | } // namespace android |