Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 17 | #include "../dispatcher/InputDispatcher.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 18 | |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 19 | #include <android-base/properties.h> |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 20 | #include <android-base/silent_death_test.h> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 21 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 22 | #include <android-base/thread_annotations.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 23 | #include <binder/Binder.h> |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 24 | #include <fcntl.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 26 | #include <input/Input.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 27 | #include <linux/input.h> |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 28 | #include <sys/epoll.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 29 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 30 | #include <cinttypes> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 31 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 32 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 33 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 34 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 35 | using android::base::StringPrintf; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 36 | using android::gui::FocusRequest; |
| 37 | using android::gui::TouchOcclusionMode; |
| 38 | using android::gui::WindowInfo; |
| 39 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 40 | using android::os::InputEventInjectionResult; |
| 41 | using android::os::InputEventInjectionSync; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 42 | using namespace android::flag_operators; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 43 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 44 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 45 | |
| 46 | // An arbitrary time value. |
| 47 | static const nsecs_t ARBITRARY_TIME = 1234; |
| 48 | |
| 49 | // An arbitrary device id. |
| 50 | static const int32_t DEVICE_ID = 1; |
| 51 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 52 | // An arbitrary display id. |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 53 | static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
| 54 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 55 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 56 | // An arbitrary injector pid / uid pair that has permission to inject events. |
| 57 | static const int32_t INJECTOR_PID = 999; |
| 58 | static const int32_t INJECTOR_UID = 1001; |
| 59 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 60 | // An arbitrary pid of the gesture monitor window |
| 61 | static constexpr int32_t MONITOR_PID = 2001; |
| 62 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 63 | struct PointF { |
| 64 | float x; |
| 65 | float y; |
| 66 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 67 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 68 | /** |
| 69 | * Return a DOWN key event with KEYCODE_A. |
| 70 | */ |
| 71 | static KeyEvent getTestKeyEvent() { |
| 72 | KeyEvent event; |
| 73 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 74 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 75 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 76 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 77 | return event; |
| 78 | } |
| 79 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 80 | static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) { |
| 81 | ASSERT_EQ(expectedAction, receivedAction) |
| 82 | << "expected " << MotionEvent::actionToString(expectedAction) << ", got " |
| 83 | << MotionEvent::actionToString(receivedAction); |
| 84 | } |
| 85 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 86 | // --- FakeInputDispatcherPolicy --- |
| 87 | |
| 88 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 89 | InputDispatcherConfiguration mConfig; |
| 90 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 91 | using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>; |
| 92 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 93 | protected: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 94 | virtual ~FakeInputDispatcherPolicy() {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 95 | |
| 96 | public: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 97 | FakeInputDispatcherPolicy() {} |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 98 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 99 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 100 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
| 101 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_KEY); |
| 102 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 103 | |
| 104 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 105 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 106 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 107 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 108 | } |
| 109 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 110 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 111 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
| 112 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_MOTION); |
| 113 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 114 | |
| 115 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 116 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 117 | EXPECT_EQ(motionEvent.getAction(), args.action); |
| 118 | EXPECT_EQ(motionEvent.getX(0), point.x); |
| 119 | EXPECT_EQ(motionEvent.getY(0), point.y); |
| 120 | EXPECT_EQ(motionEvent.getRawX(0), point.x); |
| 121 | EXPECT_EQ(motionEvent.getRawY(0), point.y); |
| 122 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 123 | } |
| 124 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 125 | void assertFilterInputEventWasNotCalled() { |
| 126 | std::scoped_lock lock(mLock); |
| 127 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 128 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 129 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 130 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 131 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 132 | ASSERT_TRUE(mConfigurationChangedTime) |
| 133 | << "Timed out waiting for configuration changed call"; |
| 134 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 135 | mConfigurationChangedTime = std::nullopt; |
| 136 | } |
| 137 | |
| 138 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 139 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 140 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 141 | // We do not check id because it is not exposed to the policy |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 142 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 143 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 144 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 145 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 146 | mLastNotifySwitch = std::nullopt; |
| 147 | } |
| 148 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 149 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 150 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 151 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 152 | mOnPointerDownToken.clear(); |
| 153 | } |
| 154 | |
| 155 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 156 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 157 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 158 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 161 | // This function must be called soon after the expected ANR timer starts, |
| 162 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 163 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 164 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 165 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 166 | std::unique_lock lock(mLock); |
| 167 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 168 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 169 | ASSERT_NO_FATAL_FAILURE( |
| 170 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 171 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 174 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 175 | const sp<WindowInfoHandle>& window) { |
| 176 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 177 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 178 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 181 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 182 | const sp<IBinder>& expectedToken, |
| 183 | int32_t expectedPid) { |
| 184 | std::unique_lock lock(mLock); |
| 185 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 186 | AnrResult result; |
| 187 | ASSERT_NO_FATAL_FAILURE(result = |
| 188 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
| 189 | const auto& [token, pid] = result; |
| 190 | ASSERT_EQ(expectedToken, token); |
| 191 | ASSERT_EQ(expectedPid, pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 192 | } |
| 193 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 194 | /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 195 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 196 | std::unique_lock lock(mLock); |
| 197 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 198 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 199 | const auto& [token, _] = result; |
| 200 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 203 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
| 204 | int32_t expectedPid) { |
| 205 | std::unique_lock lock(mLock); |
| 206 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 207 | AnrResult result; |
| 208 | ASSERT_NO_FATAL_FAILURE( |
| 209 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
| 210 | const auto& [token, pid] = result; |
| 211 | ASSERT_EQ(expectedToken, token); |
| 212 | ASSERT_EQ(expectedPid, pid); |
| 213 | } |
| 214 | |
| 215 | /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 216 | sp<IBinder> getResponsiveWindowToken() { |
| 217 | std::unique_lock lock(mLock); |
| 218 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 219 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 220 | const auto& [token, _] = result; |
| 221 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void assertNotifyAnrWasNotCalled() { |
| 225 | std::scoped_lock lock(mLock); |
| 226 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 227 | ASSERT_TRUE(mAnrWindows.empty()); |
| 228 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 229 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 230 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 233 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 234 | mConfig.keyRepeatTimeout = timeout; |
| 235 | mConfig.keyRepeatDelay = delay; |
| 236 | } |
| 237 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 238 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 239 | std::unique_lock lock(mLock); |
| 240 | base::ScopedLockAssertion assumeLocked(mLock); |
| 241 | |
| 242 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 243 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 244 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 245 | enabled; |
| 246 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 247 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 248 | << ") to be called."; |
| 249 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 250 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 251 | auto request = *mPointerCaptureRequest; |
| 252 | mPointerCaptureRequest.reset(); |
| 253 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void assertSetPointerCaptureNotCalled() { |
| 257 | std::unique_lock lock(mLock); |
| 258 | base::ScopedLockAssertion assumeLocked(mLock); |
| 259 | |
| 260 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 261 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 262 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 263 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 264 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 265 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 266 | } |
| 267 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 268 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { |
| 269 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 270 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 271 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 272 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 273 | } |
| 274 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 275 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 276 | std::unique_lock lock(mLock); |
| 277 | base::ScopedLockAssertion assumeLocked(mLock); |
| 278 | std::optional<sp<IBinder>> receivedToken = |
| 279 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 280 | mNotifyInputChannelBroken); |
| 281 | ASSERT_TRUE(receivedToken.has_value()); |
| 282 | ASSERT_EQ(token, *receivedToken); |
| 283 | } |
| 284 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 285 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 286 | std::mutex mLock; |
| 287 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 288 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 289 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 290 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 291 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 292 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 293 | |
| 294 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 295 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 296 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 297 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 298 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 299 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 300 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 301 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 302 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 303 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 304 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 305 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 306 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 307 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 308 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 309 | // first entry from the container and erase it. |
| 310 | template <class T> |
| 311 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 312 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 313 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 314 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 315 | // before checking if ANR was called. |
| 316 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 317 | // to provide it some time to act. 100ms seems reasonable. |
| 318 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 319 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 320 | std::optional<T> token = |
| 321 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 322 | if (!token.has_value()) { |
| 323 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 324 | return {}; |
| 325 | } |
| 326 | |
| 327 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 328 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 329 | // the dispatcher started counting before this function was called |
| 330 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 331 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 332 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 333 | << "ms, but waited " |
| 334 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 335 | << "ms instead"; |
| 336 | } |
| 337 | return *token; |
| 338 | } |
| 339 | |
| 340 | template <class T> |
| 341 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 342 | std::queue<T>& storage, |
| 343 | std::unique_lock<std::mutex>& lock, |
| 344 | std::condition_variable& condition) |
| 345 | REQUIRES(mLock) { |
| 346 | condition.wait_for(lock, timeout, |
| 347 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 348 | if (storage.empty()) { |
| 349 | ADD_FAILURE() << "Did not receive the expected callback"; |
| 350 | return std::nullopt; |
| 351 | } |
| 352 | T item = storage.front(); |
| 353 | storage.pop(); |
| 354 | return std::make_optional(item); |
| 355 | } |
| 356 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 357 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 358 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 359 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 362 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid, |
| 363 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 364 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 365 | ASSERT_TRUE(pid.has_value()); |
| 366 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 367 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 368 | } |
| 369 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 370 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
| 371 | std::optional<int32_t> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 372 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 373 | ASSERT_TRUE(pid.has_value()); |
| 374 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 375 | mNotifyAnr.notify_all(); |
| 376 | } |
| 377 | |
| 378 | void notifyNoFocusedWindowAnr( |
| 379 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 380 | std::scoped_lock lock(mLock); |
| 381 | mAnrApplications.push(applicationHandle); |
| 382 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 385 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 386 | std::scoped_lock lock(mLock); |
| 387 | mBrokenInputChannels.push(connectionToken); |
| 388 | mNotifyInputChannelBroken.notify_all(); |
| 389 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 390 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 391 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 392 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 393 | void notifyUntrustedTouch(const std::string& obscuringPackage) override {} |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 394 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 395 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 396 | const std::vector<float>& values) override {} |
| 397 | |
| 398 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 399 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 400 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 401 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 402 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 403 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 404 | *outConfig = mConfig; |
| 405 | } |
| 406 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 407 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 408 | std::scoped_lock lock(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 409 | switch (inputEvent->getType()) { |
| 410 | case AINPUT_EVENT_TYPE_KEY: { |
| 411 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 412 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 413 | break; |
| 414 | } |
| 415 | |
| 416 | case AINPUT_EVENT_TYPE_MOTION: { |
| 417 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 418 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 419 | break; |
| 420 | } |
| 421 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 422 | return true; |
| 423 | } |
| 424 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 425 | void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 426 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 427 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 428 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 429 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 433 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 434 | return false; |
| 435 | } |
| 436 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 437 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 438 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 439 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 440 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 441 | * essentially a passthrough for notifySwitch. |
| 442 | */ |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 443 | mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 444 | } |
| 445 | |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 446 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 447 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 448 | bool checkInjectEventsPermissionNonReentrant(int32_t pid, int32_t uid) override { |
| 449 | return pid == INJECTOR_PID && uid == INJECTOR_UID; |
| 450 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 451 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 452 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 453 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 454 | mOnPointerDownToken = newToken; |
| 455 | } |
| 456 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 457 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 458 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 459 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 460 | mPointerCaptureChangedCondition.notify_all(); |
| 461 | } |
| 462 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 463 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 464 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 465 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 466 | mDropTargetWindowToken = token; |
| 467 | } |
| 468 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 469 | void assertFilterInputEventWasCalledInternal( |
| 470 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 471 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 472 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 473 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 474 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 475 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 476 | }; |
| 477 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 478 | // --- InputDispatcherTest --- |
| 479 | |
| 480 | class InputDispatcherTest : public testing::Test { |
| 481 | protected: |
| 482 | sp<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 483 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 484 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 485 | void SetUp() override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 486 | mFakePolicy = new FakeInputDispatcherPolicy(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 487 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 488 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 489 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 490 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 493 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 494 | ASSERT_EQ(OK, mDispatcher->stop()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 495 | mFakePolicy.clear(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 496 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 497 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 498 | |
| 499 | /** |
| 500 | * Used for debugging when writing the test |
| 501 | */ |
| 502 | void dumpDispatcherState() { |
| 503 | std::string dump; |
| 504 | mDispatcher->dump(dump); |
| 505 | std::stringstream ss(dump); |
| 506 | std::string to; |
| 507 | |
| 508 | while (std::getline(ss, to, '\n')) { |
| 509 | ALOGE("%s", to.c_str()); |
| 510 | } |
| 511 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 512 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 513 | void setFocusedWindow(const sp<WindowInfoHandle>& window, |
| 514 | const sp<WindowInfoHandle>& focusedWindow = nullptr) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 515 | FocusRequest request; |
| 516 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 517 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 518 | if (focusedWindow) { |
| 519 | request.focusedToken = focusedWindow->getToken(); |
| 520 | } |
| 521 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 522 | request.displayId = window->getInfo()->displayId; |
| 523 | mDispatcher->setFocusedWindow(request); |
| 524 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 525 | }; |
| 526 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 527 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 528 | KeyEvent event; |
| 529 | |
| 530 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 531 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 532 | INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 533 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 534 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 535 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 536 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 537 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 538 | << "Should reject key events with undefined action."; |
| 539 | |
| 540 | // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 541 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 542 | INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 543 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 544 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 545 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 546 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 547 | << "Should reject key events with ACTION_MULTIPLE."; |
| 548 | } |
| 549 | |
| 550 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 551 | MotionEvent event; |
| 552 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 553 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 554 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 555 | pointerProperties[i].clear(); |
| 556 | pointerProperties[i].id = i; |
| 557 | pointerCoords[i].clear(); |
| 558 | } |
| 559 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 560 | // Some constants commonly used below |
| 561 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 562 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 563 | constexpr int32_t metaState = AMETA_NONE; |
| 564 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 565 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 566 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 567 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 568 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 569 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, |
| 570 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 571 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 572 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 573 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 574 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 575 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 576 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 577 | << "Should reject motion events with undefined action."; |
| 578 | |
| 579 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 580 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 581 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 582 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 583 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 584 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 585 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 586 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 587 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 588 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 589 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 590 | << "Should reject motion events with pointer down index too large."; |
| 591 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 592 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 593 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 594 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 595 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 596 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 597 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 598 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 599 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 600 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 601 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 602 | << "Should reject motion events with pointer down index too small."; |
| 603 | |
| 604 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 605 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 606 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 607 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 608 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 609 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 610 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 611 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 612 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 613 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 614 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 615 | << "Should reject motion events with pointer up index too large."; |
| 616 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 617 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 618 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 619 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 620 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 621 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 622 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 623 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 624 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 625 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 626 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 627 | << "Should reject motion events with pointer up index too small."; |
| 628 | |
| 629 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 630 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 631 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 632 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 633 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 634 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 635 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 636 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 637 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 638 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 639 | << "Should reject motion events with 0 pointers."; |
| 640 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 641 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 642 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 643 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 644 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 645 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 646 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 647 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 648 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 649 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 650 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 651 | |
| 652 | // Rejects motion events with invalid pointer ids. |
| 653 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 654 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 655 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 656 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 657 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 658 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 659 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 660 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 661 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 662 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 663 | << "Should reject motion events with pointer ids less than 0."; |
| 664 | |
| 665 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 666 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 667 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 668 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 669 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 670 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 671 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 672 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 673 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 674 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 675 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 676 | |
| 677 | // Rejects motion events with duplicate pointer ids. |
| 678 | pointerProperties[0].id = 1; |
| 679 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 680 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 681 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 682 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 683 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 684 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 685 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 686 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 687 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 688 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 689 | << "Should reject motion events with duplicate pointer ids."; |
| 690 | } |
| 691 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 692 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 693 | |
| 694 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 695 | constexpr nsecs_t eventTime = 20; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 696 | NotifyConfigurationChangedArgs args(10 /*id*/, eventTime); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 697 | mDispatcher->notifyConfigurationChanged(&args); |
| 698 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 699 | |
| 700 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 701 | } |
| 702 | |
| 703 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 704 | NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/, |
| 705 | 2 /*switchMask*/); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 706 | mDispatcher->notifySwitch(&args); |
| 707 | |
| 708 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 709 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 710 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 711 | } |
| 712 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 713 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 714 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 715 | // Default input dispatching timeout if there is no focused application or paused window |
| 716 | // from which to determine an appropriate dispatching timeout. |
| 717 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 718 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 719 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 720 | |
| 721 | class FakeApplicationHandle : public InputApplicationHandle { |
| 722 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 723 | FakeApplicationHandle() { |
| 724 | mInfo.name = "Fake Application"; |
| 725 | mInfo.token = new BBinder(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 726 | mInfo.dispatchingTimeoutMillis = |
| 727 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 728 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 729 | virtual ~FakeApplicationHandle() {} |
| 730 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 731 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 732 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 733 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 734 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 735 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 736 | }; |
| 737 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 738 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 739 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 740 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 741 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 742 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 743 | } |
| 744 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 745 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 746 | InputEvent* event; |
| 747 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 748 | if (!consumeSeq) { |
| 749 | return nullptr; |
| 750 | } |
| 751 | finishEvent(*consumeSeq); |
| 752 | return event; |
| 753 | } |
| 754 | |
| 755 | /** |
| 756 | * Receive an event without acknowledging it. |
| 757 | * Return the sequence number that could later be used to send finished signal. |
| 758 | */ |
| 759 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 760 | uint32_t consumeSeq; |
| 761 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 762 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 763 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 764 | status_t status = WOULD_BLOCK; |
| 765 | while (status == WOULD_BLOCK) { |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 766 | status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 767 | &event); |
| 768 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 769 | if (elapsed > 100ms) { |
| 770 | break; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | if (status == WOULD_BLOCK) { |
| 775 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 776 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | if (status != OK) { |
| 780 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 781 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 782 | } |
| 783 | if (event == nullptr) { |
| 784 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 785 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 786 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 787 | if (outEvent != nullptr) { |
| 788 | *outEvent = event; |
| 789 | } |
| 790 | return consumeSeq; |
| 791 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 792 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 793 | /** |
| 794 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 795 | */ |
| 796 | void finishEvent(uint32_t consumeSeq) { |
| 797 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 798 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 799 | } |
| 800 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 801 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 802 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); |
| 803 | ASSERT_EQ(OK, status); |
| 804 | } |
| 805 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 806 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 807 | std::optional<int32_t> expectedDisplayId, |
| 808 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 809 | InputEvent* event = consume(); |
| 810 | |
| 811 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 812 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 813 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 814 | << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType) |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 815 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 816 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 817 | if (expectedDisplayId.has_value()) { |
| 818 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 819 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 820 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 821 | switch (expectedEventType) { |
| 822 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 823 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 824 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 825 | if (expectedFlags.has_value()) { |
| 826 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 827 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 828 | break; |
| 829 | } |
| 830 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 831 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 832 | assertMotionAction(expectedAction, motionEvent.getAction()); |
| 833 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 834 | if (expectedFlags.has_value()) { |
| 835 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 836 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 837 | break; |
| 838 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 839 | case AINPUT_EVENT_TYPE_FOCUS: { |
| 840 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 841 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 842 | case AINPUT_EVENT_TYPE_CAPTURE: { |
| 843 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 844 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 845 | case AINPUT_EVENT_TYPE_TOUCH_MODE: { |
| 846 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 847 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 848 | case AINPUT_EVENT_TYPE_DRAG: { |
| 849 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 850 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 851 | default: { |
| 852 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; |
| 853 | } |
| 854 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 855 | } |
| 856 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 857 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 858 | InputEvent* event = consume(); |
| 859 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 860 | << ": consumer should have returned non-NULL event."; |
| 861 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType()) |
| 862 | << "Got " << inputEventTypeToString(event->getType()) |
| 863 | << " event instead of FOCUS event"; |
| 864 | |
| 865 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 866 | << mName.c_str() << ": event displayId should always be NONE."; |
| 867 | |
| 868 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 869 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 870 | } |
| 871 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 872 | void consumeCaptureEvent(bool hasCapture) { |
| 873 | const InputEvent* event = consume(); |
| 874 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 875 | << ": consumer should have returned non-NULL event."; |
| 876 | ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType()) |
| 877 | << "Got " << inputEventTypeToString(event->getType()) |
| 878 | << " event instead of CAPTURE event"; |
| 879 | |
| 880 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 881 | << mName.c_str() << ": event displayId should always be NONE."; |
| 882 | |
| 883 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 884 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 885 | } |
| 886 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 887 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 888 | const InputEvent* event = consume(); |
| 889 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 890 | << ": consumer should have returned non-NULL event."; |
| 891 | ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType()) |
| 892 | << "Got " << inputEventTypeToString(event->getType()) |
| 893 | << " event instead of DRAG event"; |
| 894 | |
| 895 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 896 | << mName.c_str() << ": event displayId should always be NONE."; |
| 897 | |
| 898 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 899 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 900 | EXPECT_EQ(x, dragEvent.getX()); |
| 901 | EXPECT_EQ(y, dragEvent.getY()); |
| 902 | } |
| 903 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 904 | void consumeTouchModeEvent(bool inTouchMode) { |
| 905 | const InputEvent* event = consume(); |
| 906 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 907 | << ": consumer should have returned non-NULL event."; |
| 908 | ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType()) |
| 909 | << "Got " << inputEventTypeToString(event->getType()) |
| 910 | << " event instead of TOUCH_MODE event"; |
| 911 | |
| 912 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 913 | << mName.c_str() << ": event displayId should always be NONE."; |
| 914 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 915 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 916 | } |
| 917 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 918 | void assertNoEvents() { |
| 919 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 920 | if (event == nullptr) { |
| 921 | return; |
| 922 | } |
| 923 | if (event->getType() == AINPUT_EVENT_TYPE_KEY) { |
| 924 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 925 | ADD_FAILURE() << "Received key event " |
| 926 | << KeyEvent::actionToString(keyEvent.getAction()); |
| 927 | } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) { |
| 928 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 929 | ADD_FAILURE() << "Received motion event " |
| 930 | << MotionEvent::actionToString(motionEvent.getAction()); |
| 931 | } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) { |
| 932 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 933 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 934 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 935 | } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) { |
| 936 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 937 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 938 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 939 | } else if (event->getType() == AINPUT_EVENT_TYPE_TOUCH_MODE) { |
| 940 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 941 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 942 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 943 | } |
| 944 | FAIL() << mName.c_str() |
| 945 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 949 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 950 | int getChannelFd() { return mConsumer->getChannel()->getFd().get(); } |
| 951 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 952 | protected: |
| 953 | std::unique_ptr<InputConsumer> mConsumer; |
| 954 | PreallocatedInputEventFactory mEventFactory; |
| 955 | |
| 956 | std::string mName; |
| 957 | }; |
| 958 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 959 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 960 | public: |
| 961 | static const int32_t WIDTH = 600; |
| 962 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 963 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 964 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 965 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 966 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 967 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 968 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 969 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 970 | dispatcher->createInputChannel(name); |
| 971 | token = (*channel)->getConnectionToken(); |
| 972 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | inputApplicationHandle->updateInfo(); |
| 976 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 977 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 978 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 979 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 980 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 981 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 982 | mInfo.alpha = 1.0; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 983 | mInfo.frameLeft = 0; |
| 984 | mInfo.frameTop = 0; |
| 985 | mInfo.frameRight = WIDTH; |
| 986 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 987 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 988 | mInfo.globalScaleFactor = 1.0; |
| 989 | mInfo.touchableRegion.clear(); |
| 990 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 991 | mInfo.ownerPid = INJECTOR_PID; |
| 992 | mInfo.ownerUid = INJECTOR_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 993 | mInfo.displayId = displayId; |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 994 | mInfo.inputConfig = WindowInfo::InputConfig::NONE; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 995 | } |
| 996 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 997 | sp<FakeWindowHandle> clone( |
| 998 | const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 999 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1000 | sp<FakeWindowHandle> handle = |
| 1001 | new FakeWindowHandle(inputApplicationHandle, dispatcher, mInfo.name + "(Mirror)", |
| 1002 | displayId, mInfo.token); |
| 1003 | return handle; |
| 1004 | } |
| 1005 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1006 | void setTouchable(bool touchable) { |
| 1007 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1008 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1009 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1010 | void setFocusable(bool focusable) { |
| 1011 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1012 | } |
| 1013 | |
| 1014 | void setVisible(bool visible) { |
| 1015 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1016 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1017 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1018 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1019 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1022 | void setPaused(bool paused) { |
| 1023 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1024 | } |
| 1025 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1026 | void setPreventSplitting(bool preventSplitting) { |
| 1027 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | void setSlippery(bool slippery) { |
| 1031 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1032 | } |
| 1033 | |
| 1034 | void setWatchOutsideTouch(bool watchOutside) { |
| 1035 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1036 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1037 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1038 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1039 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1040 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1041 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1042 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1043 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1044 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1045 | mInfo.frameLeft = frame.left; |
| 1046 | mInfo.frameTop = frame.top; |
| 1047 | mInfo.frameRight = frame.right; |
| 1048 | mInfo.frameBottom = frame.bottom; |
| 1049 | mInfo.touchableRegion.clear(); |
| 1050 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1051 | |
| 1052 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1053 | ui::Transform translate; |
| 1054 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1055 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1058 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1059 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1060 | void setIsWallpaper(bool isWallpaper) { |
| 1061 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1062 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1063 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1064 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1065 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1066 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1067 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1068 | void setInputFeatures(Flags<WindowInfo::Feature> features) { mInfo.inputFeatures = features; } |
| 1069 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1070 | void setTrustedOverlay(bool trustedOverlay) { |
| 1071 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1072 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1073 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1074 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1075 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1076 | } |
| 1077 | |
| 1078 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1079 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1080 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1081 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1082 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1083 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1084 | expectedFlags); |
| 1085 | } |
| 1086 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1087 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1088 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
| 1089 | } |
| 1090 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1091 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1092 | int32_t expectedFlags = 0) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1093 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId, |
| 1094 | expectedFlags); |
| 1095 | } |
| 1096 | |
| 1097 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1098 | int32_t expectedFlags = 0) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1099 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId, |
| 1100 | expectedFlags); |
| 1101 | } |
| 1102 | |
| 1103 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1104 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1105 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1106 | } |
| 1107 | |
| 1108 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1109 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1110 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1111 | expectedFlags); |
| 1112 | } |
| 1113 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1114 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1115 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1116 | int32_t expectedFlags = 0) { |
| 1117 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1118 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1119 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1120 | } |
| 1121 | |
| 1122 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1123 | int32_t expectedFlags = 0) { |
| 1124 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1125 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1126 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1127 | } |
| 1128 | |
| 1129 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1130 | int32_t expectedFlags = 0) { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1131 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
| 1132 | expectedFlags); |
| 1133 | } |
| 1134 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1135 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1136 | int32_t expectedFlags = 0) { |
| 1137 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
| 1138 | expectedFlags); |
| 1139 | } |
| 1140 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1141 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1142 | int32_t expectedFlags = 0) { |
| 1143 | InputEvent* event = consume(); |
| 1144 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()); |
| 1145 | const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1146 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked()); |
| 1147 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX()); |
| 1148 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY()); |
| 1149 | } |
| 1150 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1151 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1152 | ASSERT_NE(mInputReceiver, nullptr) |
| 1153 | << "Cannot consume events from a window with no receiver"; |
| 1154 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1155 | } |
| 1156 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1157 | void consumeCaptureEvent(bool hasCapture) { |
| 1158 | ASSERT_NE(mInputReceiver, nullptr) |
| 1159 | << "Cannot consume events from a window with no receiver"; |
| 1160 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1161 | } |
| 1162 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1163 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 1164 | std::optional<int32_t> expectedDisplayId, |
| 1165 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1166 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1167 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1168 | expectedFlags); |
| 1169 | } |
| 1170 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1171 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1172 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1173 | } |
| 1174 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1175 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1176 | ASSERT_NE(mInputReceiver, nullptr) |
| 1177 | << "Cannot consume events from a window with no receiver"; |
| 1178 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1179 | } |
| 1180 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1181 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1182 | if (mInputReceiver == nullptr) { |
| 1183 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1184 | return std::nullopt; |
| 1185 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1186 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | void finishEvent(uint32_t sequenceNum) { |
| 1190 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1191 | mInputReceiver->finishEvent(sequenceNum); |
| 1192 | } |
| 1193 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1194 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1195 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1196 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1197 | } |
| 1198 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1199 | InputEvent* consume() { |
| 1200 | if (mInputReceiver == nullptr) { |
| 1201 | return nullptr; |
| 1202 | } |
| 1203 | return mInputReceiver->consume(); |
| 1204 | } |
| 1205 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1206 | MotionEvent* consumeMotion() { |
| 1207 | InputEvent* event = consume(); |
| 1208 | if (event == nullptr) { |
| 1209 | ADD_FAILURE() << "Consume failed : no event"; |
| 1210 | return nullptr; |
| 1211 | } |
| 1212 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 1213 | ADD_FAILURE() << "Instead of motion event, got " |
| 1214 | << inputEventTypeToString(event->getType()); |
| 1215 | return nullptr; |
| 1216 | } |
| 1217 | return static_cast<MotionEvent*>(event); |
| 1218 | } |
| 1219 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1220 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1221 | if (mInputReceiver == nullptr && |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1222 | mInfo.inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1223 | return; // Can't receive events if the window does not have input channel |
| 1224 | } |
| 1225 | ASSERT_NE(nullptr, mInputReceiver) |
| 1226 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1227 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1228 | } |
| 1229 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1230 | sp<IBinder> getToken() { return mInfo.token; } |
| 1231 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1232 | const std::string& getName() { return mName; } |
| 1233 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1234 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { |
| 1235 | mInfo.ownerPid = ownerPid; |
| 1236 | mInfo.ownerUid = ownerUid; |
| 1237 | } |
| 1238 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 1239 | int32_t getPid() const { return mInfo.ownerPid; } |
| 1240 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1241 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1242 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1243 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1244 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1245 | private: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1246 | const std::string mName; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1247 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1248 | static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1249 | }; |
| 1250 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1251 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1252 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1253 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1254 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1255 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1256 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1257 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
| 1258 | bool allowKeyRepeat = true) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1259 | KeyEvent event; |
| 1260 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1261 | |
| 1262 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1263 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1264 | INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE, |
| 1265 | repeatCount, currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1266 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1267 | int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 1268 | if (!allowKeyRepeat) { |
| 1269 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1270 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1271 | // Inject event until dispatch out. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1272 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1273 | injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1274 | } |
| 1275 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1276 | static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1277 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1278 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId); |
| 1279 | } |
| 1280 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1281 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1282 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1283 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1284 | static InputEventInjectionResult injectKeyDownNoRepeat( |
| 1285 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1286 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId, |
| 1287 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
| 1288 | /* allowKeyRepeat */ false); |
| 1289 | } |
| 1290 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1291 | static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1292 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1293 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId); |
| 1294 | } |
| 1295 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1296 | class PointerBuilder { |
| 1297 | public: |
| 1298 | PointerBuilder(int32_t id, int32_t toolType) { |
| 1299 | mProperties.clear(); |
| 1300 | mProperties.id = id; |
| 1301 | mProperties.toolType = toolType; |
| 1302 | mCoords.clear(); |
| 1303 | } |
| 1304 | |
| 1305 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } |
| 1306 | |
| 1307 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } |
| 1308 | |
| 1309 | PointerBuilder& axis(int32_t axis, float value) { |
| 1310 | mCoords.setAxisValue(axis, value); |
| 1311 | return *this; |
| 1312 | } |
| 1313 | |
| 1314 | PointerProperties buildProperties() const { return mProperties; } |
| 1315 | |
| 1316 | PointerCoords buildCoords() const { return mCoords; } |
| 1317 | |
| 1318 | private: |
| 1319 | PointerProperties mProperties; |
| 1320 | PointerCoords mCoords; |
| 1321 | }; |
| 1322 | |
| 1323 | class MotionEventBuilder { |
| 1324 | public: |
| 1325 | MotionEventBuilder(int32_t action, int32_t source) { |
| 1326 | mAction = action; |
| 1327 | mSource = source; |
| 1328 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1329 | } |
| 1330 | |
| 1331 | MotionEventBuilder& eventTime(nsecs_t eventTime) { |
| 1332 | mEventTime = eventTime; |
| 1333 | return *this; |
| 1334 | } |
| 1335 | |
| 1336 | MotionEventBuilder& displayId(int32_t displayId) { |
| 1337 | mDisplayId = displayId; |
| 1338 | return *this; |
| 1339 | } |
| 1340 | |
| 1341 | MotionEventBuilder& actionButton(int32_t actionButton) { |
| 1342 | mActionButton = actionButton; |
| 1343 | return *this; |
| 1344 | } |
| 1345 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1346 | MotionEventBuilder& buttonState(int32_t buttonState) { |
| 1347 | mButtonState = buttonState; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1348 | return *this; |
| 1349 | } |
| 1350 | |
| 1351 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 1352 | mRawXCursorPosition = rawXCursorPosition; |
| 1353 | return *this; |
| 1354 | } |
| 1355 | |
| 1356 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 1357 | mRawYCursorPosition = rawYCursorPosition; |
| 1358 | return *this; |
| 1359 | } |
| 1360 | |
| 1361 | MotionEventBuilder& pointer(PointerBuilder pointer) { |
| 1362 | mPointers.push_back(pointer); |
| 1363 | return *this; |
| 1364 | } |
| 1365 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1366 | MotionEventBuilder& addFlag(uint32_t flags) { |
| 1367 | mFlags |= flags; |
| 1368 | return *this; |
| 1369 | } |
| 1370 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1371 | MotionEvent build() { |
| 1372 | std::vector<PointerProperties> pointerProperties; |
| 1373 | std::vector<PointerCoords> pointerCoords; |
| 1374 | for (const PointerBuilder& pointer : mPointers) { |
| 1375 | pointerProperties.push_back(pointer.buildProperties()); |
| 1376 | pointerCoords.push_back(pointer.buildCoords()); |
| 1377 | } |
| 1378 | |
| 1379 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 1380 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 1381 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 1382 | mPointers.size() == 1) { |
| 1383 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 1384 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 1385 | } |
| 1386 | |
| 1387 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1388 | ui::Transform identityTransform; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1389 | event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC, |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1390 | mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1391 | mButtonState, MotionClassification::NONE, identityTransform, |
| 1392 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 1393 | mRawYCursorPosition, identityTransform, mEventTime, mEventTime, |
| 1394 | mPointers.size(), pointerProperties.data(), pointerCoords.data()); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1395 | |
| 1396 | return event; |
| 1397 | } |
| 1398 | |
| 1399 | private: |
| 1400 | int32_t mAction; |
| 1401 | int32_t mSource; |
| 1402 | nsecs_t mEventTime; |
| 1403 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1404 | int32_t mActionButton{0}; |
| 1405 | int32_t mButtonState{0}; |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1406 | int32_t mFlags{0}; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1407 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1408 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1409 | |
| 1410 | std::vector<PointerBuilder> mPointers; |
| 1411 | }; |
| 1412 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1413 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1414 | const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1415 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1416 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1417 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode, |
| 1418 | injectionTimeout, |
| 1419 | POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
| 1420 | } |
| 1421 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1422 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1423 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1424 | int32_t displayId, const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1425 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1426 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1427 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1428 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1429 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1430 | MotionEvent event = MotionEventBuilder(action, source) |
| 1431 | .displayId(displayId) |
| 1432 | .eventTime(eventTime) |
| 1433 | .rawXCursorPosition(cursorPosition.x) |
| 1434 | .rawYCursorPosition(cursorPosition.y) |
| 1435 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1436 | .x(position.x) |
| 1437 | .y(position.y)) |
| 1438 | .build(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1439 | |
| 1440 | // Inject event until dispatch out. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1441 | return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1442 | } |
| 1443 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1444 | static InputEventInjectionResult injectMotionDown( |
| 1445 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId, |
| 1446 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1447 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1450 | static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1451 | int32_t source, int32_t displayId, |
| 1452 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1453 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1456 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1457 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1458 | // Define a valid key event. |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1459 | NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 1460 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, |
| 1461 | KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1462 | |
| 1463 | return args; |
| 1464 | } |
| 1465 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1466 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, |
| 1467 | const std::vector<PointF>& points) { |
| 1468 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1469 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1470 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1471 | } |
| 1472 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1473 | PointerProperties pointerProperties[pointerCount]; |
| 1474 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1475 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1476 | for (size_t i = 0; i < pointerCount; i++) { |
| 1477 | pointerProperties[i].clear(); |
| 1478 | pointerProperties[i].id = i; |
| 1479 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1480 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1481 | pointerCoords[i].clear(); |
| 1482 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1483 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1484 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1485 | |
| 1486 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1487 | // Define a valid motion event. |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1488 | NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1489 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 1490 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1491 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 1492 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1493 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 1494 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1495 | |
| 1496 | return args; |
| 1497 | } |
| 1498 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1499 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1500 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1501 | } |
| 1502 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1503 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1504 | const PointerCaptureRequest& request) { |
| 1505 | return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1506 | } |
| 1507 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1508 | /** |
| 1509 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1510 | * broken channel. |
| 1511 | */ |
| 1512 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1513 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1514 | sp<FakeWindowHandle> window = |
| 1515 | new FakeWindowHandle(application, mDispatcher, "Window that breaks its input channel", |
| 1516 | ADISPLAY_ID_DEFAULT); |
| 1517 | |
| 1518 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1519 | |
| 1520 | // Window closes its channel, but the window remains. |
| 1521 | window->destroyReceiver(); |
| 1522 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1523 | } |
| 1524 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1525 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1526 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1527 | sp<FakeWindowHandle> window = |
| 1528 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1529 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1530 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1531 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1532 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1533 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1534 | |
| 1535 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1536 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1537 | } |
| 1538 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1539 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1540 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1541 | sp<FakeWindowHandle> window = |
| 1542 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1543 | |
| 1544 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1545 | // Inject a MotionEvent to an unknown display. |
| 1546 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1547 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
| 1548 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1549 | |
| 1550 | // Window should receive motion event. |
| 1551 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1552 | } |
| 1553 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1554 | /** |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1555 | * Calling setInputWindows once should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1556 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1557 | * called twice. |
| 1558 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1559 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1560 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1561 | sp<FakeWindowHandle> window = |
| 1562 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1563 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1564 | |
| 1565 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1566 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1567 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1568 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1569 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1570 | |
| 1571 | // Window should receive motion event. |
| 1572 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1573 | } |
| 1574 | |
| 1575 | /** |
| 1576 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1577 | */ |
| 1578 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1579 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1580 | sp<FakeWindowHandle> window = |
| 1581 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1582 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1583 | |
| 1584 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1585 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1586 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1587 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1588 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1589 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1590 | |
| 1591 | // Window should receive motion event. |
| 1592 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1593 | } |
| 1594 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1595 | // The foreground window should receive the first touch down event. |
| 1596 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1597 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1598 | sp<FakeWindowHandle> windowTop = |
| 1599 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 1600 | sp<FakeWindowHandle> windowSecond = |
| 1601 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1602 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1603 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1604 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1605 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1606 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1607 | |
| 1608 | // Top window should receive the touch down event. Second window should not receive anything. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1609 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1610 | windowSecond->assertNoEvents(); |
| 1611 | } |
| 1612 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1613 | /** |
| 1614 | * Two windows: A top window, and a wallpaper behind the window. |
| 1615 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1616 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1617 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1618 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1619 | */ |
| 1620 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1621 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1622 | sp<FakeWindowHandle> foregroundWindow = |
| 1623 | new FakeWindowHandle(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1624 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1625 | sp<FakeWindowHandle> wallpaperWindow = |
| 1626 | new FakeWindowHandle(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1627 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1628 | constexpr int expectedWallpaperFlags = |
| 1629 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1630 | |
| 1631 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1632 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1633 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1634 | {100, 200})) |
| 1635 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1636 | |
| 1637 | // Both foreground window and its wallpaper should receive the touch down |
| 1638 | foregroundWindow->consumeMotionDown(); |
| 1639 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1640 | |
| 1641 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1642 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1643 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1644 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1645 | |
| 1646 | foregroundWindow->consumeMotionMove(); |
| 1647 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1648 | |
| 1649 | // Now the foreground window goes away, but the wallpaper stays |
| 1650 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1651 | foregroundWindow->consumeMotionCancel(); |
| 1652 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1653 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1654 | } |
| 1655 | |
| 1656 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1657 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1658 | * with the following differences: |
| 1659 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1660 | * clean up the connection. |
| 1661 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1662 | * Ensure that there's no crash in the dispatcher. |
| 1663 | */ |
| 1664 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1665 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1666 | sp<FakeWindowHandle> foregroundWindow = |
| 1667 | new FakeWindowHandle(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1668 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1669 | sp<FakeWindowHandle> wallpaperWindow = |
| 1670 | new FakeWindowHandle(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1671 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1672 | constexpr int expectedWallpaperFlags = |
| 1673 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1674 | |
| 1675 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1676 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1677 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1678 | {100, 200})) |
| 1679 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1680 | |
| 1681 | // Both foreground window and its wallpaper should receive the touch down |
| 1682 | foregroundWindow->consumeMotionDown(); |
| 1683 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1684 | |
| 1685 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1686 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1687 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1688 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1689 | |
| 1690 | foregroundWindow->consumeMotionMove(); |
| 1691 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1692 | |
| 1693 | // Wallpaper closes its channel, but the window remains. |
| 1694 | wallpaperWindow->destroyReceiver(); |
| 1695 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1696 | |
| 1697 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1698 | // is no longer valid. |
| 1699 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1700 | foregroundWindow->consumeMotionCancel(); |
| 1701 | } |
| 1702 | |
| 1703 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1704 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1705 | * The top window gets a multitouch gesture. |
| 1706 | * Ensure that wallpaper gets the same gesture. |
| 1707 | */ |
| 1708 | TEST_F(InputDispatcherTest, WallpaperWindow_ReceivesMultiTouch) { |
| 1709 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1710 | sp<FakeWindowHandle> window = |
| 1711 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1712 | window->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1713 | |
| 1714 | sp<FakeWindowHandle> wallpaperWindow = |
| 1715 | new FakeWindowHandle(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1716 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1717 | constexpr int expectedWallpaperFlags = |
| 1718 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1719 | |
| 1720 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, wallpaperWindow}}}); |
| 1721 | |
| 1722 | // Touch down on top window |
| 1723 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1724 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1725 | {100, 100})) |
| 1726 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1727 | |
| 1728 | // Both top window and its wallpaper should receive the touch down |
| 1729 | window->consumeMotionDown(); |
| 1730 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1731 | |
| 1732 | // Second finger down on the top window |
| 1733 | const MotionEvent secondFingerDownEvent = |
| 1734 | MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1735 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1736 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1737 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1738 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1739 | .x(100) |
| 1740 | .y(100)) |
| 1741 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1742 | .x(150) |
| 1743 | .y(150)) |
| 1744 | .build(); |
| 1745 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1746 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1747 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1748 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1749 | |
| 1750 | window->consumeMotionPointerDown(1 /* pointerIndex */); |
| 1751 | wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT, |
| 1752 | expectedWallpaperFlags); |
| 1753 | window->assertNoEvents(); |
| 1754 | wallpaperWindow->assertNoEvents(); |
| 1755 | } |
| 1756 | |
| 1757 | /** |
| 1758 | * Two windows: a window on the left and window on the right. |
| 1759 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1760 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1761 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1762 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1763 | * ACTION_POINTER_DOWN(1). |
| 1764 | */ |
| 1765 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1766 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1767 | sp<FakeWindowHandle> leftWindow = |
| 1768 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1769 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1770 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1771 | |
| 1772 | sp<FakeWindowHandle> rightWindow = |
| 1773 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1774 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1775 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1776 | |
| 1777 | sp<FakeWindowHandle> wallpaperWindow = |
| 1778 | new FakeWindowHandle(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 1779 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1780 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1781 | constexpr int expectedWallpaperFlags = |
| 1782 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1783 | |
| 1784 | mDispatcher->setInputWindows( |
| 1785 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
| 1786 | |
| 1787 | // Touch down on left window |
| 1788 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1789 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1790 | {100, 100})) |
| 1791 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1792 | |
| 1793 | // Both foreground window and its wallpaper should receive the touch down |
| 1794 | leftWindow->consumeMotionDown(); |
| 1795 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1796 | |
| 1797 | // Second finger down on the right window |
| 1798 | const MotionEvent secondFingerDownEvent = |
| 1799 | MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1800 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1801 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1802 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1803 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1804 | .x(100) |
| 1805 | .y(100)) |
| 1806 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1807 | .x(300) |
| 1808 | .y(100)) |
| 1809 | .build(); |
| 1810 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1811 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1812 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1813 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1814 | |
| 1815 | leftWindow->consumeMotionMove(); |
| 1816 | // Since the touch is split, right window gets ACTION_DOWN |
| 1817 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1818 | wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT, |
| 1819 | expectedWallpaperFlags); |
| 1820 | |
| 1821 | // Now, leftWindow, which received the first finger, disappears. |
| 1822 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); |
| 1823 | leftWindow->consumeMotionCancel(); |
| 1824 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1825 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1826 | |
| 1827 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 1828 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 1829 | const MotionEvent secondFingerMoveEvent = |
| 1830 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1831 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1832 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1833 | .x(100) |
| 1834 | .y(100)) |
| 1835 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1836 | .x(310) |
| 1837 | .y(110)) |
| 1838 | .build(); |
| 1839 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1840 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 1841 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 1842 | rightWindow->consumeMotionMove(); |
| 1843 | |
| 1844 | leftWindow->assertNoEvents(); |
| 1845 | rightWindow->assertNoEvents(); |
| 1846 | wallpaperWindow->assertNoEvents(); |
| 1847 | } |
| 1848 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1849 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1850 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1851 | sp<FakeWindowHandle> windowLeft = |
| 1852 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1853 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1854 | sp<FakeWindowHandle> windowRight = |
| 1855 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1856 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1857 | |
| 1858 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1859 | |
| 1860 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 1861 | |
| 1862 | // Start cursor position in right window so that we can move the cursor to left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1863 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1864 | injectMotionEvent(mDispatcher, |
| 1865 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1866 | AINPUT_SOURCE_MOUSE) |
| 1867 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1868 | .x(900) |
| 1869 | .y(400)) |
| 1870 | .build())); |
| 1871 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1872 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1873 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1874 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1875 | |
| 1876 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1877 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1878 | injectMotionEvent(mDispatcher, |
| 1879 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1880 | AINPUT_SOURCE_MOUSE) |
| 1881 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1882 | .x(300) |
| 1883 | .y(400)) |
| 1884 | .build())); |
| 1885 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1886 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1887 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1888 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1889 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1890 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1891 | |
| 1892 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1893 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1894 | injectMotionEvent(mDispatcher, |
| 1895 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 1896 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1897 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1898 | .x(300) |
| 1899 | .y(400)) |
| 1900 | .build())); |
| 1901 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1902 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1903 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1904 | injectMotionEvent(mDispatcher, |
| 1905 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1906 | AINPUT_SOURCE_MOUSE) |
| 1907 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1908 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1909 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1910 | .x(300) |
| 1911 | .y(400)) |
| 1912 | .build())); |
| 1913 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1914 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1915 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1916 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1917 | injectMotionEvent(mDispatcher, |
| 1918 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1919 | AINPUT_SOURCE_MOUSE) |
| 1920 | .buttonState(0) |
| 1921 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1922 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1923 | .x(300) |
| 1924 | .y(400)) |
| 1925 | .build())); |
| 1926 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1927 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1928 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1929 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1930 | injectMotionEvent(mDispatcher, |
| 1931 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 1932 | .buttonState(0) |
| 1933 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1934 | .x(300) |
| 1935 | .y(400)) |
| 1936 | .build())); |
| 1937 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1938 | |
| 1939 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1940 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1941 | injectMotionEvent(mDispatcher, |
| 1942 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1943 | AINPUT_SOURCE_MOUSE) |
| 1944 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1945 | .x(900) |
| 1946 | .y(400)) |
| 1947 | .build())); |
| 1948 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1949 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1950 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1951 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1952 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1953 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1954 | } |
| 1955 | |
| 1956 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 1957 | // directly in this test. |
| 1958 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1959 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1960 | sp<FakeWindowHandle> window = |
| 1961 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1962 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1963 | |
| 1964 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1965 | |
| 1966 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1967 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1968 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1969 | injectMotionEvent(mDispatcher, |
| 1970 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1971 | AINPUT_SOURCE_MOUSE) |
| 1972 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1973 | .x(300) |
| 1974 | .y(400)) |
| 1975 | .build())); |
| 1976 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1977 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1978 | |
| 1979 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1980 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1981 | injectMotionEvent(mDispatcher, |
| 1982 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 1983 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1984 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1985 | .x(300) |
| 1986 | .y(400)) |
| 1987 | .build())); |
| 1988 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1989 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1990 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1991 | injectMotionEvent(mDispatcher, |
| 1992 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1993 | AINPUT_SOURCE_MOUSE) |
| 1994 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1995 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1996 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1997 | .x(300) |
| 1998 | .y(400)) |
| 1999 | .build())); |
| 2000 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 2001 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 2002 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2003 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2004 | injectMotionEvent(mDispatcher, |
| 2005 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2006 | AINPUT_SOURCE_MOUSE) |
| 2007 | .buttonState(0) |
| 2008 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2009 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2010 | .x(300) |
| 2011 | .y(400)) |
| 2012 | .build())); |
| 2013 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2014 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 2015 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2016 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2017 | injectMotionEvent(mDispatcher, |
| 2018 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 2019 | .buttonState(0) |
| 2020 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2021 | .x(300) |
| 2022 | .y(400)) |
| 2023 | .build())); |
| 2024 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2025 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2026 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2027 | injectMotionEvent(mDispatcher, |
| 2028 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 2029 | AINPUT_SOURCE_MOUSE) |
| 2030 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2031 | .x(300) |
| 2032 | .y(400)) |
| 2033 | .build())); |
| 2034 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 2035 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 2036 | } |
| 2037 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2038 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2039 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2040 | |
| 2041 | sp<FakeWindowHandle> windowLeft = |
| 2042 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2043 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2044 | sp<FakeWindowHandle> windowRight = |
| 2045 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2046 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2047 | |
| 2048 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2049 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2050 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2051 | |
| 2052 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 2053 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2054 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2055 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2056 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2057 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2058 | windowRight->assertNoEvents(); |
| 2059 | } |
| 2060 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2061 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2062 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2063 | sp<FakeWindowHandle> window = |
| 2064 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2065 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2066 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2067 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2068 | setFocusedWindow(window); |
| 2069 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2070 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2071 | |
| 2072 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2073 | mDispatcher->notifyKey(&keyArgs); |
| 2074 | |
| 2075 | // Window should receive key down event. |
| 2076 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2077 | |
| 2078 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 2079 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2080 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2081 | mDispatcher->notifyDeviceReset(&args); |
| 2082 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 2083 | AKEY_EVENT_FLAG_CANCELED); |
| 2084 | } |
| 2085 | |
| 2086 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2087 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2088 | sp<FakeWindowHandle> window = |
| 2089 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2090 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2091 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2092 | |
| 2093 | NotifyMotionArgs motionArgs = |
| 2094 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2095 | ADISPLAY_ID_DEFAULT); |
| 2096 | mDispatcher->notifyMotion(&motionArgs); |
| 2097 | |
| 2098 | // Window should receive motion down event. |
| 2099 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2100 | |
| 2101 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 2102 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2103 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2104 | mDispatcher->notifyDeviceReset(&args); |
| 2105 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, |
| 2106 | 0 /*expectedFlags*/); |
| 2107 | } |
| 2108 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2109 | /** |
| 2110 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 2111 | * |
| 2112 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 2113 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 2114 | * space. |
| 2115 | */ |
| 2116 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 2117 | public: |
| 2118 | void SetUp() override { |
| 2119 | InputDispatcherTest::SetUp(); |
| 2120 | mDisplayInfos.clear(); |
| 2121 | mWindowInfos.clear(); |
| 2122 | } |
| 2123 | |
| 2124 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 2125 | gui::DisplayInfo info; |
| 2126 | info.displayId = displayId; |
| 2127 | info.transform = transform; |
| 2128 | mDisplayInfos.push_back(std::move(info)); |
| 2129 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 2130 | } |
| 2131 | |
| 2132 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 2133 | mWindowInfos.push_back(*windowHandle->getInfo()); |
| 2134 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 2135 | } |
| 2136 | |
| 2137 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 2138 | // on the display. |
| 2139 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 2140 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 2141 | // respectively. |
| 2142 | ui::Transform displayTransform; |
| 2143 | displayTransform.set(2, 0, 0, 4); |
| 2144 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 2145 | |
| 2146 | std::shared_ptr<FakeApplicationHandle> application = |
| 2147 | std::make_shared<FakeApplicationHandle>(); |
| 2148 | |
| 2149 | // Add two windows to the display. Their frames are represented in the display space. |
| 2150 | sp<FakeWindowHandle> firstWindow = |
| 2151 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2152 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 2153 | addWindow(firstWindow); |
| 2154 | |
| 2155 | sp<FakeWindowHandle> secondWindow = |
| 2156 | new FakeWindowHandle(application, mDispatcher, "Second Window", |
| 2157 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2158 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 2159 | addWindow(secondWindow); |
| 2160 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 2161 | } |
| 2162 | |
| 2163 | private: |
| 2164 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 2165 | std::vector<gui::WindowInfo> mWindowInfos; |
| 2166 | }; |
| 2167 | |
| 2168 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) { |
| 2169 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2170 | // Send down to the first window. The point is represented in the display space. The point is |
| 2171 | // selected so that if the hit test was done with the transform applied to it, then it would |
| 2172 | // end up in the incorrect window. |
| 2173 | NotifyMotionArgs downMotionArgs = |
| 2174 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2175 | ADISPLAY_ID_DEFAULT, {PointF{75, 55}}); |
| 2176 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2177 | |
| 2178 | firstWindow->consumeMotionDown(); |
| 2179 | secondWindow->assertNoEvents(); |
| 2180 | } |
| 2181 | |
| 2182 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 2183 | // the event should be treated as being in the logical display space. |
| 2184 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 2185 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2186 | // Send down to the first window. The point is represented in the logical display space. The |
| 2187 | // point is selected so that if the hit test was done in logical display space, then it would |
| 2188 | // end up in the incorrect window. |
| 2189 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2190 | PointF{75 * 2, 55 * 4}); |
| 2191 | |
| 2192 | firstWindow->consumeMotionDown(); |
| 2193 | secondWindow->assertNoEvents(); |
| 2194 | } |
| 2195 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 2196 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 2197 | // event should be treated as being in the logical display space. |
| 2198 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 2199 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2200 | |
| 2201 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 2202 | ui::Transform injectedEventTransform; |
| 2203 | injectedEventTransform.set(matrix); |
| 2204 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 2205 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 2206 | |
| 2207 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2208 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2209 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 2210 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2211 | .x(untransformedPoint.x) |
| 2212 | .y(untransformedPoint.y)) |
| 2213 | .build(); |
| 2214 | event.transform(matrix); |
| 2215 | |
| 2216 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, |
| 2217 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 2218 | |
| 2219 | firstWindow->consumeMotionDown(); |
| 2220 | secondWindow->assertNoEvents(); |
| 2221 | } |
| 2222 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2223 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 2224 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2225 | |
| 2226 | // Send down to the second window. |
| 2227 | NotifyMotionArgs downMotionArgs = |
| 2228 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2229 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}}); |
| 2230 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2231 | |
| 2232 | firstWindow->assertNoEvents(); |
| 2233 | const MotionEvent* event = secondWindow->consumeMotion(); |
| 2234 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 2235 | |
| 2236 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 2237 | EXPECT_EQ(300, event->getRawX(0)); |
| 2238 | EXPECT_EQ(880, event->getRawY(0)); |
| 2239 | |
| 2240 | // Ensure that the x and y values are in the window's coordinate space. |
| 2241 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 2242 | // the logical display space. This will be the origin of the window space. |
| 2243 | EXPECT_EQ(100, event->getX(0)); |
| 2244 | EXPECT_EQ(80, event->getY(0)); |
| 2245 | } |
| 2246 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2247 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 2248 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2249 | |
| 2250 | class TransferTouchFixture : public InputDispatcherTest, |
| 2251 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 2252 | |
| 2253 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2254 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2255 | |
| 2256 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2257 | sp<FakeWindowHandle> firstWindow = |
| 2258 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 2259 | sp<FakeWindowHandle> secondWindow = |
| 2260 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2261 | |
| 2262 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2263 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2264 | |
| 2265 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2266 | NotifyMotionArgs downMotionArgs = |
| 2267 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2268 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2269 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2270 | // Only the first window should get the down event |
| 2271 | firstWindow->consumeMotionDown(); |
| 2272 | secondWindow->assertNoEvents(); |
| 2273 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2274 | // Transfer touch to the second window |
| 2275 | TransferFunction f = GetParam(); |
| 2276 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 2277 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2278 | // The first window gets cancel and the second gets down |
| 2279 | firstWindow->consumeMotionCancel(); |
| 2280 | secondWindow->consumeMotionDown(); |
| 2281 | |
| 2282 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2283 | NotifyMotionArgs upMotionArgs = |
| 2284 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2285 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2286 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2287 | // The first window gets no events and the second gets up |
| 2288 | firstWindow->assertNoEvents(); |
| 2289 | secondWindow->consumeMotionUp(); |
| 2290 | } |
| 2291 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2292 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2293 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2294 | |
| 2295 | PointF touchPoint = {10, 10}; |
| 2296 | |
| 2297 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2298 | sp<FakeWindowHandle> firstWindow = |
| 2299 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 2300 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2301 | sp<FakeWindowHandle> secondWindow = |
| 2302 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 2303 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2304 | |
| 2305 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2306 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2307 | |
| 2308 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2309 | NotifyMotionArgs downMotionArgs = |
| 2310 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2311 | ADISPLAY_ID_DEFAULT, {touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2312 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2313 | // Only the first window should get the down event |
| 2314 | firstWindow->consumeMotionDown(); |
| 2315 | secondWindow->assertNoEvents(); |
| 2316 | |
| 2317 | // Send pointer down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2318 | NotifyMotionArgs pointerDownMotionArgs = |
| 2319 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 2320 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 2321 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2322 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2323 | mDispatcher->notifyMotion(&pointerDownMotionArgs); |
| 2324 | // Only the first window should get the pointer down event |
| 2325 | firstWindow->consumeMotionPointerDown(1); |
| 2326 | secondWindow->assertNoEvents(); |
| 2327 | |
| 2328 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2329 | TransferFunction f = GetParam(); |
| 2330 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 2331 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2332 | // The first window gets cancel and the second gets down and pointer down |
| 2333 | firstWindow->consumeMotionCancel(); |
| 2334 | secondWindow->consumeMotionDown(); |
| 2335 | secondWindow->consumeMotionPointerDown(1); |
| 2336 | |
| 2337 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2338 | NotifyMotionArgs pointerUpMotionArgs = |
| 2339 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | |
| 2340 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 2341 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2342 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2343 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 2344 | // The first window gets nothing and the second gets pointer up |
| 2345 | firstWindow->assertNoEvents(); |
| 2346 | secondWindow->consumeMotionPointerUp(1); |
| 2347 | |
| 2348 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2349 | NotifyMotionArgs upMotionArgs = |
| 2350 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2351 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2352 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2353 | // The first window gets nothing and the second gets up |
| 2354 | firstWindow->assertNoEvents(); |
| 2355 | secondWindow->consumeMotionUp(); |
| 2356 | } |
| 2357 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2358 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 2359 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 2360 | // for the case where there are multiple pointers split across several windows. |
| 2361 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 2362 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2363 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 2364 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2365 | return dispatcher->transferTouch(destChannelToken); |
| 2366 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2367 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 2368 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2369 | return dispatcher->transferTouchFocus(from, to, |
| 2370 | false /*isDragAndDrop*/); |
| 2371 | })); |
| 2372 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2373 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2374 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2375 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2376 | sp<FakeWindowHandle> firstWindow = |
| 2377 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2378 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2379 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2380 | sp<FakeWindowHandle> secondWindow = |
| 2381 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2382 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2383 | |
| 2384 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2385 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2386 | |
| 2387 | PointF pointInFirst = {300, 200}; |
| 2388 | PointF pointInSecond = {300, 600}; |
| 2389 | |
| 2390 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2391 | NotifyMotionArgs firstDownMotionArgs = |
| 2392 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2393 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2394 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 2395 | // Only the first window should get the down event |
| 2396 | firstWindow->consumeMotionDown(); |
| 2397 | secondWindow->assertNoEvents(); |
| 2398 | |
| 2399 | // Send down to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2400 | NotifyMotionArgs secondDownMotionArgs = |
| 2401 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 2402 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 2403 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2404 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2405 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 2406 | // The first window gets a move and the second a down |
| 2407 | firstWindow->consumeMotionMove(); |
| 2408 | secondWindow->consumeMotionDown(); |
| 2409 | |
| 2410 | // Transfer touch focus to the second window |
| 2411 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 2412 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 2413 | firstWindow->consumeMotionCancel(); |
| 2414 | secondWindow->consumeMotionPointerDown(1); |
| 2415 | |
| 2416 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2417 | NotifyMotionArgs pointerUpMotionArgs = |
| 2418 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | |
| 2419 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 2420 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2421 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2422 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 2423 | // The first window gets nothing and the second gets pointer up |
| 2424 | firstWindow->assertNoEvents(); |
| 2425 | secondWindow->consumeMotionPointerUp(1); |
| 2426 | |
| 2427 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2428 | NotifyMotionArgs upMotionArgs = |
| 2429 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2430 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2431 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2432 | // The first window gets nothing and the second gets up |
| 2433 | firstWindow->assertNoEvents(); |
| 2434 | secondWindow->consumeMotionUp(); |
| 2435 | } |
| 2436 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2437 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 2438 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 2439 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 2440 | // window should get nothing. |
| 2441 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 2442 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2443 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2444 | sp<FakeWindowHandle> firstWindow = |
| 2445 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 2446 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2447 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2448 | sp<FakeWindowHandle> secondWindow = |
| 2449 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
| 2450 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2451 | |
| 2452 | // Add the windows to the dispatcher |
| 2453 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 2454 | |
| 2455 | PointF pointInFirst = {300, 200}; |
| 2456 | PointF pointInSecond = {300, 600}; |
| 2457 | |
| 2458 | // Send down to the first window |
| 2459 | NotifyMotionArgs firstDownMotionArgs = |
| 2460 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2461 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 2462 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 2463 | // Only the first window should get the down event |
| 2464 | firstWindow->consumeMotionDown(); |
| 2465 | secondWindow->assertNoEvents(); |
| 2466 | |
| 2467 | // Send down to the second window |
| 2468 | NotifyMotionArgs secondDownMotionArgs = |
| 2469 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 2470 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 2471 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2472 | {pointInFirst, pointInSecond}); |
| 2473 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 2474 | // The first window gets a move and the second a down |
| 2475 | firstWindow->consumeMotionMove(); |
| 2476 | secondWindow->consumeMotionDown(); |
| 2477 | |
| 2478 | // Transfer touch focus to the second window |
| 2479 | const bool transferred = mDispatcher->transferTouch(secondWindow->getToken()); |
| 2480 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 2481 | ASSERT_FALSE(transferred); |
| 2482 | firstWindow->assertNoEvents(); |
| 2483 | secondWindow->assertNoEvents(); |
| 2484 | |
| 2485 | // The rest of the dispatch should proceed as normal |
| 2486 | // Send pointer up to the second window |
| 2487 | NotifyMotionArgs pointerUpMotionArgs = |
| 2488 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | |
| 2489 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 2490 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2491 | {pointInFirst, pointInSecond}); |
| 2492 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 2493 | // The first window gets MOVE and the second gets pointer up |
| 2494 | firstWindow->consumeMotionMove(); |
| 2495 | secondWindow->consumeMotionUp(); |
| 2496 | |
| 2497 | // Send up event to the first window |
| 2498 | NotifyMotionArgs upMotionArgs = |
| 2499 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2500 | ADISPLAY_ID_DEFAULT); |
| 2501 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2502 | // The first window gets nothing and the second gets up |
| 2503 | firstWindow->consumeMotionUp(); |
| 2504 | secondWindow->assertNoEvents(); |
| 2505 | } |
| 2506 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2507 | // This case will create two windows and one mirrored window on the default display and mirror |
| 2508 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 2509 | // the windows info of second display before default display. |
| 2510 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 2511 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2512 | sp<FakeWindowHandle> firstWindowInPrimary = |
| 2513 | new FakeWindowHandle(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
| 2514 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2515 | sp<FakeWindowHandle> secondWindowInPrimary = |
| 2516 | new FakeWindowHandle(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
| 2517 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2518 | |
| 2519 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 2520 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 2521 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2522 | |
| 2523 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 2524 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 2525 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2526 | |
| 2527 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 2528 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 2529 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2530 | |
| 2531 | // Update window info, let it find window handle of second display first. |
| 2532 | mDispatcher->setInputWindows( |
| 2533 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 2534 | {ADISPLAY_ID_DEFAULT, |
| 2535 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 2536 | |
| 2537 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2538 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2539 | {50, 50})) |
| 2540 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2541 | |
| 2542 | // Window should receive motion event. |
| 2543 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2544 | |
| 2545 | // Transfer touch focus |
| 2546 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 2547 | secondWindowInPrimary->getToken())); |
| 2548 | // The first window gets cancel. |
| 2549 | firstWindowInPrimary->consumeMotionCancel(); |
| 2550 | secondWindowInPrimary->consumeMotionDown(); |
| 2551 | |
| 2552 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2553 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2554 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 2555 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2556 | firstWindowInPrimary->assertNoEvents(); |
| 2557 | secondWindowInPrimary->consumeMotionMove(); |
| 2558 | |
| 2559 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2560 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2561 | {150, 50})) |
| 2562 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2563 | firstWindowInPrimary->assertNoEvents(); |
| 2564 | secondWindowInPrimary->consumeMotionUp(); |
| 2565 | } |
| 2566 | |
| 2567 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 2568 | // 'transferTouch' api. |
| 2569 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 2570 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2571 | sp<FakeWindowHandle> firstWindowInPrimary = |
| 2572 | new FakeWindowHandle(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
| 2573 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2574 | sp<FakeWindowHandle> secondWindowInPrimary = |
| 2575 | new FakeWindowHandle(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
| 2576 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2577 | |
| 2578 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 2579 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 2580 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2581 | |
| 2582 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 2583 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 2584 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2585 | |
| 2586 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 2587 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 2588 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2589 | |
| 2590 | // Update window info, let it find window handle of second display first. |
| 2591 | mDispatcher->setInputWindows( |
| 2592 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 2593 | {ADISPLAY_ID_DEFAULT, |
| 2594 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 2595 | |
| 2596 | // Touch on second display. |
| 2597 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2598 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) |
| 2599 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2600 | |
| 2601 | // Window should receive motion event. |
| 2602 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 2603 | |
| 2604 | // Transfer touch focus |
| 2605 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken())); |
| 2606 | |
| 2607 | // The first window gets cancel. |
| 2608 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 2609 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 2610 | |
| 2611 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2612 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2613 | SECOND_DISPLAY_ID, {150, 50})) |
| 2614 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2615 | firstWindowInPrimary->assertNoEvents(); |
| 2616 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 2617 | |
| 2618 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2619 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
| 2620 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2621 | firstWindowInPrimary->assertNoEvents(); |
| 2622 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 2623 | } |
| 2624 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2625 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2626 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2627 | sp<FakeWindowHandle> window = |
| 2628 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2629 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2630 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2631 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2632 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2633 | |
| 2634 | window->consumeFocusEvent(true); |
| 2635 | |
| 2636 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2637 | mDispatcher->notifyKey(&keyArgs); |
| 2638 | |
| 2639 | // Window should receive key down event. |
| 2640 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2641 | } |
| 2642 | |
| 2643 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2644 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2645 | sp<FakeWindowHandle> window = |
| 2646 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2647 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2648 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2649 | |
| 2650 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2651 | mDispatcher->notifyKey(&keyArgs); |
| 2652 | mDispatcher->waitForIdle(); |
| 2653 | |
| 2654 | window->assertNoEvents(); |
| 2655 | } |
| 2656 | |
| 2657 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 2658 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2659 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2660 | sp<FakeWindowHandle> window = |
| 2661 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2662 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2663 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2664 | |
| 2665 | // Send key |
| 2666 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2667 | mDispatcher->notifyKey(&keyArgs); |
| 2668 | // Send motion |
| 2669 | NotifyMotionArgs motionArgs = |
| 2670 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2671 | ADISPLAY_ID_DEFAULT); |
| 2672 | mDispatcher->notifyMotion(&motionArgs); |
| 2673 | |
| 2674 | // Window should receive only the motion event |
| 2675 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2676 | window->assertNoEvents(); // Key event or focus event will not be received |
| 2677 | } |
| 2678 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2679 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 2680 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2681 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2682 | sp<FakeWindowHandle> firstWindow = |
| 2683 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 2684 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2685 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2686 | sp<FakeWindowHandle> secondWindow = |
| 2687 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
| 2688 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2689 | |
| 2690 | // Add the windows to the dispatcher |
| 2691 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 2692 | |
| 2693 | PointF pointInFirst = {300, 200}; |
| 2694 | PointF pointInSecond = {300, 600}; |
| 2695 | |
| 2696 | // Send down to the first window |
| 2697 | NotifyMotionArgs firstDownMotionArgs = |
| 2698 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2699 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 2700 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 2701 | // Only the first window should get the down event |
| 2702 | firstWindow->consumeMotionDown(); |
| 2703 | secondWindow->assertNoEvents(); |
| 2704 | |
| 2705 | // Send down to the second window |
| 2706 | NotifyMotionArgs secondDownMotionArgs = |
| 2707 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 2708 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 2709 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2710 | {pointInFirst, pointInSecond}); |
| 2711 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 2712 | // The first window gets a move and the second a down |
| 2713 | firstWindow->consumeMotionMove(); |
| 2714 | secondWindow->consumeMotionDown(); |
| 2715 | |
| 2716 | // Send pointer cancel to the second window |
| 2717 | NotifyMotionArgs pointerUpMotionArgs = |
| 2718 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | |
| 2719 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 2720 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2721 | {pointInFirst, pointInSecond}); |
| 2722 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 2723 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 2724 | // The first window gets move and the second gets cancel. |
| 2725 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 2726 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 2727 | |
| 2728 | // Send up event. |
| 2729 | NotifyMotionArgs upMotionArgs = |
| 2730 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2731 | ADISPLAY_ID_DEFAULT); |
| 2732 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2733 | // The first window gets up and the second gets nothing. |
| 2734 | firstWindow->consumeMotionUp(); |
| 2735 | secondWindow->assertNoEvents(); |
| 2736 | } |
| 2737 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 2738 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 2739 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2740 | |
| 2741 | sp<FakeWindowHandle> window = |
| 2742 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2743 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2744 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 2745 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 2746 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 2747 | |
| 2748 | window->sendTimeline(1 /*inputEventId*/, graphicsTimeline); |
| 2749 | window->assertNoEvents(); |
| 2750 | mDispatcher->waitForIdle(); |
| 2751 | } |
| 2752 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2753 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2754 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2755 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2756 | int32_t displayId) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 2757 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2758 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 2759 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2762 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 2763 | |
| 2764 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 2765 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, |
| 2766 | expectedDisplayId, expectedFlags); |
| 2767 | } |
| 2768 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2769 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 2770 | |
| 2771 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 2772 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2773 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 2774 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 2775 | expectedDisplayId, expectedFlags); |
| 2776 | } |
| 2777 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2778 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 2779 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 2780 | expectedDisplayId, expectedFlags); |
| 2781 | } |
| 2782 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2783 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 2784 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, |
| 2785 | expectedDisplayId, expectedFlags); |
| 2786 | } |
| 2787 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2788 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 2789 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 2790 | expectedDisplayId, expectedFlags); |
| 2791 | } |
| 2792 | |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 2793 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 2794 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 2795 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 2796 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT, |
| 2797 | 0 /*expectedFlags*/); |
| 2798 | } |
| 2799 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2800 | MotionEvent* consumeMotion() { |
| 2801 | InputEvent* event = mInputReceiver->consume(); |
| 2802 | if (!event) { |
| 2803 | ADD_FAILURE() << "No event was produced"; |
| 2804 | return nullptr; |
| 2805 | } |
| 2806 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 2807 | ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion"; |
| 2808 | return nullptr; |
| 2809 | } |
| 2810 | return static_cast<MotionEvent*>(event); |
| 2811 | } |
| 2812 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2813 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 2814 | |
| 2815 | private: |
| 2816 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2817 | }; |
| 2818 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2819 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 2820 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2821 | /** |
| 2822 | * Two entities that receive touch: A window, and a global monitor. |
| 2823 | * The touch goes to the window, and then the window disappears. |
| 2824 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 2825 | * for the monitor, as well. |
| 2826 | * 1. foregroundWindow |
| 2827 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 2828 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2829 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2830 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2831 | sp<FakeWindowHandle> window = |
| 2832 | new FakeWindowHandle(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 2833 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2834 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2835 | |
| 2836 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2837 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2838 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2839 | {100, 200})) |
| 2840 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2841 | |
| 2842 | // Both the foreground window and the global monitor should receive the touch down |
| 2843 | window->consumeMotionDown(); |
| 2844 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2845 | |
| 2846 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2847 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2848 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 2849 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2850 | |
| 2851 | window->consumeMotionMove(); |
| 2852 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 2853 | |
| 2854 | // Now the foreground window goes away |
| 2855 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 2856 | window->consumeMotionCancel(); |
| 2857 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 2858 | |
| 2859 | // If more events come in, there will be no more foreground window to send them to. This will |
| 2860 | // cause a cancel for the monitor, as well. |
| 2861 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 2862 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2863 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 2864 | << "Injection should fail because the window was removed"; |
| 2865 | window->assertNoEvents(); |
| 2866 | // Global monitor now gets the cancel |
| 2867 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 2868 | } |
| 2869 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2870 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2871 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2872 | sp<FakeWindowHandle> window = |
| 2873 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2874 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2875 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2876 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2877 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2878 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2879 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2880 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2881 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2882 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2885 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
| 2886 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2887 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2888 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2889 | sp<FakeWindowHandle> window = |
| 2890 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2891 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2892 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2893 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2894 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2895 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2896 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2897 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2898 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2899 | // Pilfer pointers from the monitor. |
| 2900 | // This should not do anything and the window should continue to receive events. |
| 2901 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2902 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2903 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2904 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2905 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2906 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2907 | |
| 2908 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 2909 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2912 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2913 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2914 | sp<FakeWindowHandle> window = |
| 2915 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2916 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2917 | window->setWindowOffset(20, 40); |
| 2918 | window->setWindowTransform(0, 1, -1, 0); |
| 2919 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2920 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2921 | |
| 2922 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2923 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 2924 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2925 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2926 | MotionEvent* event = monitor.consumeMotion(); |
| 2927 | // Even though window has transform, gesture monitor must not. |
| 2928 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 2929 | } |
| 2930 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2931 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2932 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2933 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2934 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2935 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2936 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2937 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 2938 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2941 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2942 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2943 | sp<FakeWindowHandle> window = |
| 2944 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2945 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2946 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2947 | |
| 2948 | NotifyMotionArgs motionArgs = |
| 2949 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2950 | ADISPLAY_ID_DEFAULT); |
| 2951 | |
| 2952 | mDispatcher->notifyMotion(&motionArgs); |
| 2953 | // Window should receive motion down event. |
| 2954 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2955 | |
| 2956 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2957 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2958 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2959 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 2960 | motionArgs.pointerCoords[0].getX() - 10); |
| 2961 | |
| 2962 | mDispatcher->notifyMotion(&motionArgs); |
| 2963 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
| 2964 | 0 /*expectedFlags*/); |
| 2965 | } |
| 2966 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2967 | /** |
| 2968 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 2969 | * the device default right away. In the test scenario, we check both the default value, |
| 2970 | * and the action of enabling / disabling. |
| 2971 | */ |
| 2972 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2973 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2974 | sp<FakeWindowHandle> window = |
| 2975 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 2976 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2977 | |
| 2978 | // Set focused application. |
| 2979 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2980 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2981 | |
| 2982 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2983 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2984 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2985 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 2986 | |
| 2987 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2988 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2989 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2990 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); |
| 2991 | |
| 2992 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 2993 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
| 2994 | /* hasPermission */ true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2995 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2996 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2997 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2998 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2999 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); |
| 3000 | |
| 3001 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3002 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3003 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3004 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); |
| 3005 | |
| 3006 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 3007 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
| 3008 | /* hasPermission */ true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3009 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3010 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3011 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3012 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3013 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 3014 | |
| 3015 | window->assertNoEvents(); |
| 3016 | } |
| 3017 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3018 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3019 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3020 | sp<FakeWindowHandle> window = |
| 3021 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 3022 | |
| 3023 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3024 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3025 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3026 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3027 | setFocusedWindow(window); |
| 3028 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3029 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 3030 | |
| 3031 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 3032 | mDispatcher->notifyKey(&keyArgs); |
| 3033 | |
| 3034 | InputEvent* event = window->consume(); |
| 3035 | ASSERT_NE(event, nullptr); |
| 3036 | |
| 3037 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 3038 | ASSERT_NE(verified, nullptr); |
| 3039 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 3040 | |
| 3041 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 3042 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 3043 | ASSERT_EQ(keyArgs.source, verified->source); |
| 3044 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 3045 | |
| 3046 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 3047 | |
| 3048 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3049 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 3050 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3051 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 3052 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 3053 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 3054 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 3055 | } |
| 3056 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3057 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3058 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3059 | sp<FakeWindowHandle> window = |
| 3060 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 3061 | |
| 3062 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3063 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3064 | ui::Transform transform; |
| 3065 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 3066 | |
| 3067 | gui::DisplayInfo displayInfo; |
| 3068 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 3069 | displayInfo.transform = transform; |
| 3070 | |
| 3071 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3072 | |
| 3073 | NotifyMotionArgs motionArgs = |
| 3074 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3075 | ADISPLAY_ID_DEFAULT); |
| 3076 | mDispatcher->notifyMotion(&motionArgs); |
| 3077 | |
| 3078 | InputEvent* event = window->consume(); |
| 3079 | ASSERT_NE(event, nullptr); |
| 3080 | |
| 3081 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 3082 | ASSERT_NE(verified, nullptr); |
| 3083 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 3084 | |
| 3085 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 3086 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 3087 | EXPECT_EQ(motionArgs.source, verified->source); |
| 3088 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 3089 | |
| 3090 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 3091 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3092 | const vec2 rawXY = |
| 3093 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 3094 | motionArgs.pointerCoords[0].getXYValue()); |
| 3095 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 3096 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3097 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3098 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 3099 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3100 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 3101 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 3102 | } |
| 3103 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3104 | /** |
| 3105 | * Ensure that separate calls to sign the same data are generating the same key. |
| 3106 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 3107 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 3108 | * tests. |
| 3109 | */ |
| 3110 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 3111 | KeyEvent event = getTestKeyEvent(); |
| 3112 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 3113 | |
| 3114 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 3115 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 3116 | ASSERT_EQ(hmac1, hmac2); |
| 3117 | } |
| 3118 | |
| 3119 | /** |
| 3120 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 3121 | */ |
| 3122 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 3123 | KeyEvent event = getTestKeyEvent(); |
| 3124 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 3125 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 3126 | |
| 3127 | verifiedEvent.deviceId += 1; |
| 3128 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3129 | |
| 3130 | verifiedEvent.source += 1; |
| 3131 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3132 | |
| 3133 | verifiedEvent.eventTimeNanos += 1; |
| 3134 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3135 | |
| 3136 | verifiedEvent.displayId += 1; |
| 3137 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3138 | |
| 3139 | verifiedEvent.action += 1; |
| 3140 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3141 | |
| 3142 | verifiedEvent.downTimeNanos += 1; |
| 3143 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3144 | |
| 3145 | verifiedEvent.flags += 1; |
| 3146 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3147 | |
| 3148 | verifiedEvent.keyCode += 1; |
| 3149 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3150 | |
| 3151 | verifiedEvent.scanCode += 1; |
| 3152 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3153 | |
| 3154 | verifiedEvent.metaState += 1; |
| 3155 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3156 | |
| 3157 | verifiedEvent.repeatCount += 1; |
| 3158 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3159 | } |
| 3160 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3161 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 3162 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3163 | sp<FakeWindowHandle> windowTop = |
| 3164 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 3165 | sp<FakeWindowHandle> windowSecond = |
| 3166 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 3167 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3168 | |
| 3169 | // Top window is also focusable but is not granted focus. |
| 3170 | windowTop->setFocusable(true); |
| 3171 | windowSecond->setFocusable(true); |
| 3172 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 3173 | setFocusedWindow(windowSecond); |
| 3174 | |
| 3175 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3176 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3177 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3178 | |
| 3179 | // Focused window should receive event. |
| 3180 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 3181 | windowTop->assertNoEvents(); |
| 3182 | } |
| 3183 | |
| 3184 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 3185 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3186 | sp<FakeWindowHandle> window = |
| 3187 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 3188 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3189 | |
| 3190 | window->setFocusable(true); |
| 3191 | // Release channel for window is no longer valid. |
| 3192 | window->releaseChannel(); |
| 3193 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3194 | setFocusedWindow(window); |
| 3195 | |
| 3196 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3197 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3198 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3199 | |
| 3200 | // window channel is invalid, so it should not receive any input event. |
| 3201 | window->assertNoEvents(); |
| 3202 | } |
| 3203 | |
| 3204 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 3205 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3206 | sp<FakeWindowHandle> window = |
| 3207 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3208 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3209 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3210 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3211 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3212 | setFocusedWindow(window); |
| 3213 | |
| 3214 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3215 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3216 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3217 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3218 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3219 | window->assertNoEvents(); |
| 3220 | } |
| 3221 | |
| 3222 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 3223 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3224 | sp<FakeWindowHandle> windowTop = |
| 3225 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 3226 | sp<FakeWindowHandle> windowSecond = |
| 3227 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 3228 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3229 | |
| 3230 | windowTop->setFocusable(true); |
| 3231 | windowSecond->setFocusable(true); |
| 3232 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 3233 | setFocusedWindow(windowTop); |
| 3234 | windowTop->consumeFocusEvent(true); |
| 3235 | |
| 3236 | setFocusedWindow(windowSecond, windowTop); |
| 3237 | windowSecond->consumeFocusEvent(true); |
| 3238 | windowTop->consumeFocusEvent(false); |
| 3239 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3240 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3241 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3242 | |
| 3243 | // Focused window should receive event. |
| 3244 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 3245 | } |
| 3246 | |
| 3247 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { |
| 3248 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3249 | sp<FakeWindowHandle> windowTop = |
| 3250 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 3251 | sp<FakeWindowHandle> windowSecond = |
| 3252 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 3253 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3254 | |
| 3255 | windowTop->setFocusable(true); |
| 3256 | windowSecond->setFocusable(true); |
| 3257 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 3258 | setFocusedWindow(windowSecond, windowTop); |
| 3259 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3260 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3261 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3262 | |
| 3263 | // Event should be dropped. |
| 3264 | windowTop->assertNoEvents(); |
| 3265 | windowSecond->assertNoEvents(); |
| 3266 | } |
| 3267 | |
| 3268 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 3269 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3270 | sp<FakeWindowHandle> window = |
| 3271 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 3272 | sp<FakeWindowHandle> previousFocusedWindow = |
| 3273 | new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow", |
| 3274 | ADISPLAY_ID_DEFAULT); |
| 3275 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3276 | |
| 3277 | window->setFocusable(true); |
| 3278 | previousFocusedWindow->setFocusable(true); |
| 3279 | window->setVisible(false); |
| 3280 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 3281 | setFocusedWindow(previousFocusedWindow); |
| 3282 | previousFocusedWindow->consumeFocusEvent(true); |
| 3283 | |
| 3284 | // Requesting focus on invisible window takes focus from currently focused window. |
| 3285 | setFocusedWindow(window); |
| 3286 | previousFocusedWindow->consumeFocusEvent(false); |
| 3287 | |
| 3288 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3289 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3290 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3291 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3292 | |
| 3293 | // Window does not get focus event or key down. |
| 3294 | window->assertNoEvents(); |
| 3295 | |
| 3296 | // Window becomes visible. |
| 3297 | window->setVisible(true); |
| 3298 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3299 | |
| 3300 | // Window receives focus event. |
| 3301 | window->consumeFocusEvent(true); |
| 3302 | // Focused window receives key down. |
| 3303 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3304 | } |
| 3305 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 3306 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 3307 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3308 | sp<FakeWindowHandle> window = |
| 3309 | new FakeWindowHandle(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
| 3310 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3311 | |
| 3312 | // window is granted focus. |
| 3313 | window->setFocusable(true); |
| 3314 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3315 | setFocusedWindow(window); |
| 3316 | window->consumeFocusEvent(true); |
| 3317 | |
| 3318 | // When a display is removed window loses focus. |
| 3319 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 3320 | window->consumeFocusEvent(false); |
| 3321 | } |
| 3322 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3323 | /** |
| 3324 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 3325 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 3326 | * of the 'slipperyEnterWindow'. |
| 3327 | * |
| 3328 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 3329 | * a way so that the touched location is no longer covered by the top window. |
| 3330 | * |
| 3331 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 3332 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 3333 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 3334 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 3335 | * with ACTION_DOWN). |
| 3336 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 3337 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 3338 | * |
| 3339 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 3340 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 3341 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 3342 | * |
| 3343 | * In this test, we ensure that the event received by the bottom window has |
| 3344 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 3345 | */ |
| 3346 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
| 3347 | constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1; |
| 3348 | constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1; |
| 3349 | |
| 3350 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3351 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3352 | |
| 3353 | sp<FakeWindowHandle> slipperyExitWindow = |
| 3354 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3355 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3356 | // Make sure this one overlaps the bottom window |
| 3357 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 3358 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 3359 | // one. Windows with the same owner are not considered to be occluding each other. |
| 3360 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 3361 | |
| 3362 | sp<FakeWindowHandle> slipperyEnterWindow = |
| 3363 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 3364 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3365 | |
| 3366 | mDispatcher->setInputWindows( |
| 3367 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 3368 | |
| 3369 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
| 3370 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3371 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 3372 | mDispatcher->notifyMotion(&args); |
| 3373 | slipperyExitWindow->consumeMotionDown(); |
| 3374 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 3375 | mDispatcher->setInputWindows( |
| 3376 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 3377 | |
| 3378 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3379 | ADISPLAY_ID_DEFAULT, {{51, 51}}); |
| 3380 | mDispatcher->notifyMotion(&args); |
| 3381 | |
| 3382 | slipperyExitWindow->consumeMotionCancel(); |
| 3383 | |
| 3384 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 3385 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 3386 | } |
| 3387 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3388 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 3389 | protected: |
| 3390 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 3391 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 3392 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3393 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3394 | sp<FakeWindowHandle> mWindow; |
| 3395 | |
| 3396 | virtual void SetUp() override { |
| 3397 | mFakePolicy = new FakeInputDispatcherPolicy(); |
| 3398 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3399 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3400 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 3401 | ASSERT_EQ(OK, mDispatcher->start()); |
| 3402 | |
| 3403 | setUpWindow(); |
| 3404 | } |
| 3405 | |
| 3406 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3407 | mApp = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3408 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3409 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3410 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3411 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3412 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3413 | mWindow->consumeFocusEvent(true); |
| 3414 | } |
| 3415 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3416 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3417 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3418 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3419 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
| 3420 | mDispatcher->notifyKey(&keyArgs); |
| 3421 | |
| 3422 | // Window should receive key down event. |
| 3423 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3424 | } |
| 3425 | |
| 3426 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 3427 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 3428 | InputEvent* repeatEvent = mWindow->consume(); |
| 3429 | ASSERT_NE(nullptr, repeatEvent); |
| 3430 | |
| 3431 | uint32_t eventType = repeatEvent->getType(); |
| 3432 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); |
| 3433 | |
| 3434 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 3435 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 3436 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 3437 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 3438 | } |
| 3439 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3440 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3441 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3442 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3443 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
| 3444 | mDispatcher->notifyKey(&keyArgs); |
| 3445 | |
| 3446 | // Window should receive key down event. |
| 3447 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 3448 | 0 /*expectedFlags*/); |
| 3449 | } |
| 3450 | }; |
| 3451 | |
| 3452 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3453 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 3454 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 3455 | expectKeyRepeatOnce(repeatCount); |
| 3456 | } |
| 3457 | } |
| 3458 | |
| 3459 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
| 3460 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 3461 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 3462 | expectKeyRepeatOnce(repeatCount); |
| 3463 | } |
| 3464 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 3465 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3466 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 3467 | expectKeyRepeatOnce(repeatCount); |
| 3468 | } |
| 3469 | } |
| 3470 | |
| 3471 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3472 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3473 | expectKeyRepeatOnce(1 /*repeatCount*/); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3474 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 3475 | mWindow->assertNoEvents(); |
| 3476 | } |
| 3477 | |
| 3478 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
| 3479 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 3480 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 3481 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 3482 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 3483 | // Stale key up from device 1. |
| 3484 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 3485 | // Device 2 is still down, keep repeating |
| 3486 | expectKeyRepeatOnce(2 /*repeatCount*/); |
| 3487 | expectKeyRepeatOnce(3 /*repeatCount*/); |
| 3488 | // Device 2 key up |
| 3489 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 3490 | mWindow->assertNoEvents(); |
| 3491 | } |
| 3492 | |
| 3493 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
| 3494 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 3495 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 3496 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 3497 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 3498 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. |
| 3499 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 3500 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3501 | mWindow->assertNoEvents(); |
| 3502 | } |
| 3503 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 3504 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 3505 | sendAndConsumeKeyDown(DEVICE_ID); |
| 3506 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 3507 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
| 3508 | mDispatcher->notifyDeviceReset(&args); |
| 3509 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 3510 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 3511 | mWindow->assertNoEvents(); |
| 3512 | } |
| 3513 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3514 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3515 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3516 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 3517 | InputEvent* repeatEvent = mWindow->consume(); |
| 3518 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 3519 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 3520 | IdGenerator::getSource(repeatEvent->getId())); |
| 3521 | } |
| 3522 | } |
| 3523 | |
| 3524 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3525 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3526 | |
| 3527 | std::unordered_set<int32_t> idSet; |
| 3528 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 3529 | InputEvent* repeatEvent = mWindow->consume(); |
| 3530 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 3531 | int32_t id = repeatEvent->getId(); |
| 3532 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 3533 | idSet.insert(id); |
| 3534 | } |
| 3535 | } |
| 3536 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3537 | /* Test InputDispatcher for MultiDisplay */ |
| 3538 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 3539 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3540 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3541 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3542 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3543 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3544 | windowInPrimary = |
| 3545 | new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3546 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3547 | // Set focus window for primary display, but focused display would be second one. |
| 3548 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3549 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3550 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3551 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3552 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3553 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3554 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3555 | windowInSecondary = |
| 3556 | new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3557 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3558 | // Set focus display to second one. |
| 3559 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 3560 | // Set focus window for second display. |
| 3561 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3562 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3563 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3564 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3565 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3566 | } |
| 3567 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3568 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3569 | InputDispatcherTest::TearDown(); |
| 3570 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3571 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3572 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3573 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3574 | windowInSecondary.clear(); |
| 3575 | } |
| 3576 | |
| 3577 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3578 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3579 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3580 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3581 | sp<FakeWindowHandle> windowInSecondary; |
| 3582 | }; |
| 3583 | |
| 3584 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 3585 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3586 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3587 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 3588 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3589 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3590 | windowInSecondary->assertNoEvents(); |
| 3591 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3592 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3593 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3594 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 3595 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3596 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3597 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3598 | } |
| 3599 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3600 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3601 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3602 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3603 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3604 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3605 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3606 | windowInSecondary->assertNoEvents(); |
| 3607 | |
| 3608 | // Test inject a key down without display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3609 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3610 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3611 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3612 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3613 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3614 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3615 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3616 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3617 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3618 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
| 3619 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3620 | |
| 3621 | // Test inject a key down, should timeout because of no target window. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3622 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3623 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3624 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3625 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3626 | windowInSecondary->assertNoEvents(); |
| 3627 | } |
| 3628 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3629 | // Test per-display input monitors for motion event. |
| 3630 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3631 | FakeMonitorReceiver monitorInPrimary = |
| 3632 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 3633 | FakeMonitorReceiver monitorInSecondary = |
| 3634 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3635 | |
| 3636 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3637 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3638 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 3639 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3640 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3641 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3642 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3643 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3644 | |
| 3645 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3646 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3647 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 3648 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3649 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3650 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3651 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3652 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3653 | |
| 3654 | // Test inject a non-pointer motion event. |
| 3655 | // If specific a display, it will dispatch to the focused window of particular display, |
| 3656 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3657 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3658 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 3659 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3660 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3661 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3662 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3663 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3664 | } |
| 3665 | |
| 3666 | // Test per-display input monitors for key event. |
| 3667 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3668 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3669 | FakeMonitorReceiver monitorInPrimary = |
| 3670 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 3671 | FakeMonitorReceiver monitorInSecondary = |
| 3672 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3673 | |
| 3674 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3675 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3676 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3677 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3678 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3679 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3680 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3681 | } |
| 3682 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3683 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 3684 | sp<FakeWindowHandle> secondWindowInPrimary = |
| 3685 | new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
| 3686 | secondWindowInPrimary->setFocusable(true); |
| 3687 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 3688 | setFocusedWindow(secondWindowInPrimary); |
| 3689 | windowInPrimary->consumeFocusEvent(false); |
| 3690 | secondWindowInPrimary->consumeFocusEvent(true); |
| 3691 | |
| 3692 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3693 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 3694 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3695 | windowInPrimary->assertNoEvents(); |
| 3696 | windowInSecondary->assertNoEvents(); |
| 3697 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3698 | } |
| 3699 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 3700 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 3701 | FakeMonitorReceiver monitorInPrimary = |
| 3702 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 3703 | FakeMonitorReceiver monitorInSecondary = |
| 3704 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 3705 | |
| 3706 | // Test touch down on primary display. |
| 3707 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3708 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 3709 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3710 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3711 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3712 | |
| 3713 | // Test touch down on second display. |
| 3714 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3715 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 3716 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3717 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 3718 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 3719 | |
| 3720 | // Trigger cancel touch. |
| 3721 | mDispatcher->cancelCurrentTouch(); |
| 3722 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 3723 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 3724 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 3725 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 3726 | |
| 3727 | // Test inject a move motion event, no window/monitor should receive the event. |
| 3728 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 3729 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3730 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 3731 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 3732 | windowInPrimary->assertNoEvents(); |
| 3733 | monitorInPrimary.assertNoEvents(); |
| 3734 | |
| 3735 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 3736 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3737 | SECOND_DISPLAY_ID, {110, 200})) |
| 3738 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 3739 | windowInSecondary->assertNoEvents(); |
| 3740 | monitorInSecondary.assertNoEvents(); |
| 3741 | } |
| 3742 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3743 | class InputFilterTest : public InputDispatcherTest { |
| 3744 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3745 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 3746 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3747 | NotifyMotionArgs motionArgs; |
| 3748 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3749 | motionArgs = |
| 3750 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3751 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3752 | motionArgs = |
| 3753 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3754 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3755 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3756 | if (expectToBeFiltered) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3757 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); |
| 3758 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3759 | } else { |
| 3760 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 3761 | } |
| 3762 | } |
| 3763 | |
| 3764 | void testNotifyKey(bool expectToBeFiltered) { |
| 3765 | NotifyKeyArgs keyArgs; |
| 3766 | |
| 3767 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 3768 | mDispatcher->notifyKey(&keyArgs); |
| 3769 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 3770 | mDispatcher->notifyKey(&keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3771 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3772 | |
| 3773 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 3774 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3775 | } else { |
| 3776 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 3777 | } |
| 3778 | } |
| 3779 | }; |
| 3780 | |
| 3781 | // Test InputFilter for MotionEvent |
| 3782 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 3783 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 3784 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 3785 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 3786 | |
| 3787 | // Enable InputFilter |
| 3788 | mDispatcher->setInputFilterEnabled(true); |
| 3789 | // Test touch on both primary and second display, and check if both events are filtered. |
| 3790 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 3791 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 3792 | |
| 3793 | // Disable InputFilter |
| 3794 | mDispatcher->setInputFilterEnabled(false); |
| 3795 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 3796 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 3797 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 3798 | } |
| 3799 | |
| 3800 | // Test InputFilter for KeyEvent |
| 3801 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 3802 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 3803 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 3804 | |
| 3805 | // Enable InputFilter |
| 3806 | mDispatcher->setInputFilterEnabled(true); |
| 3807 | // Send a key event, and check if it is filtered. |
| 3808 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 3809 | |
| 3810 | // Disable InputFilter |
| 3811 | mDispatcher->setInputFilterEnabled(false); |
| 3812 | // Send a key event, and check if it isn't filtered. |
| 3813 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 3814 | } |
| 3815 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3816 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 3817 | // logical display coordinate space. |
| 3818 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 3819 | ui::Transform firstDisplayTransform; |
| 3820 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 3821 | ui::Transform secondDisplayTransform; |
| 3822 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 3823 | |
| 3824 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 3825 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 3826 | displayInfos[0].transform = firstDisplayTransform; |
| 3827 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 3828 | displayInfos[1].transform = secondDisplayTransform; |
| 3829 | |
| 3830 | mDispatcher->onWindowInfosChanged({}, displayInfos); |
| 3831 | |
| 3832 | // Enable InputFilter |
| 3833 | mDispatcher->setInputFilterEnabled(true); |
| 3834 | |
| 3835 | // Ensure the correct transforms are used for the displays. |
| 3836 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); |
| 3837 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); |
| 3838 | } |
| 3839 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3840 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 3841 | protected: |
| 3842 | virtual void SetUp() override { |
| 3843 | InputDispatcherTest::SetUp(); |
| 3844 | |
| 3845 | /** |
| 3846 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 3847 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 3848 | * on. |
| 3849 | */ |
| 3850 | mDispatcher->setInputFilterEnabled(true); |
| 3851 | |
| 3852 | std::shared_ptr<InputApplicationHandle> application = |
| 3853 | std::make_shared<FakeApplicationHandle>(); |
| 3854 | mWindow = |
| 3855 | new FakeWindowHandle(application, mDispatcher, "Test Window", ADISPLAY_ID_DEFAULT); |
| 3856 | |
| 3857 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3858 | mWindow->setFocusable(true); |
| 3859 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3860 | setFocusedWindow(mWindow); |
| 3861 | mWindow->consumeFocusEvent(true); |
| 3862 | } |
| 3863 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3864 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 3865 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3866 | KeyEvent event; |
| 3867 | |
| 3868 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 3869 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 3870 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
| 3871 | KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime); |
| 3872 | const int32_t additionalPolicyFlags = |
| 3873 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 3874 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3875 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
| 3876 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 3877 | policyFlags | additionalPolicyFlags)); |
| 3878 | |
| 3879 | InputEvent* received = mWindow->consume(); |
| 3880 | ASSERT_NE(nullptr, received); |
| 3881 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3882 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY); |
| 3883 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); |
| 3884 | ASSERT_EQ(flags, keyEvent.getFlags()); |
| 3885 | } |
| 3886 | |
| 3887 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 3888 | int32_t flags) { |
| 3889 | MotionEvent event; |
| 3890 | PointerProperties pointerProperties[1]; |
| 3891 | PointerCoords pointerCoords[1]; |
| 3892 | pointerProperties[0].clear(); |
| 3893 | pointerProperties[0].id = 0; |
| 3894 | pointerCoords[0].clear(); |
| 3895 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 3896 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 3897 | |
| 3898 | ui::Transform identityTransform; |
| 3899 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 3900 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 3901 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 3902 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 3903 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3904 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 3905 | eventTime, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3906 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 3907 | |
| 3908 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 3909 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3910 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
| 3911 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 3912 | policyFlags | additionalPolicyFlags)); |
| 3913 | |
| 3914 | InputEvent* received = mWindow->consume(); |
| 3915 | ASSERT_NE(nullptr, received); |
| 3916 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
| 3917 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 3918 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); |
| 3919 | ASSERT_EQ(flags, motionEvent.getFlags()); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3920 | } |
| 3921 | |
| 3922 | private: |
| 3923 | sp<FakeWindowHandle> mWindow; |
| 3924 | }; |
| 3925 | |
| 3926 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3927 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 3928 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 3929 | // injected device id will be overwritten. |
| 3930 | testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 3931 | 0 /*flags*/); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3934 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3935 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3936 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 3937 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 3938 | } |
| 3939 | |
| 3940 | TEST_F(InputFilterInjectionPolicyTest, |
| 3941 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 3942 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
| 3943 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 3944 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
| 3947 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
| 3948 | testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3949 | VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3950 | } |
| 3951 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3952 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3953 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3954 | InputDispatcherTest::SetUp(); |
| 3955 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3956 | std::shared_ptr<FakeApplicationHandle> application = |
| 3957 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3958 | mUnfocusedWindow = |
| 3959 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3960 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3961 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3962 | mFocusedWindow = |
| 3963 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 3964 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3965 | |
| 3966 | // Set focused application. |
| 3967 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3968 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3969 | |
| 3970 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3971 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3972 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3973 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3974 | } |
| 3975 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3976 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3977 | InputDispatcherTest::TearDown(); |
| 3978 | |
| 3979 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3980 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3981 | } |
| 3982 | |
| 3983 | protected: |
| 3984 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3985 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3986 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3987 | }; |
| 3988 | |
| 3989 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 3990 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 3991 | // the onPointerDownOutsideFocus callback. |
| 3992 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3993 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3994 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3995 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3996 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3997 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3998 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3999 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4000 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 4001 | } |
| 4002 | |
| 4003 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 4004 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 4005 | // onPointerDownOutsideFocus callback. |
| 4006 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4007 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4008 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4009 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4010 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4011 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4012 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4013 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4014 | } |
| 4015 | |
| 4016 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 4017 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 4018 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4019 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4020 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4021 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4022 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4023 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4024 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4025 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4026 | } |
| 4027 | |
| 4028 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 4029 | // DOWN on the window that already has focus. Ensure no window received the |
| 4030 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4031 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4032 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4033 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4034 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4035 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4036 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4037 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4038 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4039 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4040 | } |
| 4041 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 4042 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 4043 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 4044 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 4045 | const MotionEvent event = |
| 4046 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4047 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 4048 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20)) |
| 4049 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 4050 | .build(); |
| 4051 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) |
| 4052 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4053 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 4054 | |
| 4055 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4056 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 4057 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 4058 | mUnfocusedWindow->assertNoEvents(); |
| 4059 | } |
| 4060 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4061 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 4062 | // windows that point to the same client token. |
| 4063 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 4064 | virtual void SetUp() override { |
| 4065 | InputDispatcherTest::SetUp(); |
| 4066 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4067 | std::shared_ptr<FakeApplicationHandle> application = |
| 4068 | std::make_shared<FakeApplicationHandle>(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4069 | mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1", |
| 4070 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4071 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 4072 | |
| 4073 | mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2", |
| 4074 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4075 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 4076 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4077 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4078 | } |
| 4079 | |
| 4080 | protected: |
| 4081 | sp<FakeWindowHandle> mWindow1; |
| 4082 | sp<FakeWindowHandle> mWindow2; |
| 4083 | |
| 4084 | // Helper function to convert the point from screen coordinates into the window's space |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4085 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4086 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 4087 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4088 | } |
| 4089 | |
| 4090 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 4091 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4092 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4093 | InputEvent* event = window->consume(); |
| 4094 | |
| 4095 | ASSERT_NE(nullptr, event) << name.c_str() |
| 4096 | << ": consumer should have returned non-NULL event."; |
| 4097 | |
| 4098 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 4099 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 4100 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 4101 | |
| 4102 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4103 | assertMotionAction(expectedAction, motionEvent.getAction()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4104 | |
| 4105 | for (size_t i = 0; i < points.size(); i++) { |
| 4106 | float expectedX = points[i].x; |
| 4107 | float expectedY = points[i].y; |
| 4108 | |
| 4109 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 4110 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 4111 | << ", got " << motionEvent.getX(i); |
| 4112 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 4113 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 4114 | << ", got " << motionEvent.getY(i); |
| 4115 | } |
| 4116 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4117 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 4118 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4119 | std::vector<PointF> expectedPoints) { |
| 4120 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 4121 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 4122 | mDispatcher->notifyMotion(&motionArgs); |
| 4123 | |
| 4124 | // Always consume from window1 since it's the window that has the InputReceiver |
| 4125 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 4126 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4127 | }; |
| 4128 | |
| 4129 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 4130 | // Touch Window 1 |
| 4131 | PointF touchedPoint = {10, 10}; |
| 4132 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4133 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4134 | |
| 4135 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4136 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4137 | |
| 4138 | // Touch Window 2 |
| 4139 | touchedPoint = {150, 150}; |
| 4140 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4141 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4142 | } |
| 4143 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4144 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 4145 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4146 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4147 | |
| 4148 | // Touch Window 1 |
| 4149 | PointF touchedPoint = {10, 10}; |
| 4150 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4151 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4152 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4153 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4154 | |
| 4155 | // Touch Window 2 |
| 4156 | touchedPoint = {150, 150}; |
| 4157 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4158 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 4159 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4160 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4161 | // Update the transform so rotation is set |
| 4162 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4163 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 4164 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4165 | } |
| 4166 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4167 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4168 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4169 | |
| 4170 | // Touch Window 1 |
| 4171 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4172 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4173 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4174 | |
| 4175 | // Touch Window 2 |
| 4176 | int32_t actionPointerDown = |
| 4177 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4178 | touchedPoints.push_back(PointF{150, 150}); |
| 4179 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 4180 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4181 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4182 | // Release Window 2 |
| 4183 | int32_t actionPointerUp = |
| 4184 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 4185 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); |
| 4186 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4187 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4188 | // Update the transform so rotation is set for Window 2 |
| 4189 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4190 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 4191 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4194 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4195 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4196 | |
| 4197 | // Touch Window 1 |
| 4198 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4199 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4200 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4201 | |
| 4202 | // Touch Window 2 |
| 4203 | int32_t actionPointerDown = |
| 4204 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4205 | touchedPoints.push_back(PointF{150, 150}); |
| 4206 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4207 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4208 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4209 | |
| 4210 | // Move both windows |
| 4211 | touchedPoints = {{20, 20}, {175, 175}}; |
| 4212 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 4213 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 4214 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4215 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4216 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4217 | // Release Window 2 |
| 4218 | int32_t actionPointerUp = |
| 4219 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 4220 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); |
| 4221 | expectedPoints.pop_back(); |
| 4222 | |
| 4223 | // Touch Window 2 |
| 4224 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4225 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 4226 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
| 4227 | |
| 4228 | // Move both windows |
| 4229 | touchedPoints = {{20, 20}, {175, 175}}; |
| 4230 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 4231 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 4232 | |
| 4233 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4234 | } |
| 4235 | |
| 4236 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 4237 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 4238 | |
| 4239 | // Touch Window 1 |
| 4240 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4241 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4242 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4243 | |
| 4244 | // Touch Window 2 |
| 4245 | int32_t actionPointerDown = |
| 4246 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4247 | touchedPoints.push_back(PointF{150, 150}); |
| 4248 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4249 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4250 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4251 | |
| 4252 | // Move both windows |
| 4253 | touchedPoints = {{20, 20}, {175, 175}}; |
| 4254 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 4255 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 4256 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4257 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4260 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 4261 | virtual void SetUp() override { |
| 4262 | InputDispatcherTest::SetUp(); |
| 4263 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4264 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4265 | mApplication->setDispatchingTimeout(20ms); |
| 4266 | mWindow = |
| 4267 | new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 4268 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 4269 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4270 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4271 | |
| 4272 | // Set focused application. |
| 4273 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 4274 | |
| 4275 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4276 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4277 | mWindow->consumeFocusEvent(true); |
| 4278 | } |
| 4279 | |
| 4280 | virtual void TearDown() override { |
| 4281 | InputDispatcherTest::TearDown(); |
| 4282 | mWindow.clear(); |
| 4283 | } |
| 4284 | |
| 4285 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4286 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4287 | sp<FakeWindowHandle> mWindow; |
| 4288 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 4289 | |
| 4290 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4291 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4292 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4293 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4294 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4295 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4296 | WINDOW_LOCATION)); |
| 4297 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4298 | |
| 4299 | sp<FakeWindowHandle> addSpyWindow() { |
| 4300 | sp<FakeWindowHandle> spy = |
| 4301 | new FakeWindowHandle(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4302 | spy->setTrustedOverlay(true); |
| 4303 | spy->setFocusable(false); |
| 4304 | spy->setInputFeatures(WindowInfo::Feature::SPY); |
| 4305 | spy->setDispatchingTimeout(30ms); |
| 4306 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); |
| 4307 | return spy; |
| 4308 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4309 | }; |
| 4310 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4311 | // Send a tap and respond, which should not cause an ANR. |
| 4312 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 4313 | tapOnWindow(); |
| 4314 | mWindow->consumeMotionDown(); |
| 4315 | mWindow->consumeMotionUp(); |
| 4316 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4317 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4318 | } |
| 4319 | |
| 4320 | // Send a regular key and respond, which should not cause an ANR. |
| 4321 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4322 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4323 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 4324 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4325 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4326 | } |
| 4327 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4328 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 4329 | mWindow->setFocusable(false); |
| 4330 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4331 | mWindow->consumeFocusEvent(false); |
| 4332 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4333 | InputEventInjectionResult result = |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4334 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4335 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, |
| 4336 | false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4337 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4338 | // Key will not go to window because we have no focused window. |
| 4339 | // The 'no focused window' ANR timer should start instead. |
| 4340 | |
| 4341 | // Now, the focused application goes away. |
| 4342 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 4343 | // The key should get dropped and there should be no ANR. |
| 4344 | |
| 4345 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4346 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4347 | } |
| 4348 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4349 | // Send an event to the app and have the app not respond right away. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4350 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 4351 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4352 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4353 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4354 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4355 | WINDOW_LOCATION)); |
| 4356 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4357 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 4358 | ASSERT_TRUE(sequenceNum); |
| 4359 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4360 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4361 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4362 | mWindow->finishEvent(*sequenceNum); |
| 4363 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 4364 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4365 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4366 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | // Send a key to the app and have the app not respond right away. |
| 4370 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 4371 | // Inject a key, and don't respond - expect that ANR is called. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4372 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4373 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 4374 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4375 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4376 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4377 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4378 | } |
| 4379 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4380 | // We have a focused application, but no focused window |
| 4381 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4382 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4383 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4384 | mWindow->consumeFocusEvent(false); |
| 4385 | |
| 4386 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4387 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4388 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4389 | WINDOW_LOCATION)); |
| 4390 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 4391 | mDispatcher->waitForIdle(); |
| 4392 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4393 | |
| 4394 | // Once a focused event arrives, we get an ANR for this application |
| 4395 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 4396 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4397 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4398 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4399 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4400 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4401 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4402 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4403 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4404 | } |
| 4405 | |
| 4406 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4407 | // Make sure that we don't notify policy twice about the same ANR. |
| 4408 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4409 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4410 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4411 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4412 | |
| 4413 | // Once a focused event arrives, we get an ANR for this application |
| 4414 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 4415 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4416 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4417 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4418 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4419 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4420 | const std::chrono::duration appTimeout = |
| 4421 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4422 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4423 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4424 | std::this_thread::sleep_for(appTimeout); |
| 4425 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 4426 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4427 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4428 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 4429 | // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4430 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4431 | } |
| 4432 | |
| 4433 | // We have a focused application, but no focused window |
| 4434 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4435 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4436 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4437 | mWindow->consumeFocusEvent(false); |
| 4438 | |
| 4439 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4440 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4441 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4442 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 4443 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4444 | |
| 4445 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4446 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4447 | |
| 4448 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4449 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4450 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4451 | mWindow->assertNoEvents(); |
| 4452 | } |
| 4453 | |
| 4454 | /** |
| 4455 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 4456 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 4457 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 4458 | * the ANR mechanism should still work. |
| 4459 | * |
| 4460 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 4461 | * DOWN event, while not responding on the second one. |
| 4462 | */ |
| 4463 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 4464 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4465 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4466 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 4467 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 4468 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4469 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4470 | |
| 4471 | // Now send ACTION_UP, with identical timestamp |
| 4472 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4473 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 4474 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 4475 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4476 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4477 | |
| 4478 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 4479 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4480 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4481 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4482 | } |
| 4483 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4484 | // A spy window can receive an ANR |
| 4485 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 4486 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 4487 | |
| 4488 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4489 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4490 | WINDOW_LOCATION)); |
| 4491 | mWindow->consumeMotionDown(); |
| 4492 | |
| 4493 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 4494 | ASSERT_TRUE(sequenceNum); |
| 4495 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4496 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4497 | |
| 4498 | spy->finishEvent(*sequenceNum); |
| 4499 | spy->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, |
| 4500 | 0 /*flags*/); |
| 4501 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4502 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4503 | } |
| 4504 | |
| 4505 | // If an app is not responding to a key event, spy windows should continue to receive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4506 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4507 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 4508 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4509 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4510 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4511 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4512 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4513 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4514 | |
| 4515 | // Stuck on the ACTION_UP |
| 4516 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4517 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4518 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4519 | // New tap will go to the spy window, but not to the window |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4520 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4521 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4522 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4523 | |
| 4524 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 4525 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4526 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4527 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4528 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4529 | } |
| 4530 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4531 | // If an app is not responding to a motion event, spy windows should continue to receive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4532 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4533 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 4534 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4535 | |
| 4536 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4537 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4538 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4539 | |
| 4540 | mWindow->consumeMotionDown(); |
| 4541 | // Stuck on the ACTION_UP |
| 4542 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4543 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4544 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4545 | // New tap will go to the spy window, but not to the window |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4546 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4547 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4548 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4549 | |
| 4550 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 4551 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4552 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4553 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4554 | spy->assertNoEvents(); |
| 4555 | } |
| 4556 | |
| 4557 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
| 4558 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); |
| 4559 | |
| 4560 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4561 | |
| 4562 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4563 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4564 | WINDOW_LOCATION)); |
| 4565 | |
| 4566 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4567 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 4568 | ASSERT_TRUE(consumeSeq); |
| 4569 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4570 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4571 | |
| 4572 | monitor.finishEvent(*consumeSeq); |
| 4573 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4574 | |
| 4575 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4576 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4577 | } |
| 4578 | |
| 4579 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 4580 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 4581 | // get an ANR again. |
| 4582 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 4583 | // 2. consume all pending events (= queue becomes healthy again) |
| 4584 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 4585 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 4586 | tapOnWindow(); |
| 4587 | |
| 4588 | mWindow->consumeMotionDown(); |
| 4589 | // Block on ACTION_UP |
| 4590 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4591 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4592 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 4593 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4594 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4595 | mWindow->assertNoEvents(); |
| 4596 | |
| 4597 | tapOnWindow(); |
| 4598 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4599 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4600 | mWindow->consumeMotionUp(); |
| 4601 | |
| 4602 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4603 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4604 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4605 | mWindow->assertNoEvents(); |
| 4606 | } |
| 4607 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4608 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 4609 | // it. |
| 4610 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4611 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4612 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4613 | WINDOW_LOCATION)); |
| 4614 | |
| 4615 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4616 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4617 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4618 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 4619 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4620 | // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4621 | mWindow->consumeMotionDown(); |
| 4622 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 4623 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 4624 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4625 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4626 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4627 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | /** |
| 4631 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 4632 | * not to to the focused window until the motion is processed. |
| 4633 | * |
| 4634 | * Warning!!! |
| 4635 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 4636 | * and the injection timeout that we specify when injecting the key. |
| 4637 | * We must have the injection timeout (10ms) be smaller than |
| 4638 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 4639 | * |
| 4640 | * If that value changes, this test should also change. |
| 4641 | */ |
| 4642 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 4643 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 4644 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4645 | |
| 4646 | tapOnWindow(); |
| 4647 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 4648 | ASSERT_TRUE(downSequenceNum); |
| 4649 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 4650 | ASSERT_TRUE(upSequenceNum); |
| 4651 | // Don't finish the events yet, and send a key |
| 4652 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 4653 | // window even if motions are still being processed. But because the injection timeout is short, |
| 4654 | // we will receive INJECTION_TIMED_OUT as the result. |
| 4655 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4656 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4657 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4658 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 4659 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4660 | // Key will not be sent to the window, yet, because the window is still processing events |
| 4661 | // and the key remains pending, waiting for the touch events to be processed |
| 4662 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 4663 | ASSERT_FALSE(keySequenceNum); |
| 4664 | |
| 4665 | std::this_thread::sleep_for(500ms); |
| 4666 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 4667 | // to the focused window, even though we have not yet finished the motion event |
| 4668 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4669 | mWindow->finishEvent(*downSequenceNum); |
| 4670 | mWindow->finishEvent(*upSequenceNum); |
| 4671 | } |
| 4672 | |
| 4673 | /** |
| 4674 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 4675 | * not go to the focused window until the motion is processed. |
| 4676 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 4677 | * focused window right away. |
| 4678 | */ |
| 4679 | TEST_F(InputDispatcherSingleWindowAnr, |
| 4680 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 4681 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 4682 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4683 | |
| 4684 | tapOnWindow(); |
| 4685 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 4686 | ASSERT_TRUE(downSequenceNum); |
| 4687 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 4688 | ASSERT_TRUE(upSequenceNum); |
| 4689 | // Don't finish the events yet, and send a key |
| 4690 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4691 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4692 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4693 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4694 | // At this point, key is still pending, and should not be sent to the application yet. |
| 4695 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 4696 | ASSERT_FALSE(keySequenceNum); |
| 4697 | |
| 4698 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 4699 | tapOnWindow(); |
| 4700 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 4701 | // the other events yet. We can finish events in any order. |
| 4702 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 4703 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 4704 | mWindow->consumeMotionDown(); |
| 4705 | mWindow->consumeMotionUp(); |
| 4706 | mWindow->assertNoEvents(); |
| 4707 | } |
| 4708 | |
| 4709 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 4710 | virtual void SetUp() override { |
| 4711 | InputDispatcherTest::SetUp(); |
| 4712 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4713 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4714 | mApplication->setDispatchingTimeout(10ms); |
| 4715 | mUnfocusedWindow = |
| 4716 | new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT); |
| 4717 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4718 | // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4719 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4720 | |
| 4721 | mFocusedWindow = |
| 4722 | new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 4723 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4724 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4725 | |
| 4726 | // Set focused application. |
| 4727 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4728 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4729 | |
| 4730 | // Expect one focus window exist in display. |
| 4731 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4732 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4733 | mFocusedWindow->consumeFocusEvent(true); |
| 4734 | } |
| 4735 | |
| 4736 | virtual void TearDown() override { |
| 4737 | InputDispatcherTest::TearDown(); |
| 4738 | |
| 4739 | mUnfocusedWindow.clear(); |
| 4740 | mFocusedWindow.clear(); |
| 4741 | } |
| 4742 | |
| 4743 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4744 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4745 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 4746 | sp<FakeWindowHandle> mFocusedWindow; |
| 4747 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 4748 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 4749 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 4750 | |
| 4751 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 4752 | |
| 4753 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 4754 | |
| 4755 | private: |
| 4756 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4757 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4758 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4759 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4760 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4761 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4762 | location)); |
| 4763 | } |
| 4764 | }; |
| 4765 | |
| 4766 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 4767 | // should be ANR'd first. |
| 4768 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4769 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4770 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4771 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4772 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4773 | mFocusedWindow->consumeMotionDown(); |
| 4774 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 4775 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 4776 | // We consumed all events, so no ANR |
| 4777 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4778 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4779 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4780 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4781 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4782 | FOCUSED_WINDOW_LOCATION)); |
| 4783 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 4784 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4785 | |
| 4786 | const std::chrono::duration timeout = |
| 4787 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4788 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4789 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 4790 | // sequence to make it consistent |
| 4791 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4792 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4793 | mFocusedWindow->consumeMotionDown(); |
| 4794 | // This cancel is generated because the connection was unresponsive |
| 4795 | mFocusedWindow->consumeMotionCancel(); |
| 4796 | mFocusedWindow->assertNoEvents(); |
| 4797 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4798 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4799 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 4800 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4801 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4802 | } |
| 4803 | |
| 4804 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 4805 | // it doesn't matter which order they should have ANR. |
| 4806 | // But we should receive ANR for both. |
| 4807 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 4808 | // Set the timeout for unfocused window to match the focused window |
| 4809 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 4810 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 4811 | |
| 4812 | tapOnFocusedWindow(); |
| 4813 | // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4814 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; |
| 4815 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); |
| 4816 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4817 | |
| 4818 | // We don't know which window will ANR first. But both of them should happen eventually. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4819 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 4820 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 4821 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 4822 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4823 | |
| 4824 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4825 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4826 | |
| 4827 | mFocusedWindow->consumeMotionDown(); |
| 4828 | mFocusedWindow->consumeMotionUp(); |
| 4829 | mUnfocusedWindow->consumeMotionOutside(); |
| 4830 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4831 | sp<IBinder> responsiveToken1, responsiveToken2; |
| 4832 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); |
| 4833 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4834 | |
| 4835 | // Both applications should be marked as responsive, in any order |
| 4836 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 4837 | mFocusedWindow->getToken() == responsiveToken2); |
| 4838 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 4839 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 4840 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4841 | } |
| 4842 | |
| 4843 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 4844 | // We should also log an error to account for the dropped event (not tested here). |
| 4845 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 4846 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 4847 | tapOnFocusedWindow(); |
| 4848 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 4849 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 4850 | // Receive the events, but don't respond |
| 4851 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 4852 | ASSERT_TRUE(downEventSequenceNum); |
| 4853 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 4854 | ASSERT_TRUE(upEventSequenceNum); |
| 4855 | const std::chrono::duration timeout = |
| 4856 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4857 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4858 | |
| 4859 | // Tap once again |
| 4860 | // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4861 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4862 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4863 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4864 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4865 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4866 | FOCUSED_WINDOW_LOCATION)); |
| 4867 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 4868 | // valid touch target |
| 4869 | mUnfocusedWindow->assertNoEvents(); |
| 4870 | |
| 4871 | // Consume the first tap |
| 4872 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 4873 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 4874 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4875 | // The second tap did not go to the focused window |
| 4876 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4877 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4878 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 4879 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4880 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4881 | } |
| 4882 | |
| 4883 | // If you tap outside of all windows, there will not be ANR |
| 4884 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4885 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4886 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4887 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 4888 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4889 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4890 | } |
| 4891 | |
| 4892 | // Since the focused window is paused, tapping on it should not produce any events |
| 4893 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 4894 | mFocusedWindow->setPaused(true); |
| 4895 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 4896 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4897 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4898 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4899 | FOCUSED_WINDOW_LOCATION)); |
| 4900 | |
| 4901 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 4902 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4903 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 4904 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4905 | |
| 4906 | mFocusedWindow->assertNoEvents(); |
| 4907 | mUnfocusedWindow->assertNoEvents(); |
| 4908 | } |
| 4909 | |
| 4910 | /** |
| 4911 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 4912 | * not to to the focused window until the motion is processed. |
| 4913 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 4914 | * |
| 4915 | * Warning!!! |
| 4916 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 4917 | * and the injection timeout that we specify when injecting the key. |
| 4918 | * We must have the injection timeout (10ms) be smaller than |
| 4919 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 4920 | * |
| 4921 | * If that value changes, this test should also change. |
| 4922 | */ |
| 4923 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 4924 | // Set a long ANR timeout to prevent it from triggering |
| 4925 | mFocusedWindow->setDispatchingTimeout(2s); |
| 4926 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 4927 | |
| 4928 | tapOnUnfocusedWindow(); |
| 4929 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 4930 | ASSERT_TRUE(downSequenceNum); |
| 4931 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 4932 | ASSERT_TRUE(upSequenceNum); |
| 4933 | // Don't finish the events yet, and send a key |
| 4934 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 4935 | // window even if motions are still being processed. |
| 4936 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4937 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4938 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4939 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 4940 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4941 | // Key will not be sent to the window, yet, because the window is still processing events |
| 4942 | // and the key remains pending, waiting for the touch events to be processed |
| 4943 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 4944 | ASSERT_FALSE(keySequenceNum); |
| 4945 | |
| 4946 | // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4947 | mFocusedWindow->setFocusable(false); |
| 4948 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4949 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4950 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4951 | |
| 4952 | // Focus events should precede the key events |
| 4953 | mUnfocusedWindow->consumeFocusEvent(true); |
| 4954 | mFocusedWindow->consumeFocusEvent(false); |
| 4955 | |
| 4956 | // Finish the tap events, which should unblock dispatcher |
| 4957 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 4958 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 4959 | |
| 4960 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 4961 | // can finally go to the newly focused "mUnfocusedWindow". |
| 4962 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4963 | mFocusedWindow->assertNoEvents(); |
| 4964 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4965 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4966 | } |
| 4967 | |
| 4968 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 4969 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 4970 | // The other window should not be affected by that. |
| 4971 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 4972 | // Touch Window 1 |
| 4973 | NotifyMotionArgs motionArgs = |
| 4974 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4975 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); |
| 4976 | mDispatcher->notifyMotion(&motionArgs); |
| 4977 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 4978 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 4979 | |
| 4980 | // Touch Window 2 |
| 4981 | int32_t actionPointerDown = |
| 4982 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 4983 | |
| 4984 | motionArgs = |
| 4985 | generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4986 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); |
| 4987 | mDispatcher->notifyMotion(&motionArgs); |
| 4988 | |
| 4989 | const std::chrono::duration timeout = |
| 4990 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 4991 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4992 | |
| 4993 | mUnfocusedWindow->consumeMotionDown(); |
| 4994 | mFocusedWindow->consumeMotionDown(); |
| 4995 | // Focused window may or may not receive ACTION_MOVE |
| 4996 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 4997 | InputEvent* event; |
| 4998 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 4999 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 5000 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 5001 | ASSERT_NE(nullptr, event); |
| 5002 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 5003 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 5004 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 5005 | mFocusedWindow->consumeMotionCancel(); |
| 5006 | } else { |
| 5007 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 5008 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5009 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame^] | 5010 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 5011 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5012 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5013 | mUnfocusedWindow->assertNoEvents(); |
| 5014 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5015 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5016 | } |
| 5017 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5018 | /** |
| 5019 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 5020 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 5021 | * |
| 5022 | * If the user touches another application during this time, the key should be dropped. |
| 5023 | * Next, if a new focused window comes in, without toggling the focused application, |
| 5024 | * then no ANR should occur. |
| 5025 | * |
| 5026 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 5027 | * but in some cases the policy may not update the focused application. |
| 5028 | */ |
| 5029 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 5030 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 5031 | std::make_shared<FakeApplicationHandle>(); |
| 5032 | focusedApplication->setDispatchingTimeout(60ms); |
| 5033 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 5034 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 5035 | mFocusedWindow->setFocusable(false); |
| 5036 | |
| 5037 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 5038 | mFocusedWindow->consumeFocusEvent(false); |
| 5039 | |
| 5040 | // Send a key. The ANR timer should start because there is no focused window. |
| 5041 | // 'focusedApplication' will get blamed if this timer completes. |
| 5042 | // Key will not be sent anywhere because we have no focused window. It will remain pending. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5043 | InputEventInjectionResult result = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5044 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5045 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, |
| 5046 | false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5047 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5048 | |
| 5049 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 5050 | // then the injected touches won't cause the focused event to get dropped. |
| 5051 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 5052 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 5053 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 5054 | // For this test, it means that the key would get delivered to the window once it becomes |
| 5055 | // focused. |
| 5056 | std::this_thread::sleep_for(10ms); |
| 5057 | |
| 5058 | // Touch unfocused window. This should force the pending key to get dropped. |
| 5059 | NotifyMotionArgs motionArgs = |
| 5060 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5061 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); |
| 5062 | mDispatcher->notifyMotion(&motionArgs); |
| 5063 | |
| 5064 | // We do not consume the motion right away, because that would require dispatcher to first |
| 5065 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 5066 | // Set the focused window first. |
| 5067 | mFocusedWindow->setFocusable(true); |
| 5068 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 5069 | setFocusedWindow(mFocusedWindow); |
| 5070 | mFocusedWindow->consumeFocusEvent(true); |
| 5071 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 5072 | // to another application. This could be a bug / behaviour in the policy. |
| 5073 | |
| 5074 | mUnfocusedWindow->consumeMotionDown(); |
| 5075 | |
| 5076 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5077 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 5078 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 5079 | } |
| 5080 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5081 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 5082 | // that has feature NO_INPUT_CHANNEL. |
| 5083 | // Layout: |
| 5084 | // Top (closest to user) |
| 5085 | // mNoInputWindow (above all windows) |
| 5086 | // mBottomWindow |
| 5087 | // Bottom (furthest from user) |
| 5088 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 5089 | virtual void SetUp() override { |
| 5090 | InputDispatcherTest::SetUp(); |
| 5091 | |
| 5092 | mApplication = std::make_shared<FakeApplicationHandle>(); |
| 5093 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, |
| 5094 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
| 5095 | std::make_optional<sp<IBinder>>(nullptr) /*token*/); |
| 5096 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5097 | mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5098 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5099 | // It's perfectly valid for this window to not have an associated input channel |
| 5100 | |
| 5101 | mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window", |
| 5102 | ADISPLAY_ID_DEFAULT); |
| 5103 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5104 | |
| 5105 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 5106 | } |
| 5107 | |
| 5108 | protected: |
| 5109 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 5110 | sp<FakeWindowHandle> mNoInputWindow; |
| 5111 | sp<FakeWindowHandle> mBottomWindow; |
| 5112 | }; |
| 5113 | |
| 5114 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 5115 | PointF touchedPoint = {10, 10}; |
| 5116 | |
| 5117 | NotifyMotionArgs motionArgs = |
| 5118 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5119 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 5120 | mDispatcher->notifyMotion(&motionArgs); |
| 5121 | |
| 5122 | mNoInputWindow->assertNoEvents(); |
| 5123 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 5124 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 5125 | // and therefore should prevent mBottomWindow from receiving touches |
| 5126 | mBottomWindow->assertNoEvents(); |
| 5127 | } |
| 5128 | |
| 5129 | /** |
| 5130 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 5131 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 5132 | */ |
| 5133 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 5134 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
| 5135 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, |
| 5136 | "Window with input channel and NO_INPUT_CHANNEL", |
| 5137 | ADISPLAY_ID_DEFAULT); |
| 5138 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5139 | mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5140 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5141 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 5142 | |
| 5143 | PointF touchedPoint = {10, 10}; |
| 5144 | |
| 5145 | NotifyMotionArgs motionArgs = |
| 5146 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5147 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 5148 | mDispatcher->notifyMotion(&motionArgs); |
| 5149 | |
| 5150 | mNoInputWindow->assertNoEvents(); |
| 5151 | mBottomWindow->assertNoEvents(); |
| 5152 | } |
| 5153 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5154 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 5155 | protected: |
| 5156 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 5157 | sp<FakeWindowHandle> mWindow; |
| 5158 | sp<FakeWindowHandle> mMirror; |
| 5159 | |
| 5160 | virtual void SetUp() override { |
| 5161 | InputDispatcherTest::SetUp(); |
| 5162 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 5163 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 5164 | mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT, |
| 5165 | mWindow->getToken()); |
| 5166 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 5167 | mWindow->setFocusable(true); |
| 5168 | mMirror->setFocusable(true); |
| 5169 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5170 | } |
| 5171 | }; |
| 5172 | |
| 5173 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 5174 | // Request focus on a mirrored window |
| 5175 | setFocusedWindow(mMirror); |
| 5176 | |
| 5177 | // window gets focused |
| 5178 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5179 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5180 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5181 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5182 | } |
| 5183 | |
| 5184 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 5185 | // focusable. |
| 5186 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 5187 | setFocusedWindow(mMirror); |
| 5188 | |
| 5189 | // window gets focused |
| 5190 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5191 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5192 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5193 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5194 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5195 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5196 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5197 | |
| 5198 | mMirror->setFocusable(false); |
| 5199 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5200 | |
| 5201 | // window loses focus since one of the windows associated with the token in not focusable |
| 5202 | mWindow->consumeFocusEvent(false); |
| 5203 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5204 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5205 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5206 | mWindow->assertNoEvents(); |
| 5207 | } |
| 5208 | |
| 5209 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 5210 | // invisible. |
| 5211 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 5212 | setFocusedWindow(mMirror); |
| 5213 | |
| 5214 | // window gets focused |
| 5215 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5216 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5217 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5218 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5219 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5220 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5221 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5222 | |
| 5223 | mMirror->setVisible(false); |
| 5224 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5225 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5226 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5227 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5228 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5229 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5230 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5231 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5232 | |
| 5233 | mWindow->setVisible(false); |
| 5234 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5235 | |
| 5236 | // window loses focus only after all windows associated with the token become invisible. |
| 5237 | mWindow->consumeFocusEvent(false); |
| 5238 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5239 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5240 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5241 | mWindow->assertNoEvents(); |
| 5242 | } |
| 5243 | |
| 5244 | // A focused & mirrored window remains focused until both windows are removed. |
| 5245 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 5246 | setFocusedWindow(mMirror); |
| 5247 | |
| 5248 | // window gets focused |
| 5249 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5250 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5251 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5252 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5253 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5254 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5255 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5256 | |
| 5257 | // single window is removed but the window token remains focused |
| 5258 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 5259 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5260 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5261 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5262 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5263 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5264 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5265 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5266 | |
| 5267 | // Both windows are removed |
| 5268 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 5269 | mWindow->consumeFocusEvent(false); |
| 5270 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5271 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5272 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5273 | mWindow->assertNoEvents(); |
| 5274 | } |
| 5275 | |
| 5276 | // Focus request can be pending until one window becomes visible. |
| 5277 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 5278 | // Request focus on an invisible mirror. |
| 5279 | mWindow->setVisible(false); |
| 5280 | mMirror->setVisible(false); |
| 5281 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5282 | setFocusedWindow(mMirror); |
| 5283 | |
| 5284 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5285 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5286 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5287 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5288 | |
| 5289 | mMirror->setVisible(true); |
| 5290 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5291 | |
| 5292 | // window gets focused |
| 5293 | mWindow->consumeFocusEvent(true); |
| 5294 | // window gets the pending key event |
| 5295 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5296 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5297 | |
| 5298 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 5299 | protected: |
| 5300 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 5301 | sp<FakeWindowHandle> mWindow; |
| 5302 | sp<FakeWindowHandle> mSecondWindow; |
| 5303 | |
| 5304 | void SetUp() override { |
| 5305 | InputDispatcherTest::SetUp(); |
| 5306 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 5307 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 5308 | mWindow->setFocusable(true); |
| 5309 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
| 5310 | mSecondWindow->setFocusable(true); |
| 5311 | |
| 5312 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 5313 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 5314 | |
| 5315 | setFocusedWindow(mWindow); |
| 5316 | mWindow->consumeFocusEvent(true); |
| 5317 | } |
| 5318 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5319 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
| 5320 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5321 | mDispatcher->notifyPointerCaptureChanged(&args); |
| 5322 | } |
| 5323 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5324 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 5325 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5326 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5327 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 5328 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5329 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5330 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5331 | } |
| 5332 | }; |
| 5333 | |
| 5334 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 5335 | // Ensure that capture cannot be obtained for unfocused windows. |
| 5336 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 5337 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 5338 | mSecondWindow->assertNoEvents(); |
| 5339 | |
| 5340 | // Ensure that capture can be enabled from the focus window. |
| 5341 | requestAndVerifyPointerCapture(mWindow, true); |
| 5342 | |
| 5343 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 5344 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 5345 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 5346 | |
| 5347 | // Ensure that capture can be disabled from the window with capture. |
| 5348 | requestAndVerifyPointerCapture(mWindow, false); |
| 5349 | } |
| 5350 | |
| 5351 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5352 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5353 | |
| 5354 | setFocusedWindow(mSecondWindow); |
| 5355 | |
| 5356 | // Ensure that the capture disabled event was sent first. |
| 5357 | mWindow->consumeCaptureEvent(false); |
| 5358 | mWindow->consumeFocusEvent(false); |
| 5359 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5360 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5361 | |
| 5362 | // Ensure that additional state changes from InputReader are not sent to the window. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5363 | notifyPointerCaptureChanged({}); |
| 5364 | notifyPointerCaptureChanged(request); |
| 5365 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5366 | mWindow->assertNoEvents(); |
| 5367 | mSecondWindow->assertNoEvents(); |
| 5368 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 5369 | } |
| 5370 | |
| 5371 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5372 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5373 | |
| 5374 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5375 | notifyPointerCaptureChanged({}); |
| 5376 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5377 | |
| 5378 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5379 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5380 | mWindow->consumeCaptureEvent(false); |
| 5381 | mWindow->assertNoEvents(); |
| 5382 | } |
| 5383 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 5384 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 5385 | requestAndVerifyPointerCapture(mWindow, true); |
| 5386 | |
| 5387 | // The first window loses focus. |
| 5388 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5389 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 5390 | mWindow->consumeCaptureEvent(false); |
| 5391 | |
| 5392 | // Request Pointer Capture from the second window before the notification from InputReader |
| 5393 | // arrives. |
| 5394 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5395 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 5396 | |
| 5397 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5398 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 5399 | |
| 5400 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5401 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 5402 | |
| 5403 | mSecondWindow->consumeFocusEvent(true); |
| 5404 | mSecondWindow->consumeCaptureEvent(true); |
| 5405 | } |
| 5406 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5407 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 5408 | // App repeatedly enables and disables capture. |
| 5409 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 5410 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 5411 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 5412 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 5413 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 5414 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 5415 | |
| 5416 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 5417 | // first request is now stale, this should do nothing. |
| 5418 | notifyPointerCaptureChanged(firstRequest); |
| 5419 | mWindow->assertNoEvents(); |
| 5420 | |
| 5421 | // InputReader notifies that the second request was enabled. |
| 5422 | notifyPointerCaptureChanged(secondRequest); |
| 5423 | mWindow->consumeCaptureEvent(true); |
| 5424 | } |
| 5425 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5426 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 5427 | protected: |
| 5428 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5429 | |
| 5430 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 5431 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 5432 | |
| 5433 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 5434 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 5435 | |
| 5436 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 5437 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 5438 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 5439 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 5440 | MAXIMUM_OBSCURING_OPACITY); |
| 5441 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5442 | static const int32_t TOUCHED_APP_UID = 10001; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5443 | static const int32_t APP_B_UID = 10002; |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5444 | static const int32_t APP_C_UID = 10003; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5445 | |
| 5446 | sp<FakeWindowHandle> mTouchWindow; |
| 5447 | |
| 5448 | virtual void SetUp() override { |
| 5449 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5450 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5451 | mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK); |
| 5452 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 5453 | } |
| 5454 | |
| 5455 | virtual void TearDown() override { |
| 5456 | InputDispatcherTest::TearDown(); |
| 5457 | mTouchWindow.clear(); |
| 5458 | } |
| 5459 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5460 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, |
| 5461 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5462 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5463 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5464 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5465 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5466 | return window; |
| 5467 | } |
| 5468 | |
| 5469 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { |
| 5470 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 5471 | sp<FakeWindowHandle> window = |
| 5472 | new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
| 5473 | // Generate an arbitrary PID based on the UID |
| 5474 | window->setOwnerInfo(1777 + (uid % 10000), uid); |
| 5475 | return window; |
| 5476 | } |
| 5477 | |
| 5478 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
| 5479 | NotifyMotionArgs args = |
| 5480 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5481 | ADISPLAY_ID_DEFAULT, points); |
| 5482 | mDispatcher->notifyMotion(&args); |
| 5483 | } |
| 5484 | }; |
| 5485 | |
| 5486 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5487 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5488 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5489 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5490 | |
| 5491 | touch(); |
| 5492 | |
| 5493 | mTouchWindow->assertNoEvents(); |
| 5494 | } |
| 5495 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5496 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5497 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 5498 | const sp<FakeWindowHandle>& w = |
| 5499 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 5500 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5501 | |
| 5502 | touch(); |
| 5503 | |
| 5504 | mTouchWindow->assertNoEvents(); |
| 5505 | } |
| 5506 | |
| 5507 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5508 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 5509 | const sp<FakeWindowHandle>& w = |
| 5510 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 5511 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5512 | |
| 5513 | touch(); |
| 5514 | |
| 5515 | w->assertNoEvents(); |
| 5516 | } |
| 5517 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5518 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5519 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 5520 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5521 | |
| 5522 | touch(); |
| 5523 | |
| 5524 | mTouchWindow->consumeAnyMotionDown(); |
| 5525 | } |
| 5526 | |
| 5527 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5528 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5529 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5530 | w->setFrame(Rect(0, 0, 50, 50)); |
| 5531 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5532 | |
| 5533 | touch({PointF{100, 100}}); |
| 5534 | |
| 5535 | mTouchWindow->consumeAnyMotionDown(); |
| 5536 | } |
| 5537 | |
| 5538 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5539 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5540 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5541 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5542 | |
| 5543 | touch(); |
| 5544 | |
| 5545 | mTouchWindow->consumeAnyMotionDown(); |
| 5546 | } |
| 5547 | |
| 5548 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 5549 | const sp<FakeWindowHandle>& w = |
| 5550 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 5551 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5552 | |
| 5553 | touch(); |
| 5554 | |
| 5555 | mTouchWindow->consumeAnyMotionDown(); |
| 5556 | } |
| 5557 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5558 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 5559 | const sp<FakeWindowHandle>& w = |
| 5560 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 5561 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5562 | |
| 5563 | touch(); |
| 5564 | |
| 5565 | w->assertNoEvents(); |
| 5566 | } |
| 5567 | |
| 5568 | /** |
| 5569 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 5570 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 5571 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 5572 | */ |
| 5573 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5574 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 5575 | const sp<FakeWindowHandle>& w = |
| 5576 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5577 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5578 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5579 | |
| 5580 | touch(); |
| 5581 | |
| 5582 | w->consumeMotionOutside(); |
| 5583 | } |
| 5584 | |
| 5585 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 5586 | const sp<FakeWindowHandle>& w = |
| 5587 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5588 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5589 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5590 | |
| 5591 | touch(); |
| 5592 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5593 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5594 | } |
| 5595 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5596 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5597 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5598 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5599 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5600 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5601 | |
| 5602 | touch(); |
| 5603 | |
| 5604 | mTouchWindow->consumeAnyMotionDown(); |
| 5605 | } |
| 5606 | |
| 5607 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 5608 | const sp<FakeWindowHandle>& w = |
| 5609 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5610 | MAXIMUM_OBSCURING_OPACITY); |
| 5611 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5612 | |
| 5613 | touch(); |
| 5614 | |
| 5615 | mTouchWindow->consumeAnyMotionDown(); |
| 5616 | } |
| 5617 | |
| 5618 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5619 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5620 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5621 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5622 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5623 | |
| 5624 | touch(); |
| 5625 | |
| 5626 | mTouchWindow->assertNoEvents(); |
| 5627 | } |
| 5628 | |
| 5629 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 5630 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 5631 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5632 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 5633 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5634 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5635 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 5636 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5637 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 5638 | |
| 5639 | touch(); |
| 5640 | |
| 5641 | mTouchWindow->assertNoEvents(); |
| 5642 | } |
| 5643 | |
| 5644 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 5645 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 5646 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5647 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 5648 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5649 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5650 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 5651 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5652 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 5653 | |
| 5654 | touch(); |
| 5655 | |
| 5656 | mTouchWindow->consumeAnyMotionDown(); |
| 5657 | } |
| 5658 | |
| 5659 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5660 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 5661 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5662 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5663 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5664 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5665 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 5666 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5667 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 5668 | |
| 5669 | touch(); |
| 5670 | |
| 5671 | mTouchWindow->consumeAnyMotionDown(); |
| 5672 | } |
| 5673 | |
| 5674 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 5675 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5676 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5677 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5678 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5679 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 5680 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5681 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5682 | |
| 5683 | touch(); |
| 5684 | |
| 5685 | mTouchWindow->assertNoEvents(); |
| 5686 | } |
| 5687 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5688 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5689 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 5690 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5691 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 5692 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5693 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5694 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5695 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5696 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 5697 | |
| 5698 | touch(); |
| 5699 | |
| 5700 | mTouchWindow->assertNoEvents(); |
| 5701 | } |
| 5702 | |
| 5703 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5704 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 5705 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5706 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 5707 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5708 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5709 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5710 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5711 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 5712 | |
| 5713 | touch(); |
| 5714 | |
| 5715 | mTouchWindow->consumeAnyMotionDown(); |
| 5716 | } |
| 5717 | |
| 5718 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 5719 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5720 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 5721 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5722 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5723 | |
| 5724 | touch(); |
| 5725 | |
| 5726 | mTouchWindow->consumeAnyMotionDown(); |
| 5727 | } |
| 5728 | |
| 5729 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 5730 | const sp<FakeWindowHandle>& w = |
| 5731 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 5732 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5733 | |
| 5734 | touch(); |
| 5735 | |
| 5736 | mTouchWindow->consumeAnyMotionDown(); |
| 5737 | } |
| 5738 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 5739 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5740 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 5741 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 5742 | const sp<FakeWindowHandle>& w = |
| 5743 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 5744 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5745 | |
| 5746 | touch(); |
| 5747 | |
| 5748 | mTouchWindow->assertNoEvents(); |
| 5749 | } |
| 5750 | |
| 5751 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 5752 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 5753 | const sp<FakeWindowHandle>& w = |
| 5754 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 5755 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5756 | |
| 5757 | touch(); |
| 5758 | |
| 5759 | mTouchWindow->consumeAnyMotionDown(); |
| 5760 | } |
| 5761 | |
| 5762 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5763 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 5764 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 5765 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5766 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5767 | OPACITY_ABOVE_THRESHOLD); |
| 5768 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5769 | |
| 5770 | touch(); |
| 5771 | |
| 5772 | mTouchWindow->consumeAnyMotionDown(); |
| 5773 | } |
| 5774 | |
| 5775 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5776 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 5777 | const sp<FakeWindowHandle>& w1 = |
| 5778 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 5779 | OPACITY_BELOW_THRESHOLD); |
| 5780 | const sp<FakeWindowHandle>& w2 = |
| 5781 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5782 | OPACITY_BELOW_THRESHOLD); |
| 5783 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 5784 | |
| 5785 | touch(); |
| 5786 | |
| 5787 | mTouchWindow->assertNoEvents(); |
| 5788 | } |
| 5789 | |
| 5790 | /** |
| 5791 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 5792 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 5793 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 5794 | */ |
| 5795 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5796 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 5797 | const sp<FakeWindowHandle>& wB = |
| 5798 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 5799 | OPACITY_BELOW_THRESHOLD); |
| 5800 | const sp<FakeWindowHandle>& wC = |
| 5801 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 5802 | OPACITY_BELOW_THRESHOLD); |
| 5803 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 5804 | |
| 5805 | touch(); |
| 5806 | |
| 5807 | mTouchWindow->assertNoEvents(); |
| 5808 | } |
| 5809 | |
| 5810 | /** |
| 5811 | * This test is testing that a window from a different UID but with same application token doesn't |
| 5812 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 5813 | */ |
| 5814 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5815 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 5816 | const sp<FakeWindowHandle>& w = |
| 5817 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 5818 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 5819 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5820 | |
| 5821 | touch(); |
| 5822 | |
| 5823 | mTouchWindow->consumeAnyMotionDown(); |
| 5824 | } |
| 5825 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5826 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 5827 | protected: |
| 5828 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 5829 | sp<FakeWindowHandle> mWindow; |
| 5830 | sp<FakeWindowHandle> mSecondWindow; |
| 5831 | sp<FakeWindowHandle> mDragWindow; |
| 5832 | |
| 5833 | void SetUp() override { |
| 5834 | InputDispatcherTest::SetUp(); |
| 5835 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 5836 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 5837 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5838 | |
| 5839 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
| 5840 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5841 | |
| 5842 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 5843 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 5844 | } |
| 5845 | |
| 5846 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 5847 | void performDrag() { |
| 5848 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5849 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5850 | {50, 50})) |
| 5851 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5852 | |
| 5853 | // Window should receive motion event. |
| 5854 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5855 | |
| 5856 | // The drag window covers the entire display |
| 5857 | mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
| 5858 | mDispatcher->setInputWindows( |
| 5859 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 5860 | |
| 5861 | // Transfer touch focus to the drag window |
| 5862 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
| 5863 | true /* isDragDrop */); |
| 5864 | mWindow->consumeMotionCancel(); |
| 5865 | mDragWindow->consumeMotionDown(); |
| 5866 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5867 | |
| 5868 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 5869 | void performStylusDrag() { |
| 5870 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5871 | injectMotionEvent(mDispatcher, |
| 5872 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 5873 | AINPUT_SOURCE_STYLUS) |
| 5874 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 5875 | .pointer(PointerBuilder(0, |
| 5876 | AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 5877 | .x(50) |
| 5878 | .y(50)) |
| 5879 | .build())); |
| 5880 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5881 | |
| 5882 | // The drag window covers the entire display |
| 5883 | mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
| 5884 | mDispatcher->setInputWindows( |
| 5885 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 5886 | |
| 5887 | // Transfer touch focus to the drag window |
| 5888 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
| 5889 | true /* isDragDrop */); |
| 5890 | mWindow->consumeMotionCancel(); |
| 5891 | mDragWindow->consumeMotionDown(); |
| 5892 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5893 | }; |
| 5894 | |
| 5895 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
| 5896 | performDrag(); |
| 5897 | |
| 5898 | // Move on window. |
| 5899 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5900 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5901 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 5902 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5903 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5904 | mWindow->consumeDragEvent(false, 50, 50); |
| 5905 | mSecondWindow->assertNoEvents(); |
| 5906 | |
| 5907 | // Move to another window. |
| 5908 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5909 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5910 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 5911 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5912 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5913 | mWindow->consumeDragEvent(true, 150, 50); |
| 5914 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 5915 | |
| 5916 | // Move back to original window. |
| 5917 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5918 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5919 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 5920 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5921 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5922 | mWindow->consumeDragEvent(false, 50, 50); |
| 5923 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 5924 | |
| 5925 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5926 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 5927 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5928 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 5929 | mWindow->assertNoEvents(); |
| 5930 | mSecondWindow->assertNoEvents(); |
| 5931 | } |
| 5932 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5933 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
| 5934 | performDrag(); |
| 5935 | |
| 5936 | // Move on window. |
| 5937 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5938 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5939 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 5940 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5941 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5942 | mWindow->consumeDragEvent(false, 50, 50); |
| 5943 | mSecondWindow->assertNoEvents(); |
| 5944 | |
| 5945 | // Move to another window. |
| 5946 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5947 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5948 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 5949 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5950 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5951 | mWindow->consumeDragEvent(true, 150, 50); |
| 5952 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 5953 | |
| 5954 | // drop to another window. |
| 5955 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5956 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5957 | {150, 50})) |
| 5958 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5959 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 5960 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 5961 | mWindow->assertNoEvents(); |
| 5962 | mSecondWindow->assertNoEvents(); |
| 5963 | } |
| 5964 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5965 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
| 5966 | performStylusDrag(); |
| 5967 | |
| 5968 | // Move on window and keep button pressed. |
| 5969 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5970 | injectMotionEvent(mDispatcher, |
| 5971 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 5972 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 5973 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 5974 | .x(50) |
| 5975 | .y(50)) |
| 5976 | .build())) |
| 5977 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5978 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5979 | mWindow->consumeDragEvent(false, 50, 50); |
| 5980 | mSecondWindow->assertNoEvents(); |
| 5981 | |
| 5982 | // Move to another window and release button, expect to drop item. |
| 5983 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5984 | injectMotionEvent(mDispatcher, |
| 5985 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 5986 | .buttonState(0) |
| 5987 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 5988 | .x(150) |
| 5989 | .y(50)) |
| 5990 | .build())) |
| 5991 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5992 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5993 | mWindow->assertNoEvents(); |
| 5994 | mSecondWindow->assertNoEvents(); |
| 5995 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 5996 | |
| 5997 | // nothing to the window. |
| 5998 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5999 | injectMotionEvent(mDispatcher, |
| 6000 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 6001 | .buttonState(0) |
| 6002 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6003 | .x(150) |
| 6004 | .y(50)) |
| 6005 | .build())) |
| 6006 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6007 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6008 | mWindow->assertNoEvents(); |
| 6009 | mSecondWindow->assertNoEvents(); |
| 6010 | } |
| 6011 | |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 6012 | TEST_F(InputDispatcherDragTests, DragAndDrop_InvalidWindow) { |
| 6013 | performDrag(); |
| 6014 | |
| 6015 | // Set second window invisible. |
| 6016 | mSecondWindow->setVisible(false); |
| 6017 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 6018 | |
| 6019 | // Move on window. |
| 6020 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6021 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6022 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6023 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6024 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6025 | mWindow->consumeDragEvent(false, 50, 50); |
| 6026 | mSecondWindow->assertNoEvents(); |
| 6027 | |
| 6028 | // Move to another window. |
| 6029 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6030 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6031 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 6032 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6033 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6034 | mWindow->consumeDragEvent(true, 150, 50); |
| 6035 | mSecondWindow->assertNoEvents(); |
| 6036 | |
| 6037 | // drop to another window. |
| 6038 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6039 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6040 | {150, 50})) |
| 6041 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6042 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6043 | mFakePolicy->assertDropTargetEquals(nullptr); |
| 6044 | mWindow->assertNoEvents(); |
| 6045 | mSecondWindow->assertNoEvents(); |
| 6046 | } |
| 6047 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6048 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 6049 | |
| 6050 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 6051 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6052 | sp<FakeWindowHandle> window = |
| 6053 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 6054 | window->setInputFeatures(WindowInfo::Feature::DROP_INPUT); |
| 6055 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6056 | window->setFocusable(true); |
| 6057 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 6058 | setFocusedWindow(window); |
| 6059 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 6060 | |
| 6061 | // With the flag set, window should not get any input |
| 6062 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 6063 | mDispatcher->notifyKey(&keyArgs); |
| 6064 | window->assertNoEvents(); |
| 6065 | |
| 6066 | NotifyMotionArgs motionArgs = |
| 6067 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6068 | ADISPLAY_ID_DEFAULT); |
| 6069 | mDispatcher->notifyMotion(&motionArgs); |
| 6070 | window->assertNoEvents(); |
| 6071 | |
| 6072 | // With the flag cleared, the window should get input |
| 6073 | window->setInputFeatures({}); |
| 6074 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 6075 | |
| 6076 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 6077 | mDispatcher->notifyKey(&keyArgs); |
| 6078 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 6079 | |
| 6080 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6081 | ADISPLAY_ID_DEFAULT); |
| 6082 | mDispatcher->notifyMotion(&motionArgs); |
| 6083 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6084 | window->assertNoEvents(); |
| 6085 | } |
| 6086 | |
| 6087 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 6088 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 6089 | std::make_shared<FakeApplicationHandle>(); |
| 6090 | sp<FakeWindowHandle> obscuringWindow = |
| 6091 | new FakeWindowHandle(obscuringApplication, mDispatcher, "obscuringWindow", |
| 6092 | ADISPLAY_ID_DEFAULT); |
| 6093 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 6094 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6095 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6096 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6097 | sp<FakeWindowHandle> window = |
| 6098 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 6099 | window->setInputFeatures(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED); |
| 6100 | window->setOwnerInfo(222, 222); |
| 6101 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6102 | window->setFocusable(true); |
| 6103 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 6104 | setFocusedWindow(window); |
| 6105 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 6106 | |
| 6107 | // With the flag set, window should not get any input |
| 6108 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 6109 | mDispatcher->notifyKey(&keyArgs); |
| 6110 | window->assertNoEvents(); |
| 6111 | |
| 6112 | NotifyMotionArgs motionArgs = |
| 6113 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6114 | ADISPLAY_ID_DEFAULT); |
| 6115 | mDispatcher->notifyMotion(&motionArgs); |
| 6116 | window->assertNoEvents(); |
| 6117 | |
| 6118 | // With the flag cleared, the window should get input |
| 6119 | window->setInputFeatures({}); |
| 6120 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 6121 | |
| 6122 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 6123 | mDispatcher->notifyKey(&keyArgs); |
| 6124 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 6125 | |
| 6126 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6127 | ADISPLAY_ID_DEFAULT); |
| 6128 | mDispatcher->notifyMotion(&motionArgs); |
| 6129 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 6130 | window->assertNoEvents(); |
| 6131 | } |
| 6132 | |
| 6133 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 6134 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 6135 | std::make_shared<FakeApplicationHandle>(); |
| 6136 | sp<FakeWindowHandle> obscuringWindow = |
| 6137 | new FakeWindowHandle(obscuringApplication, mDispatcher, "obscuringWindow", |
| 6138 | ADISPLAY_ID_DEFAULT); |
| 6139 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 6140 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6141 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6142 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6143 | sp<FakeWindowHandle> window = |
| 6144 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 6145 | window->setInputFeatures(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED); |
| 6146 | window->setOwnerInfo(222, 222); |
| 6147 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6148 | window->setFocusable(true); |
| 6149 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 6150 | setFocusedWindow(window); |
| 6151 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 6152 | |
| 6153 | // With the flag set, window should not get any input |
| 6154 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 6155 | mDispatcher->notifyKey(&keyArgs); |
| 6156 | window->assertNoEvents(); |
| 6157 | |
| 6158 | NotifyMotionArgs motionArgs = |
| 6159 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6160 | ADISPLAY_ID_DEFAULT); |
| 6161 | mDispatcher->notifyMotion(&motionArgs); |
| 6162 | window->assertNoEvents(); |
| 6163 | |
| 6164 | // When the window is no longer obscured because it went on top, it should get input |
| 6165 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 6166 | |
| 6167 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 6168 | mDispatcher->notifyKey(&keyArgs); |
| 6169 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 6170 | |
| 6171 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6172 | ADISPLAY_ID_DEFAULT); |
| 6173 | mDispatcher->notifyMotion(&motionArgs); |
| 6174 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6175 | window->assertNoEvents(); |
| 6176 | } |
| 6177 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6178 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 6179 | protected: |
| 6180 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6181 | sp<FakeWindowHandle> mWindow; |
| 6182 | sp<FakeWindowHandle> mSecondWindow; |
| 6183 | |
| 6184 | void SetUp() override { |
| 6185 | InputDispatcherTest::SetUp(); |
| 6186 | |
| 6187 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 6188 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 6189 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 6190 | setFocusedWindow(mWindow); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6191 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
| 6192 | mSecondWindow->setFocusable(true); |
| 6193 | |
| 6194 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 6195 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6196 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 6197 | |
| 6198 | // Set initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 6199 | mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, INJECTOR_PID, |
| 6200 | INJECTOR_UID, /* hasPermission */ true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6201 | } |
| 6202 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6203 | void changeAndVerifyTouchMode(bool inTouchMode, int32_t pid, int32_t uid, bool hasPermission) { |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 6204 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6205 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 6206 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
| 6207 | } |
| 6208 | }; |
| 6209 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 6210 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6211 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 6212 | changeAndVerifyTouchMode(!InputDispatcher::kDefaultInTouchMode, windowInfo.ownerPid, |
| 6213 | windowInfo.ownerUid, /* hasPermission */ false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6214 | } |
| 6215 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 6216 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 6217 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 6218 | int32_t ownerPid = windowInfo.ownerPid; |
| 6219 | int32_t ownerUid = windowInfo.ownerUid; |
| 6220 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
| 6221 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
| 6222 | ownerUid, /* hasPermission */ false)); |
| 6223 | mWindow->assertNoEvents(); |
| 6224 | mSecondWindow->assertNoEvents(); |
| 6225 | } |
| 6226 | |
| 6227 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 6228 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 6229 | int32_t ownerPid = windowInfo.ownerPid; |
| 6230 | int32_t ownerUid = windowInfo.ownerUid; |
| 6231 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
| 6232 | changeAndVerifyTouchMode(!InputDispatcher::kDefaultInTouchMode, ownerPid, ownerUid, |
| 6233 | /* hasPermission */ true); |
| 6234 | } |
| 6235 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6236 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6237 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 6238 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 6239 | windowInfo.ownerPid, windowInfo.ownerUid, |
| 6240 | /* hasPermission */ true)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6241 | mWindow->assertNoEvents(); |
| 6242 | mSecondWindow->assertNoEvents(); |
| 6243 | } |
| 6244 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6245 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 6246 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6247 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6248 | std::shared_ptr<FakeApplicationHandle> application = |
| 6249 | std::make_shared<FakeApplicationHandle>(); |
| 6250 | std::string name = "Fake Spy "; |
| 6251 | name += std::to_string(mSpyCount++); |
| 6252 | sp<FakeWindowHandle> spy = |
| 6253 | new FakeWindowHandle(application, mDispatcher, name.c_str(), ADISPLAY_ID_DEFAULT); |
| 6254 | spy->setInputFeatures(WindowInfo::Feature::SPY); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 6255 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6256 | return spy; |
| 6257 | } |
| 6258 | |
| 6259 | sp<FakeWindowHandle> createForeground() { |
| 6260 | std::shared_ptr<FakeApplicationHandle> application = |
| 6261 | std::make_shared<FakeApplicationHandle>(); |
| 6262 | sp<FakeWindowHandle> window = |
| 6263 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6264 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6265 | return window; |
| 6266 | } |
| 6267 | |
| 6268 | private: |
| 6269 | int mSpyCount{0}; |
| 6270 | }; |
| 6271 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 6272 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6273 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 6274 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 6275 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 6276 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
| 6277 | ScopedSilentDeath _silentDeath; |
| 6278 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6279 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 6280 | spy->setTrustedOverlay(false); |
| 6281 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), |
| 6282 | ".* not a trusted overlay"); |
| 6283 | } |
| 6284 | |
| 6285 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6286 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 6287 | */ |
| 6288 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6289 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6290 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); |
| 6291 | |
| 6292 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6293 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6294 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6295 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6296 | } |
| 6297 | |
| 6298 | /** |
| 6299 | * Verify the order in which different input windows receive events. The touched foreground window |
| 6300 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 6301 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 6302 | * receive events before ones belows it. |
| 6303 | * |
| 6304 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 6305 | * spy1, spy2, window, spy3. |
| 6306 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 6307 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 6308 | * window. |
| 6309 | */ |
| 6310 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 6311 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6312 | auto spy1 = createSpy(); |
| 6313 | auto spy2 = createSpy(); |
| 6314 | auto spy3 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6315 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); |
| 6316 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 6317 | const size_t numChannels = channels.size(); |
| 6318 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 6319 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6320 | if (!epollFd.ok()) { |
| 6321 | FAIL() << "Failed to create epoll fd"; |
| 6322 | } |
| 6323 | |
| 6324 | for (size_t i = 0; i < numChannels; i++) { |
| 6325 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 6326 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 6327 | FAIL() << "Failed to add fd to epoll"; |
| 6328 | } |
| 6329 | } |
| 6330 | |
| 6331 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6332 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6333 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6334 | |
| 6335 | std::vector<size_t> eventOrder; |
| 6336 | std::vector<struct epoll_event> events(numChannels); |
| 6337 | for (;;) { |
| 6338 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 6339 | (100ms).count()); |
| 6340 | if (nFds < 0) { |
| 6341 | FAIL() << "Failed to call epoll_wait"; |
| 6342 | } |
| 6343 | if (nFds == 0) { |
| 6344 | break; // epoll_wait timed out |
| 6345 | } |
| 6346 | for (int i = 0; i < nFds; i++) { |
| 6347 | ASSERT_EQ(EPOLLIN, events[i].events); |
| 6348 | eventOrder.push_back(events[i].data.u64); |
| 6349 | channels[i]->consumeMotionDown(); |
| 6350 | } |
| 6351 | } |
| 6352 | |
| 6353 | // Verify the order in which the events were received. |
| 6354 | EXPECT_EQ(3u, eventOrder.size()); |
| 6355 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 6356 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 6357 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 6358 | } |
| 6359 | |
| 6360 | /** |
| 6361 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 6362 | */ |
| 6363 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 6364 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6365 | auto spy = createSpy(); |
| 6366 | spy->setTouchable(false); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6367 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 6368 | |
| 6369 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6370 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6371 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6372 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6373 | spy->assertNoEvents(); |
| 6374 | } |
| 6375 | |
| 6376 | /** |
| 6377 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 6378 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 6379 | * to the window. |
| 6380 | */ |
| 6381 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 6382 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6383 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6384 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
| 6385 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 6386 | |
| 6387 | // Inject an event outside the spy window's touchable region. |
| 6388 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6389 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6390 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6391 | window->consumeMotionDown(); |
| 6392 | spy->assertNoEvents(); |
| 6393 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6394 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6395 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6396 | window->consumeMotionUp(); |
| 6397 | spy->assertNoEvents(); |
| 6398 | |
| 6399 | // Inject an event inside the spy window's touchable region. |
| 6400 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6401 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6402 | {5, 10})) |
| 6403 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6404 | window->consumeMotionDown(); |
| 6405 | spy->consumeMotionDown(); |
| 6406 | } |
| 6407 | |
| 6408 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6409 | * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6410 | * flag, but it will get zero-ed out coordinates if the foreground has a different owner. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6411 | */ |
| 6412 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 6413 | auto window = createForeground(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6414 | window->setOwnerInfo(12, 34); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6415 | auto spy = createSpy(); |
| 6416 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6417 | spy->setOwnerInfo(56, 78); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6418 | spy->setFrame(Rect{0, 0, 20, 20}); |
| 6419 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 6420 | |
| 6421 | // Inject an event outside the spy window's frame and touchable region. |
| 6422 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6423 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6424 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6425 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6426 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6427 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6428 | } |
| 6429 | |
| 6430 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6431 | * A spy window can pilfer pointers. When this happens, touch gestures that are currently sent to |
| 6432 | * any other windows - including other spy windows - will also be cancelled. |
| 6433 | */ |
| 6434 | TEST_F(InputDispatcherSpyWindowTest, PilferPointers) { |
| 6435 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6436 | auto spy1 = createSpy(); |
| 6437 | auto spy2 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6438 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); |
| 6439 | |
| 6440 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6441 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6442 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6443 | window->consumeMotionDown(); |
| 6444 | spy1->consumeMotionDown(); |
| 6445 | spy2->consumeMotionDown(); |
| 6446 | |
| 6447 | // Pilfer pointers from the second spy window. |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6448 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6449 | spy2->assertNoEvents(); |
| 6450 | spy1->consumeMotionCancel(); |
| 6451 | window->consumeMotionCancel(); |
| 6452 | |
| 6453 | // The rest of the gesture should only be sent to the second spy window. |
| 6454 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6455 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6456 | ADISPLAY_ID_DEFAULT)) |
| 6457 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6458 | spy2->consumeMotionMove(); |
| 6459 | spy1->assertNoEvents(); |
| 6460 | window->assertNoEvents(); |
| 6461 | } |
| 6462 | |
| 6463 | /** |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6464 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 6465 | * in the middle of the gesture. |
| 6466 | */ |
| 6467 | TEST_F(InputDispatcherSpyWindowTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 6468 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6469 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6470 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 6471 | |
| 6472 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6473 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6474 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6475 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6476 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6477 | |
| 6478 | window->releaseChannel(); |
| 6479 | |
| 6480 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 6481 | |
| 6482 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6483 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6484 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6485 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6486 | } |
| 6487 | |
| 6488 | /** |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6489 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 6490 | * the spy, but not to any other windows. |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6491 | */ |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6492 | TEST_F(InputDispatcherSpyWindowTest, ContinuesToReceiveGestureAfterPilfer) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6493 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6494 | auto window = createForeground(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6495 | |
| 6496 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 6497 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6498 | // First finger down on the window and the spy. |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6499 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6500 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6501 | {100, 200})) |
| 6502 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6503 | spy->consumeMotionDown(); |
| 6504 | window->consumeMotionDown(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6505 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6506 | // Spy window pilfers the pointers. |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6507 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6508 | window->consumeMotionCancel(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6509 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6510 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6511 | const MotionEvent secondFingerDownEvent = |
| 6512 | MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 6513 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 6514 | AINPUT_SOURCE_TOUCHSCREEN) |
| 6515 | .displayId(ADISPLAY_ID_DEFAULT) |
| 6516 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6517 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 6518 | .x(100) |
| 6519 | .y(200)) |
| 6520 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6521 | .build(); |
| 6522 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6523 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6524 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6525 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6526 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6527 | spy->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 6528 | |
| 6529 | // Third finger goes down outside all windows, so injection should fail. |
| 6530 | const MotionEvent thirdFingerDownEvent = |
| 6531 | MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 6532 | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 6533 | AINPUT_SOURCE_TOUCHSCREEN) |
| 6534 | .displayId(ADISPLAY_ID_DEFAULT) |
| 6535 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6536 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 6537 | .x(100) |
| 6538 | .y(200)) |
| 6539 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6540 | .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-5).y(-5)) |
| 6541 | .build(); |
| 6542 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6543 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6544 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6545 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6546 | |
| 6547 | spy->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6548 | window->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6549 | } |
| 6550 | |
| 6551 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6552 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 6553 | * pointers that are down within its bounds. |
| 6554 | */ |
| 6555 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 6556 | auto windowLeft = createForeground(); |
| 6557 | windowLeft->setFrame({0, 0, 100, 200}); |
| 6558 | auto windowRight = createForeground(); |
| 6559 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6560 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6561 | spy->setFrame({0, 0, 200, 200}); |
| 6562 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); |
| 6563 | |
| 6564 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6565 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6566 | {50, 50})) |
| 6567 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6568 | windowLeft->consumeMotionDown(); |
| 6569 | spy->consumeMotionDown(); |
| 6570 | |
| 6571 | const MotionEvent secondFingerDownEvent = |
| 6572 | MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 6573 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 6574 | AINPUT_SOURCE_TOUCHSCREEN) |
| 6575 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6576 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6577 | .pointer( |
| 6578 | PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 6579 | .build(); |
| 6580 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6581 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6582 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6583 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6584 | windowRight->consumeMotionDown(); |
| 6585 | spy->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 6586 | } |
| 6587 | |
| 6588 | /** |
| 6589 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 6590 | * the spy should receive the second pointer with ACTION_DOWN. |
| 6591 | */ |
| 6592 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 6593 | auto window = createForeground(); |
| 6594 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6595 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6596 | spyRight->setFrame({100, 0, 200, 200}); |
| 6597 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); |
| 6598 | |
| 6599 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6600 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6601 | {50, 50})) |
| 6602 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6603 | window->consumeMotionDown(); |
| 6604 | spyRight->assertNoEvents(); |
| 6605 | |
| 6606 | const MotionEvent secondFingerDownEvent = |
| 6607 | MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 6608 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 6609 | AINPUT_SOURCE_TOUCHSCREEN) |
| 6610 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6611 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6612 | .pointer( |
| 6613 | PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 6614 | .build(); |
| 6615 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6616 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6617 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6618 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6619 | window->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 6620 | spyRight->consumeMotionDown(); |
| 6621 | } |
| 6622 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6623 | /** |
| 6624 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 6625 | * windows should be allowed to control split touch. |
| 6626 | */ |
| 6627 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6628 | // This spy window prevents touch splitting. However, we still expect to split touches |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6629 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6630 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6631 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6632 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6633 | auto window = createForeground(); |
| 6634 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6635 | |
| 6636 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 6637 | |
| 6638 | // First finger down, no window touched. |
| 6639 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6640 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6641 | {100, 200})) |
| 6642 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6643 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6644 | window->assertNoEvents(); |
| 6645 | |
| 6646 | // Second finger down on window, the window should receive touch down. |
| 6647 | const MotionEvent secondFingerDownEvent = |
| 6648 | MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 6649 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 6650 | AINPUT_SOURCE_TOUCHSCREEN) |
| 6651 | .displayId(ADISPLAY_ID_DEFAULT) |
| 6652 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6653 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 6654 | .x(100) |
| 6655 | .y(200)) |
| 6656 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6657 | .build(); |
| 6658 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6659 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6660 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6661 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6662 | |
| 6663 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6664 | spy->consumeMotionPointerDown(1 /* pointerIndex */); |
| 6665 | } |
| 6666 | |
| 6667 | /** |
| 6668 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 6669 | * do not receive key events. |
| 6670 | */ |
| 6671 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6672 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6673 | spy->setFocusable(false); |
| 6674 | |
| 6675 | auto window = createForeground(); |
| 6676 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 6677 | setFocusedWindow(window); |
| 6678 | window->consumeFocusEvent(true); |
| 6679 | |
| 6680 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6681 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 6682 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6683 | |
| 6684 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6685 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 6686 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6687 | |
| 6688 | spy->assertNoEvents(); |
| 6689 | } |
| 6690 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 6691 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 6692 | public: |
| 6693 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 6694 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 6695 | std::make_shared<FakeApplicationHandle>(); |
| 6696 | sp<FakeWindowHandle> overlay = |
| 6697 | new FakeWindowHandle(overlayApplication, mDispatcher, "Stylus interceptor window", |
| 6698 | ADISPLAY_ID_DEFAULT); |
| 6699 | overlay->setFocusable(false); |
| 6700 | overlay->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6701 | overlay->setTouchable(false); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 6702 | overlay->setInputFeatures(WindowInfo::Feature::INTERCEPTS_STYLUS); |
| 6703 | overlay->setTrustedOverlay(true); |
| 6704 | |
| 6705 | std::shared_ptr<FakeApplicationHandle> application = |
| 6706 | std::make_shared<FakeApplicationHandle>(); |
| 6707 | sp<FakeWindowHandle> window = |
| 6708 | new FakeWindowHandle(application, mDispatcher, "Application window", |
| 6709 | ADISPLAY_ID_DEFAULT); |
| 6710 | window->setFocusable(true); |
| 6711 | window->setOwnerInfo(222, 222); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 6712 | |
| 6713 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6714 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 6715 | setFocusedWindow(window); |
| 6716 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 6717 | return {std::move(overlay), std::move(window)}; |
| 6718 | } |
| 6719 | |
| 6720 | void sendFingerEvent(int32_t action) { |
| 6721 | NotifyMotionArgs motionArgs = |
| 6722 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 6723 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}}); |
| 6724 | mDispatcher->notifyMotion(&motionArgs); |
| 6725 | } |
| 6726 | |
| 6727 | void sendStylusEvent(int32_t action) { |
| 6728 | NotifyMotionArgs motionArgs = |
| 6729 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 6730 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
| 6731 | motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 6732 | mDispatcher->notifyMotion(&motionArgs); |
| 6733 | } |
| 6734 | }; |
| 6735 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 6736 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 6737 | |
| 6738 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
| 6739 | ScopedSilentDeath _silentDeath; |
| 6740 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 6741 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 6742 | overlay->setTrustedOverlay(false); |
| 6743 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
| 6744 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), |
| 6745 | ".* not a trusted overlay"); |
| 6746 | } |
| 6747 | |
| 6748 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 6749 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 6750 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 6751 | |
| 6752 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 6753 | overlay->consumeMotionDown(); |
| 6754 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 6755 | overlay->consumeMotionUp(); |
| 6756 | |
| 6757 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 6758 | window->consumeMotionDown(); |
| 6759 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 6760 | window->consumeMotionUp(); |
| 6761 | |
| 6762 | overlay->assertNoEvents(); |
| 6763 | window->assertNoEvents(); |
| 6764 | } |
| 6765 | |
| 6766 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 6767 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 6768 | overlay->setInputFeatures(overlay->getInfo()->inputFeatures | WindowInfo::Feature::SPY); |
| 6769 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 6770 | |
| 6771 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 6772 | overlay->consumeMotionDown(); |
| 6773 | window->consumeMotionDown(); |
| 6774 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 6775 | overlay->consumeMotionUp(); |
| 6776 | window->consumeMotionUp(); |
| 6777 | |
| 6778 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 6779 | window->consumeMotionDown(); |
| 6780 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 6781 | window->consumeMotionUp(); |
| 6782 | |
| 6783 | overlay->assertNoEvents(); |
| 6784 | window->assertNoEvents(); |
| 6785 | } |
| 6786 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6787 | } // namespace android::inputdispatcher |