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 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 19 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 20 | #include <android-base/thread_annotations.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 21 | #include <binder/Binder.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 23 | #include <input/Input.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 24 | #include <linux/input.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 25 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 26 | #include <cinttypes> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 27 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 28 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 29 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 30 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 31 | using android::base::StringPrintf; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 32 | using android::os::InputEventInjectionResult; |
| 33 | using android::os::InputEventInjectionSync; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 34 | using android::os::TouchOcclusionMode; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 35 | using namespace android::flag_operators; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 36 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 37 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 38 | |
| 39 | // An arbitrary time value. |
| 40 | static const nsecs_t ARBITRARY_TIME = 1234; |
| 41 | |
| 42 | // An arbitrary device id. |
| 43 | static const int32_t DEVICE_ID = 1; |
| 44 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 45 | // An arbitrary display id. |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 46 | static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 47 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 48 | // An arbitrary injector pid / uid pair that has permission to inject events. |
| 49 | static const int32_t INJECTOR_PID = 999; |
| 50 | static const int32_t INJECTOR_UID = 1001; |
| 51 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 52 | // An arbitrary pid of the gesture monitor window |
| 53 | static constexpr int32_t MONITOR_PID = 2001; |
| 54 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 55 | struct PointF { |
| 56 | float x; |
| 57 | float y; |
| 58 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 59 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 60 | /** |
| 61 | * Return a DOWN key event with KEYCODE_A. |
| 62 | */ |
| 63 | static KeyEvent getTestKeyEvent() { |
| 64 | KeyEvent event; |
| 65 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 66 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 67 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 68 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 69 | return event; |
| 70 | } |
| 71 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 72 | // --- FakeInputDispatcherPolicy --- |
| 73 | |
| 74 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 75 | InputDispatcherConfiguration mConfig; |
| 76 | |
| 77 | protected: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 78 | virtual ~FakeInputDispatcherPolicy() {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 79 | |
| 80 | public: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 81 | FakeInputDispatcherPolicy() {} |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 82 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 83 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 84 | assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action, |
| 85 | args.displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 86 | } |
| 87 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 88 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) { |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 89 | assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action, |
| 90 | args.displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 91 | } |
| 92 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 93 | void assertFilterInputEventWasNotCalled() { |
| 94 | std::scoped_lock lock(mLock); |
| 95 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 96 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 97 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 98 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 99 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 100 | ASSERT_TRUE(mConfigurationChangedTime) |
| 101 | << "Timed out waiting for configuration changed call"; |
| 102 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 103 | mConfigurationChangedTime = std::nullopt; |
| 104 | } |
| 105 | |
| 106 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 107 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 108 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 109 | // 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] | 110 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 111 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 112 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 113 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 114 | mLastNotifySwitch = std::nullopt; |
| 115 | } |
| 116 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 117 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 118 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 119 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 120 | mOnPointerDownToken.clear(); |
| 121 | } |
| 122 | |
| 123 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 124 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 125 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 126 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 129 | // This function must be called soon after the expected ANR timer starts, |
| 130 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 131 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 132 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 133 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
| 134 | std::shared_ptr<InputApplicationHandle> application; |
| 135 | { // acquire lock |
| 136 | std::unique_lock lock(mLock); |
| 137 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 138 | ASSERT_NO_FATAL_FAILURE( |
| 139 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
| 140 | } // release lock |
| 141 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 144 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 145 | const sp<IBinder>& expectedConnectionToken) { |
| 146 | sp<IBinder> connectionToken = getUnresponsiveWindowToken(timeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 147 | ASSERT_EQ(expectedConnectionToken, connectionToken); |
| 148 | } |
| 149 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 150 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) { |
| 151 | sp<IBinder> connectionToken = getResponsiveWindowToken(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 152 | ASSERT_EQ(expectedConnectionToken, connectionToken); |
| 153 | } |
| 154 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 155 | void assertNotifyMonitorUnresponsiveWasCalled(std::chrono::nanoseconds timeout) { |
| 156 | int32_t pid = getUnresponsiveMonitorPid(timeout); |
| 157 | ASSERT_EQ(MONITOR_PID, pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 158 | } |
| 159 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 160 | void assertNotifyMonitorResponsiveWasCalled() { |
| 161 | int32_t pid = getResponsiveMonitorPid(); |
| 162 | ASSERT_EQ(MONITOR_PID, pid); |
| 163 | } |
| 164 | |
| 165 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 166 | std::unique_lock lock(mLock); |
| 167 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 168 | return getAnrTokenLockedInterruptible(timeout, mAnrWindowTokens, lock); |
| 169 | } |
| 170 | |
| 171 | sp<IBinder> getResponsiveWindowToken() { |
| 172 | std::unique_lock lock(mLock); |
| 173 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 174 | return getAnrTokenLockedInterruptible(0s, mResponsiveWindowTokens, lock); |
| 175 | } |
| 176 | |
| 177 | int32_t getUnresponsiveMonitorPid(std::chrono::nanoseconds timeout) { |
| 178 | std::unique_lock lock(mLock); |
| 179 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 180 | return getAnrTokenLockedInterruptible(timeout, mAnrMonitorPids, lock); |
| 181 | } |
| 182 | |
| 183 | int32_t getResponsiveMonitorPid() { |
| 184 | std::unique_lock lock(mLock); |
| 185 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 186 | return getAnrTokenLockedInterruptible(0s, mResponsiveMonitorPids, lock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 190 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 191 | // first entry from the container and erase it. |
| 192 | template <class T> |
| 193 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 194 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 195 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 196 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 197 | |
| 198 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 199 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 200 | // before checking if ANR was called. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 201 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 202 | // to provide it some time to act. 100ms seems reasonable. |
| 203 | mNotifyAnr.wait_for(lock, timeToWait, |
| 204 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 205 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 206 | if (storage.empty()) { |
| 207 | ADD_FAILURE() << "Did not receive the ANR callback"; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 208 | return {}; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 209 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 210 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 211 | // the dispatcher started counting before this function was called |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 212 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 213 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 214 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 215 | << "ms, but waited " |
| 216 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 217 | << "ms instead"; |
| 218 | } |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 219 | T token = storage.front(); |
| 220 | storage.pop(); |
| 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()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 227 | ASSERT_TRUE(mAnrWindowTokens.empty()); |
| 228 | ASSERT_TRUE(mAnrMonitorPids.empty()); |
| 229 | ASSERT_TRUE(mResponsiveWindowTokens.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 230 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 231 | "signal"; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 232 | ASSERT_TRUE(mResponsiveMonitorPids.empty()) |
| 233 | << "Monitor ANR was not called, but please also consume the 'monitor is responsive'" |
| 234 | " signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 237 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 238 | mConfig.keyRepeatTimeout = timeout; |
| 239 | mConfig.keyRepeatDelay = delay; |
| 240 | } |
| 241 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 242 | void waitForSetPointerCapture(bool enabled) { |
| 243 | std::unique_lock lock(mLock); |
| 244 | base::ScopedLockAssertion assumeLocked(mLock); |
| 245 | |
| 246 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 247 | [this, enabled]() REQUIRES(mLock) { |
| 248 | return mPointerCaptureEnabled && |
| 249 | *mPointerCaptureEnabled == |
| 250 | enabled; |
| 251 | })) { |
| 252 | FAIL() << "Timed out waiting for setPointerCapture(" << enabled << ") to be called."; |
| 253 | } |
| 254 | mPointerCaptureEnabled.reset(); |
| 255 | } |
| 256 | |
| 257 | void assertSetPointerCaptureNotCalled() { |
| 258 | std::unique_lock lock(mLock); |
| 259 | base::ScopedLockAssertion assumeLocked(mLock); |
| 260 | |
| 261 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
| 262 | FAIL() << "Expected setPointerCapture(enabled) to not be called, but was called. " |
| 263 | "enabled = " |
| 264 | << *mPointerCaptureEnabled; |
| 265 | } |
| 266 | mPointerCaptureEnabled.reset(); |
| 267 | } |
| 268 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 269 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { |
| 270 | std::scoped_lock lock(mLock); |
| 271 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
| 272 | } |
| 273 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 274 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 275 | std::mutex mLock; |
| 276 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 277 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 278 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 279 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 280 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 281 | std::condition_variable mPointerCaptureChangedCondition; |
| 282 | std::optional<bool> mPointerCaptureEnabled GUARDED_BY(mLock); |
| 283 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 284 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 285 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 286 | std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock); |
| 287 | std::queue<sp<IBinder>> mResponsiveWindowTokens GUARDED_BY(mLock); |
| 288 | std::queue<int32_t> mAnrMonitorPids GUARDED_BY(mLock); |
| 289 | std::queue<int32_t> mResponsiveMonitorPids GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 290 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 291 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 292 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
| 293 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 294 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 295 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 296 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 299 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 300 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 301 | mAnrWindowTokens.push(connectionToken); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 302 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 303 | } |
| 304 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 305 | void notifyMonitorUnresponsive(int32_t pid, const std::string&) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 306 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 307 | mAnrMonitorPids.push(pid); |
| 308 | mNotifyAnr.notify_all(); |
| 309 | } |
| 310 | |
| 311 | void notifyWindowResponsive(const sp<IBinder>& connectionToken) override { |
| 312 | std::scoped_lock lock(mLock); |
| 313 | mResponsiveWindowTokens.push(connectionToken); |
| 314 | mNotifyAnr.notify_all(); |
| 315 | } |
| 316 | |
| 317 | void notifyMonitorResponsive(int32_t pid) override { |
| 318 | std::scoped_lock lock(mLock); |
| 319 | mResponsiveMonitorPids.push(pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 320 | mNotifyAnr.notify_all(); |
| 321 | } |
| 322 | |
| 323 | void notifyNoFocusedWindowAnr( |
| 324 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 325 | std::scoped_lock lock(mLock); |
| 326 | mAnrApplications.push(applicationHandle); |
| 327 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 330 | void notifyInputChannelBroken(const sp<IBinder>&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 331 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 332 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 333 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 334 | void notifyUntrustedTouch(const std::string& obscuringPackage) override {} |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 335 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 336 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 337 | const std::vector<float>& values) override {} |
| 338 | |
| 339 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 340 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 341 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 342 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 343 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 344 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 345 | *outConfig = mConfig; |
| 346 | } |
| 347 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 348 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 349 | std::scoped_lock lock(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 350 | switch (inputEvent->getType()) { |
| 351 | case AINPUT_EVENT_TYPE_KEY: { |
| 352 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 353 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 354 | break; |
| 355 | } |
| 356 | |
| 357 | case AINPUT_EVENT_TYPE_MOTION: { |
| 358 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 359 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 360 | break; |
| 361 | } |
| 362 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 363 | return true; |
| 364 | } |
| 365 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 366 | void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 367 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 368 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 369 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 370 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 371 | return 0; |
| 372 | } |
| 373 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 374 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 375 | return false; |
| 376 | } |
| 377 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 378 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 379 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 380 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 381 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 382 | * essentially a passthrough for notifySwitch. |
| 383 | */ |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 384 | mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 387 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 388 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 389 | bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 390 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 391 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 392 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 393 | mOnPointerDownToken = newToken; |
| 394 | } |
| 395 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 396 | void setPointerCapture(bool enabled) override { |
| 397 | std::scoped_lock lock(mLock); |
| 398 | mPointerCaptureEnabled = {enabled}; |
| 399 | mPointerCaptureChangedCondition.notify_all(); |
| 400 | } |
| 401 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 402 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 403 | std::scoped_lock lock(mLock); |
| 404 | mDropTargetWindowToken = token; |
| 405 | } |
| 406 | |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 407 | void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action, |
| 408 | int32_t displayId) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 409 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 410 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
| 411 | ASSERT_EQ(mFilteredEvent->getType(), type); |
| 412 | |
| 413 | if (type == AINPUT_EVENT_TYPE_KEY) { |
| 414 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent); |
| 415 | EXPECT_EQ(keyEvent.getEventTime(), eventTime); |
| 416 | EXPECT_EQ(keyEvent.getAction(), action); |
| 417 | EXPECT_EQ(keyEvent.getDisplayId(), displayId); |
| 418 | } else if (type == AINPUT_EVENT_TYPE_MOTION) { |
| 419 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent); |
| 420 | EXPECT_EQ(motionEvent.getEventTime(), eventTime); |
| 421 | EXPECT_EQ(motionEvent.getAction(), action); |
| 422 | EXPECT_EQ(motionEvent.getDisplayId(), displayId); |
| 423 | } else { |
| 424 | FAIL() << "Unknown type: " << type; |
| 425 | } |
| 426 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 427 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 428 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 429 | }; |
| 430 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 431 | // --- InputDispatcherTest --- |
| 432 | |
| 433 | class InputDispatcherTest : public testing::Test { |
| 434 | protected: |
| 435 | sp<FakeInputDispatcherPolicy> mFakePolicy; |
| 436 | sp<InputDispatcher> mDispatcher; |
| 437 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 438 | virtual void SetUp() override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 439 | mFakePolicy = new FakeInputDispatcherPolicy(); |
| 440 | mDispatcher = new InputDispatcher(mFakePolicy); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 441 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 442 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 443 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 444 | } |
| 445 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 446 | virtual void TearDown() override { |
| 447 | ASSERT_EQ(OK, mDispatcher->stop()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 448 | mFakePolicy.clear(); |
| 449 | mDispatcher.clear(); |
| 450 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 451 | |
| 452 | /** |
| 453 | * Used for debugging when writing the test |
| 454 | */ |
| 455 | void dumpDispatcherState() { |
| 456 | std::string dump; |
| 457 | mDispatcher->dump(dump); |
| 458 | std::stringstream ss(dump); |
| 459 | std::string to; |
| 460 | |
| 461 | while (std::getline(ss, to, '\n')) { |
| 462 | ALOGE("%s", to.c_str()); |
| 463 | } |
| 464 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 465 | |
| 466 | void setFocusedWindow(const sp<InputWindowHandle>& window, |
| 467 | const sp<InputWindowHandle>& focusedWindow = nullptr) { |
| 468 | FocusRequest request; |
| 469 | request.token = window->getToken(); |
| 470 | if (focusedWindow) { |
| 471 | request.focusedToken = focusedWindow->getToken(); |
| 472 | } |
| 473 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 474 | request.displayId = window->getInfo()->displayId; |
| 475 | mDispatcher->setFocusedWindow(request); |
| 476 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 477 | }; |
| 478 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 479 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 480 | KeyEvent event; |
| 481 | |
| 482 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 483 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 484 | INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 485 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 486 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 487 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 488 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 489 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 490 | << "Should reject key events with undefined action."; |
| 491 | |
| 492 | // 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] | 493 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 494 | 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] | 495 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 496 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 497 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 498 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 499 | << "Should reject key events with ACTION_MULTIPLE."; |
| 500 | } |
| 501 | |
| 502 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 503 | MotionEvent event; |
| 504 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 505 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
| 506 | for (int i = 0; i <= MAX_POINTERS; i++) { |
| 507 | pointerProperties[i].clear(); |
| 508 | pointerProperties[i].id = i; |
| 509 | pointerCoords[i].clear(); |
| 510 | } |
| 511 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 512 | // Some constants commonly used below |
| 513 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 514 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 515 | constexpr int32_t metaState = AMETA_NONE; |
| 516 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 517 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 518 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 519 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 520 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 521 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, |
| 522 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 523 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 524 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 525 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 526 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 527 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 528 | << "Should reject motion events with undefined action."; |
| 529 | |
| 530 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 531 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 532 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 533 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 534 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 535 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 536 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, |
| 537 | pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 538 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 539 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 540 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 541 | << "Should reject motion events with pointer down index too large."; |
| 542 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 543 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 544 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 545 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 546 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 547 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 548 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, |
| 549 | pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 550 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 551 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 552 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 553 | << "Should reject motion events with pointer down index too small."; |
| 554 | |
| 555 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 556 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 557 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 558 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 559 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 560 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 561 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, |
| 562 | pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 563 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 564 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 565 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 566 | << "Should reject motion events with pointer up index too large."; |
| 567 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 568 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 569 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 570 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 571 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 572 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 573 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, |
| 574 | pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 575 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 576 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 577 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 578 | << "Should reject motion events with pointer up index too small."; |
| 579 | |
| 580 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 581 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 582 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 583 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 584 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 585 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 586 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 587 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 588 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 589 | << "Should reject motion events with 0 pointers."; |
| 590 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 591 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 592 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 593 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 594 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 595 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 596 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 597 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 598 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 599 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 600 | |
| 601 | // Rejects motion events with invalid pointer ids. |
| 602 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 603 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 604 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 605 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 606 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 607 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 608 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 609 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 610 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 611 | << "Should reject motion events with pointer ids less than 0."; |
| 612 | |
| 613 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 614 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 615 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 616 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 617 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 618 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 619 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 620 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 621 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 622 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 623 | |
| 624 | // Rejects motion events with duplicate pointer ids. |
| 625 | pointerProperties[0].id = 1; |
| 626 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 627 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 628 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 629 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 630 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 631 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 632 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 633 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 634 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 635 | << "Should reject motion events with duplicate pointer ids."; |
| 636 | } |
| 637 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 638 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 639 | |
| 640 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 641 | constexpr nsecs_t eventTime = 20; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 642 | NotifyConfigurationChangedArgs args(10 /*id*/, eventTime); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 643 | mDispatcher->notifyConfigurationChanged(&args); |
| 644 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 645 | |
| 646 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 647 | } |
| 648 | |
| 649 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 650 | NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/, |
| 651 | 2 /*switchMask*/); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 652 | mDispatcher->notifySwitch(&args); |
| 653 | |
| 654 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 655 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 656 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 657 | } |
| 658 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 659 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 660 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 661 | static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 662 | |
| 663 | class FakeApplicationHandle : public InputApplicationHandle { |
| 664 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 665 | FakeApplicationHandle() { |
| 666 | mInfo.name = "Fake Application"; |
| 667 | mInfo.token = new BBinder(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 668 | mInfo.dispatchingTimeoutMillis = |
| 669 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 670 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 671 | virtual ~FakeApplicationHandle() {} |
| 672 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 673 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 674 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 675 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 676 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 677 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 678 | }; |
| 679 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 680 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 681 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 682 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 683 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 684 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 685 | } |
| 686 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 687 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 688 | InputEvent* event; |
| 689 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 690 | if (!consumeSeq) { |
| 691 | return nullptr; |
| 692 | } |
| 693 | finishEvent(*consumeSeq); |
| 694 | return event; |
| 695 | } |
| 696 | |
| 697 | /** |
| 698 | * Receive an event without acknowledging it. |
| 699 | * Return the sequence number that could later be used to send finished signal. |
| 700 | */ |
| 701 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 702 | uint32_t consumeSeq; |
| 703 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 704 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 705 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 706 | status_t status = WOULD_BLOCK; |
| 707 | while (status == WOULD_BLOCK) { |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 708 | status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 709 | &event); |
| 710 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 711 | if (elapsed > 100ms) { |
| 712 | break; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | if (status == WOULD_BLOCK) { |
| 717 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 718 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | if (status != OK) { |
| 722 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 723 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 724 | } |
| 725 | if (event == nullptr) { |
| 726 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 727 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 728 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 729 | if (outEvent != nullptr) { |
| 730 | *outEvent = event; |
| 731 | } |
| 732 | return consumeSeq; |
| 733 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 734 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 735 | /** |
| 736 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 737 | */ |
| 738 | void finishEvent(uint32_t consumeSeq) { |
| 739 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 740 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 741 | } |
| 742 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame^] | 743 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 744 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); |
| 745 | ASSERT_EQ(OK, status); |
| 746 | } |
| 747 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 748 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 749 | std::optional<int32_t> expectedDisplayId, |
| 750 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 751 | InputEvent* event = consume(); |
| 752 | |
| 753 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 754 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 755 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 756 | << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType) |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 757 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 758 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 759 | if (expectedDisplayId.has_value()) { |
| 760 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 761 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 762 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 763 | switch (expectedEventType) { |
| 764 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 765 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 766 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 767 | if (expectedFlags.has_value()) { |
| 768 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 769 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 770 | break; |
| 771 | } |
| 772 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 773 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 774 | EXPECT_EQ(expectedAction, motionEvent.getAction()); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 775 | if (expectedFlags.has_value()) { |
| 776 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 777 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 778 | break; |
| 779 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 780 | case AINPUT_EVENT_TYPE_FOCUS: { |
| 781 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 782 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 783 | case AINPUT_EVENT_TYPE_CAPTURE: { |
| 784 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 785 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 786 | case AINPUT_EVENT_TYPE_DRAG: { |
| 787 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 788 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 789 | default: { |
| 790 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; |
| 791 | } |
| 792 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 793 | } |
| 794 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 795 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 796 | InputEvent* event = consume(); |
| 797 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 798 | << ": consumer should have returned non-NULL event."; |
| 799 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType()) |
| 800 | << "Got " << inputEventTypeToString(event->getType()) |
| 801 | << " event instead of FOCUS event"; |
| 802 | |
| 803 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 804 | << mName.c_str() << ": event displayId should always be NONE."; |
| 805 | |
| 806 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 807 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
| 808 | EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode()); |
| 809 | } |
| 810 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 811 | void consumeCaptureEvent(bool hasCapture) { |
| 812 | const InputEvent* event = consume(); |
| 813 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 814 | << ": consumer should have returned non-NULL event."; |
| 815 | ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType()) |
| 816 | << "Got " << inputEventTypeToString(event->getType()) |
| 817 | << " event instead of CAPTURE event"; |
| 818 | |
| 819 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 820 | << mName.c_str() << ": event displayId should always be NONE."; |
| 821 | |
| 822 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 823 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 824 | } |
| 825 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 826 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 827 | const InputEvent* event = consume(); |
| 828 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 829 | << ": consumer should have returned non-NULL event."; |
| 830 | ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType()) |
| 831 | << "Got " << inputEventTypeToString(event->getType()) |
| 832 | << " event instead of DRAG event"; |
| 833 | |
| 834 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 835 | << mName.c_str() << ": event displayId should always be NONE."; |
| 836 | |
| 837 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 838 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 839 | EXPECT_EQ(x, dragEvent.getX()); |
| 840 | EXPECT_EQ(y, dragEvent.getY()); |
| 841 | } |
| 842 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 843 | void assertNoEvents() { |
| 844 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 845 | if (event == nullptr) { |
| 846 | return; |
| 847 | } |
| 848 | if (event->getType() == AINPUT_EVENT_TYPE_KEY) { |
| 849 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 850 | ADD_FAILURE() << "Received key event " |
| 851 | << KeyEvent::actionToString(keyEvent.getAction()); |
| 852 | } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) { |
| 853 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 854 | ADD_FAILURE() << "Received motion event " |
| 855 | << MotionEvent::actionToString(motionEvent.getAction()); |
| 856 | } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) { |
| 857 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 858 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 859 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 860 | } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) { |
| 861 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 862 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 863 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 864 | } |
| 865 | FAIL() << mName.c_str() |
| 866 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 870 | |
| 871 | protected: |
| 872 | std::unique_ptr<InputConsumer> mConsumer; |
| 873 | PreallocatedInputEventFactory mEventFactory; |
| 874 | |
| 875 | std::string mName; |
| 876 | }; |
| 877 | |
| 878 | class FakeWindowHandle : public InputWindowHandle { |
| 879 | public: |
| 880 | static const int32_t WIDTH = 600; |
| 881 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 882 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 883 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 884 | const sp<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 885 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 886 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 887 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 888 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 889 | dispatcher->createInputChannel(name); |
| 890 | token = (*channel)->getConnectionToken(); |
| 891 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | inputApplicationHandle->updateInfo(); |
| 895 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 896 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 897 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 898 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 899 | mInfo.name = name; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 900 | mInfo.type = InputWindowInfo::Type::APPLICATION; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 901 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 902 | mInfo.alpha = 1.0; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 903 | mInfo.frameLeft = 0; |
| 904 | mInfo.frameTop = 0; |
| 905 | mInfo.frameRight = WIDTH; |
| 906 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 907 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 908 | mInfo.globalScaleFactor = 1.0; |
| 909 | mInfo.touchableRegion.clear(); |
| 910 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
| 911 | mInfo.visible = true; |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 912 | mInfo.focusable = false; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 913 | mInfo.hasWallpaper = false; |
| 914 | mInfo.paused = false; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 915 | mInfo.ownerPid = INJECTOR_PID; |
| 916 | mInfo.ownerUid = INJECTOR_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 917 | mInfo.displayId = displayId; |
| 918 | } |
| 919 | |
| 920 | virtual bool updateInfo() { return true; } |
| 921 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 922 | void setFocusable(bool focusable) { mInfo.focusable = focusable; } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 923 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 924 | void setVisible(bool visible) { mInfo.visible = visible; } |
| 925 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 926 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 927 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 930 | void setPaused(bool paused) { mInfo.paused = paused; } |
| 931 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 932 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 933 | |
| 934 | void setTouchOcclusionMode(android::os::TouchOcclusionMode mode) { |
| 935 | mInfo.touchOcclusionMode = mode; |
| 936 | } |
| 937 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 938 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 939 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 940 | void setFrame(const Rect& frame) { |
| 941 | mInfo.frameLeft = frame.left; |
| 942 | mInfo.frameTop = frame.top; |
| 943 | mInfo.frameRight = frame.right; |
| 944 | mInfo.frameBottom = frame.bottom; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 945 | mInfo.transform.set(-frame.left, -frame.top); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 946 | mInfo.touchableRegion.clear(); |
| 947 | mInfo.addTouchableRegion(frame); |
| 948 | } |
| 949 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 950 | void addFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags |= flags; } |
| 951 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 952 | void setFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags = flags; } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 953 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 954 | void setInputFeatures(InputWindowInfo::Feature features) { mInfo.inputFeatures = features; } |
| 955 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 956 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 957 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 958 | } |
| 959 | |
| 960 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 961 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 962 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 963 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 964 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 965 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 966 | expectedFlags); |
| 967 | } |
| 968 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 969 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 970 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
| 971 | } |
| 972 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 973 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 974 | int32_t expectedFlags = 0) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 975 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId, |
| 976 | expectedFlags); |
| 977 | } |
| 978 | |
| 979 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 980 | int32_t expectedFlags = 0) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 981 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId, |
| 982 | expectedFlags); |
| 983 | } |
| 984 | |
| 985 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 986 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 987 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 988 | } |
| 989 | |
| 990 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 991 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 992 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
| 993 | expectedFlags); |
| 994 | } |
| 995 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 996 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 997 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 998 | int32_t expectedFlags = 0) { |
| 999 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1000 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1001 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1002 | } |
| 1003 | |
| 1004 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1005 | int32_t expectedFlags = 0) { |
| 1006 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1007 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1008 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1009 | } |
| 1010 | |
| 1011 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1012 | int32_t expectedFlags = 0) { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1013 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
| 1014 | expectedFlags); |
| 1015 | } |
| 1016 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1017 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1018 | int32_t expectedFlags = 0) { |
| 1019 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
| 1020 | expectedFlags); |
| 1021 | } |
| 1022 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1023 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1024 | ASSERT_NE(mInputReceiver, nullptr) |
| 1025 | << "Cannot consume events from a window with no receiver"; |
| 1026 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1027 | } |
| 1028 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1029 | void consumeCaptureEvent(bool hasCapture) { |
| 1030 | ASSERT_NE(mInputReceiver, nullptr) |
| 1031 | << "Cannot consume events from a window with no receiver"; |
| 1032 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1033 | } |
| 1034 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1035 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 1036 | std::optional<int32_t> expectedDisplayId, |
| 1037 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1038 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1039 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1040 | expectedFlags); |
| 1041 | } |
| 1042 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1043 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1044 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1045 | } |
| 1046 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1047 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1048 | if (mInputReceiver == nullptr) { |
| 1049 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1050 | return std::nullopt; |
| 1051 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1052 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | void finishEvent(uint32_t sequenceNum) { |
| 1056 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1057 | mInputReceiver->finishEvent(sequenceNum); |
| 1058 | } |
| 1059 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame^] | 1060 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1061 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1062 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1063 | } |
| 1064 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1065 | InputEvent* consume() { |
| 1066 | if (mInputReceiver == nullptr) { |
| 1067 | return nullptr; |
| 1068 | } |
| 1069 | return mInputReceiver->consume(); |
| 1070 | } |
| 1071 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1072 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1073 | if (mInputReceiver == nullptr && |
| 1074 | mInfo.inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL)) { |
| 1075 | return; // Can't receive events if the window does not have input channel |
| 1076 | } |
| 1077 | ASSERT_NE(nullptr, mInputReceiver) |
| 1078 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1079 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1080 | } |
| 1081 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1082 | sp<IBinder> getToken() { return mInfo.token; } |
| 1083 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1084 | const std::string& getName() { return mName; } |
| 1085 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1086 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { |
| 1087 | mInfo.ownerPid = ownerPid; |
| 1088 | mInfo.ownerUid = ownerUid; |
| 1089 | } |
| 1090 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1091 | private: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1092 | const std::string mName; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1093 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1094 | 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] | 1095 | }; |
| 1096 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1097 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1098 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1099 | static InputEventInjectionResult injectKey( |
| 1100 | const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
| 1101 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1102 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
| 1103 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1104 | KeyEvent event; |
| 1105 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1106 | |
| 1107 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1108 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1109 | INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE, |
| 1110 | repeatCount, currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1111 | |
| 1112 | // Inject event until dispatch out. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1113 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode, |
| 1114 | injectionTimeout, |
| 1115 | POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1116 | } |
| 1117 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1118 | static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher, |
| 1119 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1120 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId); |
| 1121 | } |
| 1122 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1123 | static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher, |
| 1124 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1125 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId); |
| 1126 | } |
| 1127 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1128 | class PointerBuilder { |
| 1129 | public: |
| 1130 | PointerBuilder(int32_t id, int32_t toolType) { |
| 1131 | mProperties.clear(); |
| 1132 | mProperties.id = id; |
| 1133 | mProperties.toolType = toolType; |
| 1134 | mCoords.clear(); |
| 1135 | } |
| 1136 | |
| 1137 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } |
| 1138 | |
| 1139 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } |
| 1140 | |
| 1141 | PointerBuilder& axis(int32_t axis, float value) { |
| 1142 | mCoords.setAxisValue(axis, value); |
| 1143 | return *this; |
| 1144 | } |
| 1145 | |
| 1146 | PointerProperties buildProperties() const { return mProperties; } |
| 1147 | |
| 1148 | PointerCoords buildCoords() const { return mCoords; } |
| 1149 | |
| 1150 | private: |
| 1151 | PointerProperties mProperties; |
| 1152 | PointerCoords mCoords; |
| 1153 | }; |
| 1154 | |
| 1155 | class MotionEventBuilder { |
| 1156 | public: |
| 1157 | MotionEventBuilder(int32_t action, int32_t source) { |
| 1158 | mAction = action; |
| 1159 | mSource = source; |
| 1160 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1161 | } |
| 1162 | |
| 1163 | MotionEventBuilder& eventTime(nsecs_t eventTime) { |
| 1164 | mEventTime = eventTime; |
| 1165 | return *this; |
| 1166 | } |
| 1167 | |
| 1168 | MotionEventBuilder& displayId(int32_t displayId) { |
| 1169 | mDisplayId = displayId; |
| 1170 | return *this; |
| 1171 | } |
| 1172 | |
| 1173 | MotionEventBuilder& actionButton(int32_t actionButton) { |
| 1174 | mActionButton = actionButton; |
| 1175 | return *this; |
| 1176 | } |
| 1177 | |
| 1178 | MotionEventBuilder& buttonState(int32_t actionButton) { |
| 1179 | mActionButton = actionButton; |
| 1180 | return *this; |
| 1181 | } |
| 1182 | |
| 1183 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 1184 | mRawXCursorPosition = rawXCursorPosition; |
| 1185 | return *this; |
| 1186 | } |
| 1187 | |
| 1188 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 1189 | mRawYCursorPosition = rawYCursorPosition; |
| 1190 | return *this; |
| 1191 | } |
| 1192 | |
| 1193 | MotionEventBuilder& pointer(PointerBuilder pointer) { |
| 1194 | mPointers.push_back(pointer); |
| 1195 | return *this; |
| 1196 | } |
| 1197 | |
| 1198 | MotionEvent build() { |
| 1199 | std::vector<PointerProperties> pointerProperties; |
| 1200 | std::vector<PointerCoords> pointerCoords; |
| 1201 | for (const PointerBuilder& pointer : mPointers) { |
| 1202 | pointerProperties.push_back(pointer.buildProperties()); |
| 1203 | pointerCoords.push_back(pointer.buildCoords()); |
| 1204 | } |
| 1205 | |
| 1206 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 1207 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 1208 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 1209 | mPointers.size() == 1) { |
| 1210 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 1211 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 1212 | } |
| 1213 | |
| 1214 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1215 | ui::Transform identityTransform; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1216 | event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC, |
| 1217 | mAction, mActionButton, /* flags */ 0, /* edgeFlags */ 0, AMETA_NONE, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1218 | mButtonState, MotionClassification::NONE, identityTransform, |
| 1219 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, |
| 1220 | mRawYCursorPosition, mEventTime, mEventTime, mPointers.size(), |
| 1221 | pointerProperties.data(), pointerCoords.data()); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1222 | |
| 1223 | return event; |
| 1224 | } |
| 1225 | |
| 1226 | private: |
| 1227 | int32_t mAction; |
| 1228 | int32_t mSource; |
| 1229 | nsecs_t mEventTime; |
| 1230 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1231 | int32_t mActionButton{0}; |
| 1232 | int32_t mButtonState{0}; |
| 1233 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1234 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1235 | |
| 1236 | std::vector<PointerBuilder> mPointers; |
| 1237 | }; |
| 1238 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1239 | static InputEventInjectionResult injectMotionEvent( |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1240 | const sp<InputDispatcher>& dispatcher, const MotionEvent& event, |
| 1241 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1242 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1243 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode, |
| 1244 | injectionTimeout, |
| 1245 | POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
| 1246 | } |
| 1247 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1248 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1249 | const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId, |
| 1250 | const PointF& position, |
| 1251 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1252 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1253 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1254 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1255 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1256 | MotionEvent event = MotionEventBuilder(action, source) |
| 1257 | .displayId(displayId) |
| 1258 | .eventTime(eventTime) |
| 1259 | .rawXCursorPosition(cursorPosition.x) |
| 1260 | .rawYCursorPosition(cursorPosition.y) |
| 1261 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1262 | .x(position.x) |
| 1263 | .y(position.y)) |
| 1264 | .build(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1265 | |
| 1266 | // Inject event until dispatch out. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1267 | return injectMotionEvent(dispatcher, event); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1268 | } |
| 1269 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1270 | static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher, |
| 1271 | int32_t source, int32_t displayId, |
| 1272 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1273 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1276 | static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher, |
| 1277 | int32_t source, int32_t displayId, |
| 1278 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1279 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1282 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1283 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1284 | // Define a valid key event. |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1285 | NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 1286 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, |
| 1287 | KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1288 | |
| 1289 | return args; |
| 1290 | } |
| 1291 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1292 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, |
| 1293 | const std::vector<PointF>& points) { |
| 1294 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1295 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1296 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1297 | } |
| 1298 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1299 | PointerProperties pointerProperties[pointerCount]; |
| 1300 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1301 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1302 | for (size_t i = 0; i < pointerCount; i++) { |
| 1303 | pointerProperties[i].clear(); |
| 1304 | pointerProperties[i].id = i; |
| 1305 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1306 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1307 | pointerCoords[i].clear(); |
| 1308 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1309 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1310 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1311 | |
| 1312 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1313 | // Define a valid motion event. |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1314 | NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1315 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 1316 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1317 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 1318 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1319 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 1320 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1321 | |
| 1322 | return args; |
| 1323 | } |
| 1324 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1325 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1326 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1327 | } |
| 1328 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1329 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(bool enabled) { |
| 1330 | return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), enabled); |
| 1331 | } |
| 1332 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1333 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1334 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1335 | sp<FakeWindowHandle> window = |
| 1336 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1337 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1338 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1339 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1340 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1341 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1342 | |
| 1343 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1344 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1345 | } |
| 1346 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1347 | /** |
| 1348 | * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues. |
| 1349 | * To ensure that window receives only events that were directly inside of it, add |
| 1350 | * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input |
| 1351 | * when finding touched windows. |
| 1352 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1353 | * called twice. |
| 1354 | */ |
| 1355 | TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1356 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1357 | sp<FakeWindowHandle> window = |
| 1358 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1359 | window->setFrame(Rect(0, 0, 100, 100)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1360 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1361 | |
| 1362 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1363 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1364 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1365 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1366 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1367 | |
| 1368 | // Window should receive motion event. |
| 1369 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1370 | } |
| 1371 | |
| 1372 | /** |
| 1373 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
| 1374 | * To ensure that window receives only events that were directly inside of it, add |
| 1375 | * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input |
| 1376 | * when finding touched windows. |
| 1377 | */ |
| 1378 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1379 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1380 | sp<FakeWindowHandle> window = |
| 1381 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1382 | window->setFrame(Rect(0, 0, 100, 100)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1383 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1384 | |
| 1385 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1386 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1387 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1388 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1389 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1390 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1391 | |
| 1392 | // Window should receive motion event. |
| 1393 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1394 | } |
| 1395 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1396 | // The foreground window should receive the first touch down event. |
| 1397 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1398 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1399 | sp<FakeWindowHandle> windowTop = |
| 1400 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 1401 | sp<FakeWindowHandle> windowSecond = |
| 1402 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1403 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1404 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1405 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1406 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1407 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1408 | |
| 1409 | // 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] | 1410 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1411 | windowSecond->assertNoEvents(); |
| 1412 | } |
| 1413 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1414 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1415 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1416 | sp<FakeWindowHandle> windowLeft = |
| 1417 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1418 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1419 | windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1420 | sp<FakeWindowHandle> windowRight = |
| 1421 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1422 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1423 | windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1424 | |
| 1425 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1426 | |
| 1427 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 1428 | |
| 1429 | // 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] | 1430 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1431 | injectMotionEvent(mDispatcher, |
| 1432 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1433 | AINPUT_SOURCE_MOUSE) |
| 1434 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1435 | .x(900) |
| 1436 | .y(400)) |
| 1437 | .build())); |
| 1438 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1439 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1440 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1441 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1442 | |
| 1443 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1444 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1445 | injectMotionEvent(mDispatcher, |
| 1446 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1447 | AINPUT_SOURCE_MOUSE) |
| 1448 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1449 | .x(300) |
| 1450 | .y(400)) |
| 1451 | .build())); |
| 1452 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1453 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1454 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1455 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1456 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1457 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1458 | |
| 1459 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1460 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1461 | injectMotionEvent(mDispatcher, |
| 1462 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 1463 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1464 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1465 | .x(300) |
| 1466 | .y(400)) |
| 1467 | .build())); |
| 1468 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1469 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1470 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1471 | injectMotionEvent(mDispatcher, |
| 1472 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1473 | AINPUT_SOURCE_MOUSE) |
| 1474 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1475 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1476 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1477 | .x(300) |
| 1478 | .y(400)) |
| 1479 | .build())); |
| 1480 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1481 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1482 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1483 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1484 | injectMotionEvent(mDispatcher, |
| 1485 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1486 | AINPUT_SOURCE_MOUSE) |
| 1487 | .buttonState(0) |
| 1488 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1489 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1490 | .x(300) |
| 1491 | .y(400)) |
| 1492 | .build())); |
| 1493 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1494 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1495 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1496 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1497 | injectMotionEvent(mDispatcher, |
| 1498 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 1499 | .buttonState(0) |
| 1500 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1501 | .x(300) |
| 1502 | .y(400)) |
| 1503 | .build())); |
| 1504 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1505 | |
| 1506 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1507 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1508 | injectMotionEvent(mDispatcher, |
| 1509 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1510 | AINPUT_SOURCE_MOUSE) |
| 1511 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1512 | .x(900) |
| 1513 | .y(400)) |
| 1514 | .build())); |
| 1515 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1516 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1517 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1518 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1519 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1520 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1521 | } |
| 1522 | |
| 1523 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 1524 | // directly in this test. |
| 1525 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1526 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1527 | sp<FakeWindowHandle> window = |
| 1528 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1529 | window->setFrame(Rect(0, 0, 1200, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1530 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1531 | |
| 1532 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1533 | |
| 1534 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1535 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1536 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1537 | injectMotionEvent(mDispatcher, |
| 1538 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1539 | AINPUT_SOURCE_MOUSE) |
| 1540 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1541 | .x(300) |
| 1542 | .y(400)) |
| 1543 | .build())); |
| 1544 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1545 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1546 | |
| 1547 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1548 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1549 | injectMotionEvent(mDispatcher, |
| 1550 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 1551 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1552 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1553 | .x(300) |
| 1554 | .y(400)) |
| 1555 | .build())); |
| 1556 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1557 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1558 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1559 | injectMotionEvent(mDispatcher, |
| 1560 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1561 | AINPUT_SOURCE_MOUSE) |
| 1562 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1563 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1564 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1565 | .x(300) |
| 1566 | .y(400)) |
| 1567 | .build())); |
| 1568 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1569 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1570 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1571 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1572 | injectMotionEvent(mDispatcher, |
| 1573 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1574 | AINPUT_SOURCE_MOUSE) |
| 1575 | .buttonState(0) |
| 1576 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1577 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1578 | .x(300) |
| 1579 | .y(400)) |
| 1580 | .build())); |
| 1581 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1582 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1583 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1584 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1585 | injectMotionEvent(mDispatcher, |
| 1586 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 1587 | .buttonState(0) |
| 1588 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1589 | .x(300) |
| 1590 | .y(400)) |
| 1591 | .build())); |
| 1592 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1593 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1594 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1595 | injectMotionEvent(mDispatcher, |
| 1596 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1597 | AINPUT_SOURCE_MOUSE) |
| 1598 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1599 | .x(300) |
| 1600 | .y(400)) |
| 1601 | .build())); |
| 1602 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1603 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1604 | } |
| 1605 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1606 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1607 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1608 | |
| 1609 | sp<FakeWindowHandle> windowLeft = |
| 1610 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1611 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1612 | windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1613 | sp<FakeWindowHandle> windowRight = |
| 1614 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1615 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1616 | windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1617 | |
| 1618 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1619 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1620 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1621 | |
| 1622 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 1623 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1624 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1625 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1626 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1627 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1628 | windowRight->assertNoEvents(); |
| 1629 | } |
| 1630 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1631 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1632 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1633 | sp<FakeWindowHandle> window = |
| 1634 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1635 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1636 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1637 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1638 | setFocusedWindow(window); |
| 1639 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1640 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1641 | |
| 1642 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1643 | mDispatcher->notifyKey(&keyArgs); |
| 1644 | |
| 1645 | // Window should receive key down event. |
| 1646 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 1647 | |
| 1648 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 1649 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1650 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1651 | mDispatcher->notifyDeviceReset(&args); |
| 1652 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 1653 | AKEY_EVENT_FLAG_CANCELED); |
| 1654 | } |
| 1655 | |
| 1656 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1657 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1658 | sp<FakeWindowHandle> window = |
| 1659 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1660 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1661 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1662 | |
| 1663 | NotifyMotionArgs motionArgs = |
| 1664 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1665 | ADISPLAY_ID_DEFAULT); |
| 1666 | mDispatcher->notifyMotion(&motionArgs); |
| 1667 | |
| 1668 | // Window should receive motion down event. |
| 1669 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1670 | |
| 1671 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 1672 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1673 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1674 | mDispatcher->notifyDeviceReset(&args); |
| 1675 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, |
| 1676 | 0 /*expectedFlags*/); |
| 1677 | } |
| 1678 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1679 | TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1680 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1681 | |
| 1682 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1683 | sp<FakeWindowHandle> firstWindow = |
| 1684 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 1685 | sp<FakeWindowHandle> secondWindow = |
| 1686 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1687 | |
| 1688 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1689 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1690 | |
| 1691 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1692 | NotifyMotionArgs downMotionArgs = |
| 1693 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1694 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1695 | mDispatcher->notifyMotion(&downMotionArgs); |
| 1696 | // Only the first window should get the down event |
| 1697 | firstWindow->consumeMotionDown(); |
| 1698 | secondWindow->assertNoEvents(); |
| 1699 | |
| 1700 | // Transfer touch focus to the second window |
| 1701 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 1702 | // The first window gets cancel and the second gets down |
| 1703 | firstWindow->consumeMotionCancel(); |
| 1704 | secondWindow->consumeMotionDown(); |
| 1705 | |
| 1706 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1707 | NotifyMotionArgs upMotionArgs = |
| 1708 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 1709 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1710 | mDispatcher->notifyMotion(&upMotionArgs); |
| 1711 | // The first window gets no events and the second gets up |
| 1712 | firstWindow->assertNoEvents(); |
| 1713 | secondWindow->consumeMotionUp(); |
| 1714 | } |
| 1715 | |
| 1716 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1717 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1718 | |
| 1719 | PointF touchPoint = {10, 10}; |
| 1720 | |
| 1721 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1722 | sp<FakeWindowHandle> firstWindow = |
| 1723 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 1724 | sp<FakeWindowHandle> secondWindow = |
| 1725 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1726 | |
| 1727 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1728 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1729 | |
| 1730 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1731 | NotifyMotionArgs downMotionArgs = |
| 1732 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1733 | ADISPLAY_ID_DEFAULT, {touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1734 | mDispatcher->notifyMotion(&downMotionArgs); |
| 1735 | // Only the first window should get the down event |
| 1736 | firstWindow->consumeMotionDown(); |
| 1737 | secondWindow->assertNoEvents(); |
| 1738 | |
| 1739 | // Send pointer down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1740 | NotifyMotionArgs pointerDownMotionArgs = |
| 1741 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1742 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1743 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1744 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1745 | mDispatcher->notifyMotion(&pointerDownMotionArgs); |
| 1746 | // Only the first window should get the pointer down event |
| 1747 | firstWindow->consumeMotionPointerDown(1); |
| 1748 | secondWindow->assertNoEvents(); |
| 1749 | |
| 1750 | // Transfer touch focus to the second window |
| 1751 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 1752 | // The first window gets cancel and the second gets down and pointer down |
| 1753 | firstWindow->consumeMotionCancel(); |
| 1754 | secondWindow->consumeMotionDown(); |
| 1755 | secondWindow->consumeMotionPointerDown(1); |
| 1756 | |
| 1757 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1758 | NotifyMotionArgs pointerUpMotionArgs = |
| 1759 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | |
| 1760 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1761 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1762 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1763 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 1764 | // The first window gets nothing and the second gets pointer up |
| 1765 | firstWindow->assertNoEvents(); |
| 1766 | secondWindow->consumeMotionPointerUp(1); |
| 1767 | |
| 1768 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1769 | NotifyMotionArgs upMotionArgs = |
| 1770 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 1771 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1772 | mDispatcher->notifyMotion(&upMotionArgs); |
| 1773 | // The first window gets nothing and the second gets up |
| 1774 | firstWindow->assertNoEvents(); |
| 1775 | secondWindow->consumeMotionUp(); |
| 1776 | } |
| 1777 | |
| 1778 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1779 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1780 | |
| 1781 | // Create a non touch modal window that supports split touch |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1782 | sp<FakeWindowHandle> firstWindow = |
| 1783 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1784 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1785 | firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 1786 | InputWindowInfo::Flag::SPLIT_TOUCH); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1787 | |
| 1788 | // Create a non touch modal window that supports split touch |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1789 | sp<FakeWindowHandle> secondWindow = |
| 1790 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1791 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1792 | secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 1793 | InputWindowInfo::Flag::SPLIT_TOUCH); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1794 | |
| 1795 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1796 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1797 | |
| 1798 | PointF pointInFirst = {300, 200}; |
| 1799 | PointF pointInSecond = {300, 600}; |
| 1800 | |
| 1801 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1802 | NotifyMotionArgs firstDownMotionArgs = |
| 1803 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1804 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1805 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 1806 | // Only the first window should get the down event |
| 1807 | firstWindow->consumeMotionDown(); |
| 1808 | secondWindow->assertNoEvents(); |
| 1809 | |
| 1810 | // Send down to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1811 | NotifyMotionArgs secondDownMotionArgs = |
| 1812 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1813 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1814 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1815 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1816 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 1817 | // The first window gets a move and the second a down |
| 1818 | firstWindow->consumeMotionMove(); |
| 1819 | secondWindow->consumeMotionDown(); |
| 1820 | |
| 1821 | // Transfer touch focus to the second window |
| 1822 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 1823 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 1824 | firstWindow->consumeMotionCancel(); |
| 1825 | secondWindow->consumeMotionPointerDown(1); |
| 1826 | |
| 1827 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1828 | NotifyMotionArgs pointerUpMotionArgs = |
| 1829 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | |
| 1830 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1831 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1832 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1833 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 1834 | // The first window gets nothing and the second gets pointer up |
| 1835 | firstWindow->assertNoEvents(); |
| 1836 | secondWindow->consumeMotionPointerUp(1); |
| 1837 | |
| 1838 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1839 | NotifyMotionArgs upMotionArgs = |
| 1840 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 1841 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1842 | mDispatcher->notifyMotion(&upMotionArgs); |
| 1843 | // The first window gets nothing and the second gets up |
| 1844 | firstWindow->assertNoEvents(); |
| 1845 | secondWindow->consumeMotionUp(); |
| 1846 | } |
| 1847 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1848 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1849 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1850 | sp<FakeWindowHandle> window = |
| 1851 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1852 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1853 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1854 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1855 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1856 | |
| 1857 | window->consumeFocusEvent(true); |
| 1858 | |
| 1859 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1860 | mDispatcher->notifyKey(&keyArgs); |
| 1861 | |
| 1862 | // Window should receive key down event. |
| 1863 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 1864 | } |
| 1865 | |
| 1866 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1867 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1868 | sp<FakeWindowHandle> window = |
| 1869 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1870 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1871 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1872 | |
| 1873 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1874 | mDispatcher->notifyKey(&keyArgs); |
| 1875 | mDispatcher->waitForIdle(); |
| 1876 | |
| 1877 | window->assertNoEvents(); |
| 1878 | } |
| 1879 | |
| 1880 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 1881 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1882 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1883 | sp<FakeWindowHandle> window = |
| 1884 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1885 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1886 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1887 | |
| 1888 | // Send key |
| 1889 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1890 | mDispatcher->notifyKey(&keyArgs); |
| 1891 | // Send motion |
| 1892 | NotifyMotionArgs motionArgs = |
| 1893 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1894 | ADISPLAY_ID_DEFAULT); |
| 1895 | mDispatcher->notifyMotion(&motionArgs); |
| 1896 | |
| 1897 | // Window should receive only the motion event |
| 1898 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1899 | window->assertNoEvents(); // Key event or focus event will not be received |
| 1900 | } |
| 1901 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 1902 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 1903 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1904 | |
| 1905 | // Create first non touch modal window that supports split touch |
| 1906 | sp<FakeWindowHandle> firstWindow = |
| 1907 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 1908 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
| 1909 | firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 1910 | InputWindowInfo::Flag::SPLIT_TOUCH); |
| 1911 | |
| 1912 | // Create second non touch modal window that supports split touch |
| 1913 | sp<FakeWindowHandle> secondWindow = |
| 1914 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
| 1915 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
| 1916 | secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 1917 | InputWindowInfo::Flag::SPLIT_TOUCH); |
| 1918 | |
| 1919 | // Add the windows to the dispatcher |
| 1920 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 1921 | |
| 1922 | PointF pointInFirst = {300, 200}; |
| 1923 | PointF pointInSecond = {300, 600}; |
| 1924 | |
| 1925 | // Send down to the first window |
| 1926 | NotifyMotionArgs firstDownMotionArgs = |
| 1927 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1928 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 1929 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 1930 | // Only the first window should get the down event |
| 1931 | firstWindow->consumeMotionDown(); |
| 1932 | secondWindow->assertNoEvents(); |
| 1933 | |
| 1934 | // Send down to the second window |
| 1935 | NotifyMotionArgs secondDownMotionArgs = |
| 1936 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1937 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1938 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1939 | {pointInFirst, pointInSecond}); |
| 1940 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 1941 | // The first window gets a move and the second a down |
| 1942 | firstWindow->consumeMotionMove(); |
| 1943 | secondWindow->consumeMotionDown(); |
| 1944 | |
| 1945 | // Send pointer cancel to the second window |
| 1946 | NotifyMotionArgs pointerUpMotionArgs = |
| 1947 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | |
| 1948 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1949 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1950 | {pointInFirst, pointInSecond}); |
| 1951 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 1952 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 1953 | // The first window gets move and the second gets cancel. |
| 1954 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 1955 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 1956 | |
| 1957 | // Send up event. |
| 1958 | NotifyMotionArgs upMotionArgs = |
| 1959 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 1960 | ADISPLAY_ID_DEFAULT); |
| 1961 | mDispatcher->notifyMotion(&upMotionArgs); |
| 1962 | // The first window gets up and the second gets nothing. |
| 1963 | firstWindow->consumeMotionUp(); |
| 1964 | secondWindow->assertNoEvents(); |
| 1965 | } |
| 1966 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame^] | 1967 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 1968 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1969 | |
| 1970 | sp<FakeWindowHandle> window = |
| 1971 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1972 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1973 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 1974 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 1975 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 1976 | |
| 1977 | window->sendTimeline(1 /*inputEventId*/, graphicsTimeline); |
| 1978 | window->assertNoEvents(); |
| 1979 | mDispatcher->waitForIdle(); |
| 1980 | } |
| 1981 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1982 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1983 | public: |
| 1984 | FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1985 | int32_t displayId, bool isGestureMonitor = false) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1986 | base::Result<std::unique_ptr<InputChannel>> channel = |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 1987 | dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1988 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1991 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 1992 | |
| 1993 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1994 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, |
| 1995 | expectedDisplayId, expectedFlags); |
| 1996 | } |
| 1997 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1998 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 1999 | |
| 2000 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 2001 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2002 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 2003 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 2004 | expectedDisplayId, expectedFlags); |
| 2005 | } |
| 2006 | |
| 2007 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 2008 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, |
| 2009 | expectedDisplayId, expectedFlags); |
| 2010 | } |
| 2011 | |
| 2012 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 2013 | |
| 2014 | private: |
| 2015 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2016 | }; |
| 2017 | |
| 2018 | // Tests for gesture monitors |
| 2019 | TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2020 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2021 | sp<FakeWindowHandle> window = |
| 2022 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2023 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2024 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2025 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 2026 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2027 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2028 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2029 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2030 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2031 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2032 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2036 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2037 | sp<FakeWindowHandle> window = |
| 2038 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2039 | |
| 2040 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2041 | window->setFocusable(true); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2042 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2043 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2044 | setFocusedWindow(window); |
| 2045 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2046 | window->consumeFocusEvent(true); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2047 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2048 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 2049 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2050 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2051 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 2052 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2053 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2054 | monitor.assertNoEvents(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2058 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2059 | sp<FakeWindowHandle> window = |
| 2060 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2061 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2062 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2063 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 2064 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2065 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2066 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2067 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2068 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2069 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2070 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2071 | |
| 2072 | window->releaseChannel(); |
| 2073 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2074 | mDispatcher->pilferPointers(monitor.getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2075 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2076 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2077 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2078 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2079 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2082 | TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) { |
| 2083 | FakeMonitorReceiver monitor = |
| 2084 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, |
| 2085 | true /*isGestureMonitor*/); |
| 2086 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2087 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2088 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
| 2089 | std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 2090 | ASSERT_TRUE(consumeSeq); |
| 2091 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 2092 | mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2093 | monitor.finishEvent(*consumeSeq); |
| 2094 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 2095 | mFakePolicy->assertNotifyMonitorResponsiveWasCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2096 | } |
| 2097 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2098 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2099 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2100 | sp<FakeWindowHandle> window = |
| 2101 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2102 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2103 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2104 | |
| 2105 | NotifyMotionArgs motionArgs = |
| 2106 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2107 | ADISPLAY_ID_DEFAULT); |
| 2108 | |
| 2109 | mDispatcher->notifyMotion(&motionArgs); |
| 2110 | // Window should receive motion down event. |
| 2111 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2112 | |
| 2113 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2114 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2115 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2116 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 2117 | motionArgs.pointerCoords[0].getX() - 10); |
| 2118 | |
| 2119 | mDispatcher->notifyMotion(&motionArgs); |
| 2120 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
| 2121 | 0 /*expectedFlags*/); |
| 2122 | } |
| 2123 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2124 | /** |
| 2125 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 2126 | * the device default right away. In the test scenario, we check both the default value, |
| 2127 | * and the action of enabling / disabling. |
| 2128 | */ |
| 2129 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2130 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2131 | sp<FakeWindowHandle> window = |
| 2132 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 2133 | |
| 2134 | // Set focused application. |
| 2135 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2136 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2137 | |
| 2138 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2139 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2140 | setFocusedWindow(window); |
| 2141 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2142 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 2143 | |
| 2144 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2145 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2146 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2147 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); |
| 2148 | |
| 2149 | SCOPED_TRACE("Disable touch mode"); |
| 2150 | mDispatcher->setInTouchMode(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2151 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2152 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2153 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2154 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); |
| 2155 | |
| 2156 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2157 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2158 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2159 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); |
| 2160 | |
| 2161 | SCOPED_TRACE("Enable touch mode again"); |
| 2162 | mDispatcher->setInTouchMode(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2163 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2164 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2165 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2166 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 2167 | |
| 2168 | window->assertNoEvents(); |
| 2169 | } |
| 2170 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2171 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2172 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2173 | sp<FakeWindowHandle> window = |
| 2174 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 2175 | |
| 2176 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2177 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2178 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2179 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2180 | setFocusedWindow(window); |
| 2181 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2182 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 2183 | |
| 2184 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 2185 | mDispatcher->notifyKey(&keyArgs); |
| 2186 | |
| 2187 | InputEvent* event = window->consume(); |
| 2188 | ASSERT_NE(event, nullptr); |
| 2189 | |
| 2190 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 2191 | ASSERT_NE(verified, nullptr); |
| 2192 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 2193 | |
| 2194 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 2195 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 2196 | ASSERT_EQ(keyArgs.source, verified->source); |
| 2197 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 2198 | |
| 2199 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 2200 | |
| 2201 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
| 2202 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2203 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
| 2204 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 2205 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 2206 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 2207 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 2208 | } |
| 2209 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2210 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2211 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2212 | sp<FakeWindowHandle> window = |
| 2213 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 2214 | |
| 2215 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2216 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2217 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2218 | |
| 2219 | NotifyMotionArgs motionArgs = |
| 2220 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2221 | ADISPLAY_ID_DEFAULT); |
| 2222 | mDispatcher->notifyMotion(&motionArgs); |
| 2223 | |
| 2224 | InputEvent* event = window->consume(); |
| 2225 | ASSERT_NE(event, nullptr); |
| 2226 | |
| 2227 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 2228 | ASSERT_NE(verified, nullptr); |
| 2229 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 2230 | |
| 2231 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 2232 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 2233 | EXPECT_EQ(motionArgs.source, verified->source); |
| 2234 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 2235 | |
| 2236 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 2237 | |
| 2238 | EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX); |
| 2239 | EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY); |
| 2240 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
| 2241 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
| 2242 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
| 2243 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 2244 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 2245 | } |
| 2246 | |
Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2247 | TEST_F(InputDispatcherTest, NonPointerMotionEvent_NotTransformed) { |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2248 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2249 | sp<FakeWindowHandle> window = |
| 2250 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 2251 | const std::string name = window->getName(); |
| 2252 | |
| 2253 | // Window gets transformed by offset values. |
| 2254 | window->setWindowOffset(500.0f, 500.0f); |
| 2255 | |
| 2256 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2257 | window->setFocusable(true); |
| 2258 | |
| 2259 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2260 | |
| 2261 | // First, we set focused window so that focusedWindowHandle is not null. |
| 2262 | setFocusedWindow(window); |
| 2263 | |
| 2264 | // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked. |
| 2265 | window->consumeFocusEvent(true); |
| 2266 | |
Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2267 | constexpr const std::array nonPointerSources = {AINPUT_SOURCE_TRACKBALL, |
| 2268 | AINPUT_SOURCE_MOUSE_RELATIVE, |
| 2269 | AINPUT_SOURCE_JOYSTICK}; |
| 2270 | for (const int source : nonPointerSources) { |
| 2271 | // Notify motion with a non-pointer source. |
| 2272 | NotifyMotionArgs motionArgs = |
| 2273 | generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, source, ADISPLAY_ID_DEFAULT); |
| 2274 | mDispatcher->notifyMotion(&motionArgs); |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2275 | |
Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2276 | InputEvent* event = window->consume(); |
| 2277 | ASSERT_NE(event, nullptr); |
| 2278 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 2279 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 2280 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2281 | |
Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2282 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 2283 | EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, motionEvent.getAction()); |
| 2284 | EXPECT_EQ(motionArgs.pointerCount, motionEvent.getPointerCount()); |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2285 | |
Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2286 | float expectedX = motionArgs.pointerCoords[0].getX(); |
| 2287 | float expectedY = motionArgs.pointerCoords[0].getY(); |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2288 | |
Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2289 | // Ensure the axis values from the final motion event are not transformed. |
| 2290 | EXPECT_EQ(expectedX, motionEvent.getX(0)) |
| 2291 | << "expected " << expectedX << " for x coord of " << name.c_str() << ", got " |
| 2292 | << motionEvent.getX(0); |
| 2293 | EXPECT_EQ(expectedY, motionEvent.getY(0)) |
| 2294 | << "expected " << expectedY << " for y coord of " << name.c_str() << ", got " |
| 2295 | << motionEvent.getY(0); |
| 2296 | // Ensure the raw and transformed axis values for the motion event are the same. |
| 2297 | EXPECT_EQ(motionEvent.getRawX(0), motionEvent.getX(0)) |
| 2298 | << "expected raw and transformed X-axis values to be equal"; |
| 2299 | EXPECT_EQ(motionEvent.getRawY(0), motionEvent.getY(0)) |
| 2300 | << "expected raw and transformed Y-axis values to be equal"; |
| 2301 | } |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2302 | } |
| 2303 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2304 | /** |
| 2305 | * Ensure that separate calls to sign the same data are generating the same key. |
| 2306 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 2307 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 2308 | * tests. |
| 2309 | */ |
| 2310 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 2311 | KeyEvent event = getTestKeyEvent(); |
| 2312 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 2313 | |
| 2314 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 2315 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 2316 | ASSERT_EQ(hmac1, hmac2); |
| 2317 | } |
| 2318 | |
| 2319 | /** |
| 2320 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 2321 | */ |
| 2322 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 2323 | KeyEvent event = getTestKeyEvent(); |
| 2324 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 2325 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 2326 | |
| 2327 | verifiedEvent.deviceId += 1; |
| 2328 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2329 | |
| 2330 | verifiedEvent.source += 1; |
| 2331 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2332 | |
| 2333 | verifiedEvent.eventTimeNanos += 1; |
| 2334 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2335 | |
| 2336 | verifiedEvent.displayId += 1; |
| 2337 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2338 | |
| 2339 | verifiedEvent.action += 1; |
| 2340 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2341 | |
| 2342 | verifiedEvent.downTimeNanos += 1; |
| 2343 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2344 | |
| 2345 | verifiedEvent.flags += 1; |
| 2346 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2347 | |
| 2348 | verifiedEvent.keyCode += 1; |
| 2349 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2350 | |
| 2351 | verifiedEvent.scanCode += 1; |
| 2352 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2353 | |
| 2354 | verifiedEvent.metaState += 1; |
| 2355 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2356 | |
| 2357 | verifiedEvent.repeatCount += 1; |
| 2358 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2359 | } |
| 2360 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2361 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 2362 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2363 | sp<FakeWindowHandle> windowTop = |
| 2364 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 2365 | sp<FakeWindowHandle> windowSecond = |
| 2366 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2367 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2368 | |
| 2369 | // Top window is also focusable but is not granted focus. |
| 2370 | windowTop->setFocusable(true); |
| 2371 | windowSecond->setFocusable(true); |
| 2372 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 2373 | setFocusedWindow(windowSecond); |
| 2374 | |
| 2375 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2376 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 2377 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2378 | |
| 2379 | // Focused window should receive event. |
| 2380 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 2381 | windowTop->assertNoEvents(); |
| 2382 | } |
| 2383 | |
| 2384 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 2385 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2386 | sp<FakeWindowHandle> window = |
| 2387 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 2388 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2389 | |
| 2390 | window->setFocusable(true); |
| 2391 | // Release channel for window is no longer valid. |
| 2392 | window->releaseChannel(); |
| 2393 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2394 | setFocusedWindow(window); |
| 2395 | |
| 2396 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2397 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 2398 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2399 | |
| 2400 | // window channel is invalid, so it should not receive any input event. |
| 2401 | window->assertNoEvents(); |
| 2402 | } |
| 2403 | |
| 2404 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 2405 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2406 | sp<FakeWindowHandle> window = |
| 2407 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 2408 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2409 | |
| 2410 | // Window is not focusable. |
| 2411 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2412 | setFocusedWindow(window); |
| 2413 | |
| 2414 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2415 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 2416 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2417 | |
| 2418 | // window is invalid, so it should not receive any input event. |
| 2419 | window->assertNoEvents(); |
| 2420 | } |
| 2421 | |
| 2422 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 2423 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2424 | sp<FakeWindowHandle> windowTop = |
| 2425 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 2426 | sp<FakeWindowHandle> windowSecond = |
| 2427 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2428 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2429 | |
| 2430 | windowTop->setFocusable(true); |
| 2431 | windowSecond->setFocusable(true); |
| 2432 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 2433 | setFocusedWindow(windowTop); |
| 2434 | windowTop->consumeFocusEvent(true); |
| 2435 | |
| 2436 | setFocusedWindow(windowSecond, windowTop); |
| 2437 | windowSecond->consumeFocusEvent(true); |
| 2438 | windowTop->consumeFocusEvent(false); |
| 2439 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2440 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 2441 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2442 | |
| 2443 | // Focused window should receive event. |
| 2444 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 2445 | } |
| 2446 | |
| 2447 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { |
| 2448 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2449 | sp<FakeWindowHandle> windowTop = |
| 2450 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 2451 | sp<FakeWindowHandle> windowSecond = |
| 2452 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2453 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2454 | |
| 2455 | windowTop->setFocusable(true); |
| 2456 | windowSecond->setFocusable(true); |
| 2457 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 2458 | setFocusedWindow(windowSecond, windowTop); |
| 2459 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2460 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 2461 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2462 | |
| 2463 | // Event should be dropped. |
| 2464 | windowTop->assertNoEvents(); |
| 2465 | windowSecond->assertNoEvents(); |
| 2466 | } |
| 2467 | |
| 2468 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 2469 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2470 | sp<FakeWindowHandle> window = |
| 2471 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 2472 | sp<FakeWindowHandle> previousFocusedWindow = |
| 2473 | new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow", |
| 2474 | ADISPLAY_ID_DEFAULT); |
| 2475 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2476 | |
| 2477 | window->setFocusable(true); |
| 2478 | previousFocusedWindow->setFocusable(true); |
| 2479 | window->setVisible(false); |
| 2480 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 2481 | setFocusedWindow(previousFocusedWindow); |
| 2482 | previousFocusedWindow->consumeFocusEvent(true); |
| 2483 | |
| 2484 | // Requesting focus on invisible window takes focus from currently focused window. |
| 2485 | setFocusedWindow(window); |
| 2486 | previousFocusedWindow->consumeFocusEvent(false); |
| 2487 | |
| 2488 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2489 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2490 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2491 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2492 | |
| 2493 | // Window does not get focus event or key down. |
| 2494 | window->assertNoEvents(); |
| 2495 | |
| 2496 | // Window becomes visible. |
| 2497 | window->setVisible(true); |
| 2498 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2499 | |
| 2500 | // Window receives focus event. |
| 2501 | window->consumeFocusEvent(true); |
| 2502 | // Focused window receives key down. |
| 2503 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2504 | } |
| 2505 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2506 | /** |
| 2507 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 2508 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 2509 | * of the 'slipperyEnterWindow'. |
| 2510 | * |
| 2511 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 2512 | * a way so that the touched location is no longer covered by the top window. |
| 2513 | * |
| 2514 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 2515 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 2516 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 2517 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 2518 | * with ACTION_DOWN). |
| 2519 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 2520 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 2521 | * |
| 2522 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 2523 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 2524 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 2525 | * |
| 2526 | * In this test, we ensure that the event received by the bottom window has |
| 2527 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 2528 | */ |
| 2529 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
| 2530 | constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1; |
| 2531 | constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1; |
| 2532 | |
| 2533 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2534 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2535 | |
| 2536 | sp<FakeWindowHandle> slipperyExitWindow = |
| 2537 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 2538 | slipperyExitWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 2539 | InputWindowInfo::Flag::SLIPPERY); |
| 2540 | // Make sure this one overlaps the bottom window |
| 2541 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 2542 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 2543 | // one. Windows with the same owner are not considered to be occluding each other. |
| 2544 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 2545 | |
| 2546 | sp<FakeWindowHandle> slipperyEnterWindow = |
| 2547 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2548 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 2549 | |
| 2550 | mDispatcher->setInputWindows( |
| 2551 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 2552 | |
| 2553 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
| 2554 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2555 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 2556 | mDispatcher->notifyMotion(&args); |
| 2557 | slipperyExitWindow->consumeMotionDown(); |
| 2558 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 2559 | mDispatcher->setInputWindows( |
| 2560 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 2561 | |
| 2562 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2563 | ADISPLAY_ID_DEFAULT, {{51, 51}}); |
| 2564 | mDispatcher->notifyMotion(&args); |
| 2565 | |
| 2566 | slipperyExitWindow->consumeMotionCancel(); |
| 2567 | |
| 2568 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 2569 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 2570 | } |
| 2571 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2572 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 2573 | protected: |
| 2574 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 2575 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 2576 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2577 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2578 | sp<FakeWindowHandle> mWindow; |
| 2579 | |
| 2580 | virtual void SetUp() override { |
| 2581 | mFakePolicy = new FakeInputDispatcherPolicy(); |
| 2582 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
| 2583 | mDispatcher = new InputDispatcher(mFakePolicy); |
| 2584 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 2585 | ASSERT_EQ(OK, mDispatcher->start()); |
| 2586 | |
| 2587 | setUpWindow(); |
| 2588 | } |
| 2589 | |
| 2590 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2591 | mApp = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2592 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2593 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2594 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2595 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2596 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2597 | mWindow->consumeFocusEvent(true); |
| 2598 | } |
| 2599 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2600 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2601 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2602 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2603 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
| 2604 | mDispatcher->notifyKey(&keyArgs); |
| 2605 | |
| 2606 | // Window should receive key down event. |
| 2607 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2608 | } |
| 2609 | |
| 2610 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 2611 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 2612 | InputEvent* repeatEvent = mWindow->consume(); |
| 2613 | ASSERT_NE(nullptr, repeatEvent); |
| 2614 | |
| 2615 | uint32_t eventType = repeatEvent->getType(); |
| 2616 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); |
| 2617 | |
| 2618 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 2619 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 2620 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 2621 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 2622 | } |
| 2623 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2624 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2625 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2626 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2627 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
| 2628 | mDispatcher->notifyKey(&keyArgs); |
| 2629 | |
| 2630 | // Window should receive key down event. |
| 2631 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 2632 | 0 /*expectedFlags*/); |
| 2633 | } |
| 2634 | }; |
| 2635 | |
| 2636 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2637 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 2638 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2639 | expectKeyRepeatOnce(repeatCount); |
| 2640 | } |
| 2641 | } |
| 2642 | |
| 2643 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
| 2644 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 2645 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2646 | expectKeyRepeatOnce(repeatCount); |
| 2647 | } |
| 2648 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 2649 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2650 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2651 | expectKeyRepeatOnce(repeatCount); |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2656 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2657 | expectKeyRepeatOnce(1 /*repeatCount*/); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2658 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 2659 | mWindow->assertNoEvents(); |
| 2660 | } |
| 2661 | |
| 2662 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
| 2663 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 2664 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 2665 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 2666 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 2667 | // Stale key up from device 1. |
| 2668 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 2669 | // Device 2 is still down, keep repeating |
| 2670 | expectKeyRepeatOnce(2 /*repeatCount*/); |
| 2671 | expectKeyRepeatOnce(3 /*repeatCount*/); |
| 2672 | // Device 2 key up |
| 2673 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 2674 | mWindow->assertNoEvents(); |
| 2675 | } |
| 2676 | |
| 2677 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
| 2678 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 2679 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 2680 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 2681 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 2682 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. |
| 2683 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 2684 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2685 | mWindow->assertNoEvents(); |
| 2686 | } |
| 2687 | |
| 2688 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2689 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2690 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2691 | InputEvent* repeatEvent = mWindow->consume(); |
| 2692 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 2693 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 2694 | IdGenerator::getSource(repeatEvent->getId())); |
| 2695 | } |
| 2696 | } |
| 2697 | |
| 2698 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2699 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2700 | |
| 2701 | std::unordered_set<int32_t> idSet; |
| 2702 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2703 | InputEvent* repeatEvent = mWindow->consume(); |
| 2704 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 2705 | int32_t id = repeatEvent->getId(); |
| 2706 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 2707 | idSet.insert(id); |
| 2708 | } |
| 2709 | } |
| 2710 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2711 | /* Test InputDispatcher for MultiDisplay */ |
| 2712 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 2713 | public: |
| 2714 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2715 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2716 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2717 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2718 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2719 | windowInPrimary = |
| 2720 | new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2721 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2722 | // Set focus window for primary display, but focused display would be second one. |
| 2723 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2724 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2725 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2726 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2727 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2728 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2729 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2730 | windowInSecondary = |
| 2731 | new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2732 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2733 | // Set focus display to second one. |
| 2734 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 2735 | // Set focus window for second display. |
| 2736 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2737 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2738 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2739 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2740 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2741 | } |
| 2742 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2743 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2744 | InputDispatcherTest::TearDown(); |
| 2745 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2746 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2747 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2748 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2749 | windowInSecondary.clear(); |
| 2750 | } |
| 2751 | |
| 2752 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2753 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2754 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2755 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2756 | sp<FakeWindowHandle> windowInSecondary; |
| 2757 | }; |
| 2758 | |
| 2759 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 2760 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2761 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2762 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 2763 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2764 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2765 | windowInSecondary->assertNoEvents(); |
| 2766 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2767 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2768 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2769 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 2770 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2771 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2772 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2773 | } |
| 2774 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2775 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2776 | // Test inject a key down with display id specified. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2777 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 2778 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2779 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2780 | windowInSecondary->assertNoEvents(); |
| 2781 | |
| 2782 | // Test inject a key down without display id specified. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2783 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 2784 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2785 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2786 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2787 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2788 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2789 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2790 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2791 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2792 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
| 2793 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2794 | |
| 2795 | // Test inject a key down, should timeout because of no target window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2796 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 2797 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2798 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2799 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2800 | windowInSecondary->assertNoEvents(); |
| 2801 | } |
| 2802 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2803 | // Test per-display input monitors for motion event. |
| 2804 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2805 | FakeMonitorReceiver monitorInPrimary = |
| 2806 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 2807 | FakeMonitorReceiver monitorInSecondary = |
| 2808 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2809 | |
| 2810 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2811 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2812 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 2813 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2814 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2815 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2816 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2817 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2818 | |
| 2819 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2820 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2821 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 2822 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2823 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2824 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2825 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2826 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2827 | |
| 2828 | // Test inject a non-pointer motion event. |
| 2829 | // If specific a display, it will dispatch to the focused window of particular display, |
| 2830 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2831 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2832 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 2833 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2834 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2835 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2836 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2837 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | // Test per-display input monitors for key event. |
| 2841 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2842 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2843 | FakeMonitorReceiver monitorInPrimary = |
| 2844 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 2845 | FakeMonitorReceiver monitorInSecondary = |
| 2846 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2847 | |
| 2848 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2849 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 2850 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2851 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2852 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2853 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2854 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2855 | } |
| 2856 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2857 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 2858 | sp<FakeWindowHandle> secondWindowInPrimary = |
| 2859 | new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
| 2860 | secondWindowInPrimary->setFocusable(true); |
| 2861 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 2862 | setFocusedWindow(secondWindowInPrimary); |
| 2863 | windowInPrimary->consumeFocusEvent(false); |
| 2864 | secondWindowInPrimary->consumeFocusEvent(true); |
| 2865 | |
| 2866 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2867 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 2868 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2869 | windowInPrimary->assertNoEvents(); |
| 2870 | windowInSecondary->assertNoEvents(); |
| 2871 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2872 | } |
| 2873 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2874 | class InputFilterTest : public InputDispatcherTest { |
| 2875 | protected: |
| 2876 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
| 2877 | |
| 2878 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) { |
| 2879 | NotifyMotionArgs motionArgs; |
| 2880 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2881 | motionArgs = |
| 2882 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2883 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2884 | motionArgs = |
| 2885 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2886 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2887 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2888 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 2889 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2890 | } else { |
| 2891 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 2892 | } |
| 2893 | } |
| 2894 | |
| 2895 | void testNotifyKey(bool expectToBeFiltered) { |
| 2896 | NotifyKeyArgs keyArgs; |
| 2897 | |
| 2898 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 2899 | mDispatcher->notifyKey(&keyArgs); |
| 2900 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 2901 | mDispatcher->notifyKey(&keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2902 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2903 | |
| 2904 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 2905 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2906 | } else { |
| 2907 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 2908 | } |
| 2909 | } |
| 2910 | }; |
| 2911 | |
| 2912 | // Test InputFilter for MotionEvent |
| 2913 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 2914 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 2915 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 2916 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 2917 | |
| 2918 | // Enable InputFilter |
| 2919 | mDispatcher->setInputFilterEnabled(true); |
| 2920 | // Test touch on both primary and second display, and check if both events are filtered. |
| 2921 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 2922 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 2923 | |
| 2924 | // Disable InputFilter |
| 2925 | mDispatcher->setInputFilterEnabled(false); |
| 2926 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 2927 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 2928 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 2929 | } |
| 2930 | |
| 2931 | // Test InputFilter for KeyEvent |
| 2932 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 2933 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 2934 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 2935 | |
| 2936 | // Enable InputFilter |
| 2937 | mDispatcher->setInputFilterEnabled(true); |
| 2938 | // Send a key event, and check if it is filtered. |
| 2939 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 2940 | |
| 2941 | // Disable InputFilter |
| 2942 | mDispatcher->setInputFilterEnabled(false); |
| 2943 | // Send a key event, and check if it isn't filtered. |
| 2944 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 2945 | } |
| 2946 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2947 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2948 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2949 | InputDispatcherTest::SetUp(); |
| 2950 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2951 | std::shared_ptr<FakeApplicationHandle> application = |
| 2952 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2953 | mUnfocusedWindow = |
| 2954 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2955 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
| 2956 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 2957 | // window. |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2958 | mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2959 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2960 | mFocusedWindow = |
| 2961 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2962 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2963 | mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2964 | |
| 2965 | // Set focused application. |
| 2966 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2967 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2968 | |
| 2969 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2970 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2971 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2972 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2973 | } |
| 2974 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2975 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2976 | InputDispatcherTest::TearDown(); |
| 2977 | |
| 2978 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2979 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | protected: |
| 2983 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2984 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 2985 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2986 | }; |
| 2987 | |
| 2988 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 2989 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 2990 | // the onPointerDownOutsideFocus callback. |
| 2991 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2992 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 2993 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2994 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2995 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 2996 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2997 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2998 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2999 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 3000 | } |
| 3001 | |
| 3002 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 3003 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 3004 | // onPointerDownOutsideFocus callback. |
| 3005 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3006 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3007 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3008 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3009 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3010 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3011 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3012 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 3016 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 3017 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3018 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 3019 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3020 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3021 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3022 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3023 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3024 | } |
| 3025 | |
| 3026 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 3027 | // DOWN on the window that already has focus. Ensure no window received the |
| 3028 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3029 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3030 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3031 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3032 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3033 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3034 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3035 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3036 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3037 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3038 | } |
| 3039 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3040 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 3041 | // windows that point to the same client token. |
| 3042 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 3043 | virtual void SetUp() override { |
| 3044 | InputDispatcherTest::SetUp(); |
| 3045 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3046 | std::shared_ptr<FakeApplicationHandle> application = |
| 3047 | std::make_shared<FakeApplicationHandle>(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3048 | mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1", |
| 3049 | ADISPLAY_ID_DEFAULT); |
| 3050 | // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window. |
| 3051 | // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows. |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3052 | mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 3053 | InputWindowInfo::Flag::SPLIT_TOUCH); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3054 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 3055 | |
| 3056 | mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2", |
| 3057 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3058 | mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 3059 | InputWindowInfo::Flag::SPLIT_TOUCH); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3060 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 3061 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3062 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3063 | } |
| 3064 | |
| 3065 | protected: |
| 3066 | sp<FakeWindowHandle> mWindow1; |
| 3067 | sp<FakeWindowHandle> mWindow2; |
| 3068 | |
| 3069 | // Helper function to convert the point from screen coordinates into the window's space |
| 3070 | static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3071 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 3072 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 3076 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3077 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3078 | InputEvent* event = window->consume(); |
| 3079 | |
| 3080 | ASSERT_NE(nullptr, event) << name.c_str() |
| 3081 | << ": consumer should have returned non-NULL event."; |
| 3082 | |
| 3083 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 3084 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 3085 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 3086 | |
| 3087 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 3088 | EXPECT_EQ(expectedAction, motionEvent.getAction()); |
| 3089 | |
| 3090 | for (size_t i = 0; i < points.size(); i++) { |
| 3091 | float expectedX = points[i].x; |
| 3092 | float expectedY = points[i].y; |
| 3093 | |
| 3094 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 3095 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 3096 | << ", got " << motionEvent.getX(i); |
| 3097 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 3098 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 3099 | << ", got " << motionEvent.getY(i); |
| 3100 | } |
| 3101 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3102 | |
| 3103 | void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints, |
| 3104 | std::vector<PointF> expectedPoints) { |
| 3105 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 3106 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 3107 | mDispatcher->notifyMotion(&motionArgs); |
| 3108 | |
| 3109 | // Always consume from window1 since it's the window that has the InputReceiver |
| 3110 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 3111 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3112 | }; |
| 3113 | |
| 3114 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 3115 | // Touch Window 1 |
| 3116 | PointF touchedPoint = {10, 10}; |
| 3117 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3118 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3119 | |
| 3120 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3121 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3122 | |
| 3123 | // Touch Window 2 |
| 3124 | touchedPoint = {150, 150}; |
| 3125 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3126 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3127 | } |
| 3128 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3129 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 3130 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3131 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 3132 | |
| 3133 | // Touch Window 1 |
| 3134 | PointF touchedPoint = {10, 10}; |
| 3135 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3136 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3137 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3138 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3139 | |
| 3140 | // Touch Window 2 |
| 3141 | touchedPoint = {150, 150}; |
| 3142 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3143 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 3144 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3145 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3146 | // Update the transform so rotation is set |
| 3147 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 3148 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 3149 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3150 | } |
| 3151 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3152 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3153 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 3154 | |
| 3155 | // Touch Window 1 |
| 3156 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 3157 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3158 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3159 | |
| 3160 | // Touch Window 2 |
| 3161 | int32_t actionPointerDown = |
| 3162 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3163 | touchedPoints.push_back(PointF{150, 150}); |
| 3164 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 3165 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3166 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3167 | // Release Window 2 |
| 3168 | int32_t actionPointerUp = |
| 3169 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 3170 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); |
| 3171 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3172 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3173 | // Update the transform so rotation is set for Window 2 |
| 3174 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 3175 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 3176 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3179 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3180 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 3181 | |
| 3182 | // Touch Window 1 |
| 3183 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 3184 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3185 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3186 | |
| 3187 | // Touch Window 2 |
| 3188 | int32_t actionPointerDown = |
| 3189 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3190 | touchedPoints.push_back(PointF{150, 150}); |
| 3191 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3192 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3193 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3194 | |
| 3195 | // Move both windows |
| 3196 | touchedPoints = {{20, 20}, {175, 175}}; |
| 3197 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 3198 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 3199 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3200 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3201 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3202 | // Release Window 2 |
| 3203 | int32_t actionPointerUp = |
| 3204 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 3205 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); |
| 3206 | expectedPoints.pop_back(); |
| 3207 | |
| 3208 | // Touch Window 2 |
| 3209 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 3210 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 3211 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
| 3212 | |
| 3213 | // Move both windows |
| 3214 | touchedPoints = {{20, 20}, {175, 175}}; |
| 3215 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 3216 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 3217 | |
| 3218 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
| 3221 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 3222 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 3223 | |
| 3224 | // Touch Window 1 |
| 3225 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 3226 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3227 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3228 | |
| 3229 | // Touch Window 2 |
| 3230 | int32_t actionPointerDown = |
| 3231 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3232 | touchedPoints.push_back(PointF{150, 150}); |
| 3233 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3234 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3235 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3236 | |
| 3237 | // Move both windows |
| 3238 | touchedPoints = {{20, 20}, {175, 175}}; |
| 3239 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 3240 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 3241 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3242 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3245 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 3246 | virtual void SetUp() override { |
| 3247 | InputDispatcherTest::SetUp(); |
| 3248 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3249 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3250 | mApplication->setDispatchingTimeout(20ms); |
| 3251 | mWindow = |
| 3252 | new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 3253 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 3254 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3255 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3256 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 3257 | // window. |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3258 | mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3259 | |
| 3260 | // Set focused application. |
| 3261 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 3262 | |
| 3263 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3264 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3265 | mWindow->consumeFocusEvent(true); |
| 3266 | } |
| 3267 | |
| 3268 | virtual void TearDown() override { |
| 3269 | InputDispatcherTest::TearDown(); |
| 3270 | mWindow.clear(); |
| 3271 | } |
| 3272 | |
| 3273 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3274 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3275 | sp<FakeWindowHandle> mWindow; |
| 3276 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 3277 | |
| 3278 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3279 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3280 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3281 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3282 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3283 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3284 | WINDOW_LOCATION)); |
| 3285 | } |
| 3286 | }; |
| 3287 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3288 | // Send a tap and respond, which should not cause an ANR. |
| 3289 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 3290 | tapOnWindow(); |
| 3291 | mWindow->consumeMotionDown(); |
| 3292 | mWindow->consumeMotionUp(); |
| 3293 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3294 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3295 | } |
| 3296 | |
| 3297 | // Send a regular key and respond, which should not cause an ANR. |
| 3298 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3299 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3300 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 3301 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3302 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3303 | } |
| 3304 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3305 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 3306 | mWindow->setFocusable(false); |
| 3307 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3308 | mWindow->consumeFocusEvent(false); |
| 3309 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3310 | InputEventInjectionResult result = |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3311 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3312 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 3313 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3314 | // Key will not go to window because we have no focused window. |
| 3315 | // The 'no focused window' ANR timer should start instead. |
| 3316 | |
| 3317 | // Now, the focused application goes away. |
| 3318 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 3319 | // The key should get dropped and there should be no ANR. |
| 3320 | |
| 3321 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3322 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3323 | } |
| 3324 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3325 | // 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] | 3326 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 3327 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3328 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3329 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3330 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3331 | WINDOW_LOCATION)); |
| 3332 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3333 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 3334 | ASSERT_TRUE(sequenceNum); |
| 3335 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3336 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3337 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3338 | mWindow->finishEvent(*sequenceNum); |
| 3339 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 3340 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3341 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3342 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3343 | } |
| 3344 | |
| 3345 | // Send a key to the app and have the app not respond right away. |
| 3346 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 3347 | // Inject a key, and don't respond - expect that ANR is called. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3348 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3349 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 3350 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3351 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3352 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3353 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3354 | } |
| 3355 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3356 | // We have a focused application, but no focused window |
| 3357 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3358 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3359 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3360 | mWindow->consumeFocusEvent(false); |
| 3361 | |
| 3362 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3363 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3364 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3365 | WINDOW_LOCATION)); |
| 3366 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 3367 | mDispatcher->waitForIdle(); |
| 3368 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3369 | |
| 3370 | // Once a focused event arrives, we get an ANR for this application |
| 3371 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 3372 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3373 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3374 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3375 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 3376 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3377 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3378 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3379 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3380 | } |
| 3381 | |
| 3382 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3383 | // Make sure that we don't notify policy twice about the same ANR. |
| 3384 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3385 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3386 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3387 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3388 | |
| 3389 | // Once a focused event arrives, we get an ANR for this application |
| 3390 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 3391 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3392 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3393 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3394 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 3395 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3396 | const std::chrono::duration appTimeout = |
| 3397 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3398 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3399 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3400 | std::this_thread::sleep_for(appTimeout); |
| 3401 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 3402 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3403 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3404 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 3405 | // '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] | 3406 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3407 | } |
| 3408 | |
| 3409 | // We have a focused application, but no focused window |
| 3410 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3411 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3412 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3413 | mWindow->consumeFocusEvent(false); |
| 3414 | |
| 3415 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3416 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3417 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3418 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 3419 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3420 | |
| 3421 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3422 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3423 | |
| 3424 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3425 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3426 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3427 | mWindow->assertNoEvents(); |
| 3428 | } |
| 3429 | |
| 3430 | /** |
| 3431 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 3432 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 3433 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 3434 | * the ANR mechanism should still work. |
| 3435 | * |
| 3436 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 3437 | * DOWN event, while not responding on the second one. |
| 3438 | */ |
| 3439 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 3440 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 3441 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3442 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 3443 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 3444 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3445 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3446 | |
| 3447 | // Now send ACTION_UP, with identical timestamp |
| 3448 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3449 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 3450 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 3451 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3452 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3453 | |
| 3454 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 3455 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3456 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3457 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3458 | } |
| 3459 | |
| 3460 | // If an app is not responding to a key event, gesture monitors should continue to receive |
| 3461 | // new motion events |
| 3462 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) { |
| 3463 | FakeMonitorReceiver monitor = |
| 3464 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, |
| 3465 | true /*isGestureMonitor*/); |
| 3466 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3467 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3468 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3469 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3470 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3471 | |
| 3472 | // Stuck on the ACTION_UP |
| 3473 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3474 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3475 | |
| 3476 | // New tap will go to the gesture monitor, but not to the window |
| 3477 | tapOnWindow(); |
| 3478 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3479 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3480 | |
| 3481 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 3482 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3483 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3484 | mWindow->assertNoEvents(); |
| 3485 | monitor.assertNoEvents(); |
| 3486 | } |
| 3487 | |
| 3488 | // If an app is not responding to a motion event, gesture monitors should continue to receive |
| 3489 | // new motion events |
| 3490 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) { |
| 3491 | FakeMonitorReceiver monitor = |
| 3492 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, |
| 3493 | true /*isGestureMonitor*/); |
| 3494 | |
| 3495 | tapOnWindow(); |
| 3496 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3497 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3498 | |
| 3499 | mWindow->consumeMotionDown(); |
| 3500 | // Stuck on the ACTION_UP |
| 3501 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3502 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3503 | |
| 3504 | // New tap will go to the gesture monitor, but not to the window |
| 3505 | tapOnWindow(); |
| 3506 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3507 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3508 | |
| 3509 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 3510 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3511 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3512 | mWindow->assertNoEvents(); |
| 3513 | monitor.assertNoEvents(); |
| 3514 | } |
| 3515 | |
| 3516 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 3517 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 3518 | // get an ANR again. |
| 3519 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 3520 | // 2. consume all pending events (= queue becomes healthy again) |
| 3521 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 3522 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 3523 | tapOnWindow(); |
| 3524 | |
| 3525 | mWindow->consumeMotionDown(); |
| 3526 | // Block on ACTION_UP |
| 3527 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3528 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3529 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 3530 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3531 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3532 | mWindow->assertNoEvents(); |
| 3533 | |
| 3534 | tapOnWindow(); |
| 3535 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3536 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3537 | mWindow->consumeMotionUp(); |
| 3538 | |
| 3539 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3540 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3541 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3542 | mWindow->assertNoEvents(); |
| 3543 | } |
| 3544 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3545 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 3546 | // it. |
| 3547 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3548 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3549 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3550 | WINDOW_LOCATION)); |
| 3551 | |
| 3552 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3553 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3554 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3555 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 3556 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3557 | // 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] | 3558 | mWindow->consumeMotionDown(); |
| 3559 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 3560 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 3561 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3562 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3563 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3564 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3565 | } |
| 3566 | |
| 3567 | /** |
| 3568 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 3569 | * not to to the focused window until the motion is processed. |
| 3570 | * |
| 3571 | * Warning!!! |
| 3572 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 3573 | * and the injection timeout that we specify when injecting the key. |
| 3574 | * We must have the injection timeout (10ms) be smaller than |
| 3575 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 3576 | * |
| 3577 | * If that value changes, this test should also change. |
| 3578 | */ |
| 3579 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 3580 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 3581 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3582 | |
| 3583 | tapOnWindow(); |
| 3584 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 3585 | ASSERT_TRUE(downSequenceNum); |
| 3586 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 3587 | ASSERT_TRUE(upSequenceNum); |
| 3588 | // Don't finish the events yet, and send a key |
| 3589 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 3590 | // window even if motions are still being processed. But because the injection timeout is short, |
| 3591 | // we will receive INJECTION_TIMED_OUT as the result. |
| 3592 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3593 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3594 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3595 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 3596 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3597 | // Key will not be sent to the window, yet, because the window is still processing events |
| 3598 | // and the key remains pending, waiting for the touch events to be processed |
| 3599 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 3600 | ASSERT_FALSE(keySequenceNum); |
| 3601 | |
| 3602 | std::this_thread::sleep_for(500ms); |
| 3603 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 3604 | // to the focused window, even though we have not yet finished the motion event |
| 3605 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3606 | mWindow->finishEvent(*downSequenceNum); |
| 3607 | mWindow->finishEvent(*upSequenceNum); |
| 3608 | } |
| 3609 | |
| 3610 | /** |
| 3611 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 3612 | * not go to the focused window until the motion is processed. |
| 3613 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 3614 | * focused window right away. |
| 3615 | */ |
| 3616 | TEST_F(InputDispatcherSingleWindowAnr, |
| 3617 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 3618 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 3619 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3620 | |
| 3621 | tapOnWindow(); |
| 3622 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 3623 | ASSERT_TRUE(downSequenceNum); |
| 3624 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 3625 | ASSERT_TRUE(upSequenceNum); |
| 3626 | // Don't finish the events yet, and send a key |
| 3627 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3628 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3629 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3630 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3631 | // At this point, key is still pending, and should not be sent to the application yet. |
| 3632 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 3633 | ASSERT_FALSE(keySequenceNum); |
| 3634 | |
| 3635 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 3636 | tapOnWindow(); |
| 3637 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 3638 | // the other events yet. We can finish events in any order. |
| 3639 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 3640 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 3641 | mWindow->consumeMotionDown(); |
| 3642 | mWindow->consumeMotionUp(); |
| 3643 | mWindow->assertNoEvents(); |
| 3644 | } |
| 3645 | |
| 3646 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 3647 | virtual void SetUp() override { |
| 3648 | InputDispatcherTest::SetUp(); |
| 3649 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3650 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3651 | mApplication->setDispatchingTimeout(10ms); |
| 3652 | mUnfocusedWindow = |
| 3653 | new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT); |
| 3654 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
| 3655 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 3656 | // window. |
| 3657 | // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3658 | mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 3659 | InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH | |
| 3660 | InputWindowInfo::Flag::SPLIT_TOUCH); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3661 | |
| 3662 | mFocusedWindow = |
| 3663 | new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 3664 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3665 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3666 | mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 3667 | InputWindowInfo::Flag::SPLIT_TOUCH); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3668 | |
| 3669 | // Set focused application. |
| 3670 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3671 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3672 | |
| 3673 | // Expect one focus window exist in display. |
| 3674 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3675 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3676 | mFocusedWindow->consumeFocusEvent(true); |
| 3677 | } |
| 3678 | |
| 3679 | virtual void TearDown() override { |
| 3680 | InputDispatcherTest::TearDown(); |
| 3681 | |
| 3682 | mUnfocusedWindow.clear(); |
| 3683 | mFocusedWindow.clear(); |
| 3684 | } |
| 3685 | |
| 3686 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3687 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3688 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 3689 | sp<FakeWindowHandle> mFocusedWindow; |
| 3690 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 3691 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 3692 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 3693 | |
| 3694 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 3695 | |
| 3696 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 3697 | |
| 3698 | private: |
| 3699 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3700 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3701 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3702 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3703 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3704 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3705 | location)); |
| 3706 | } |
| 3707 | }; |
| 3708 | |
| 3709 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 3710 | // should be ANR'd first. |
| 3711 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3712 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3713 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3714 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3715 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3716 | mFocusedWindow->consumeMotionDown(); |
| 3717 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 3718 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 3719 | // We consumed all events, so no ANR |
| 3720 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3721 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3722 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3723 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3724 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3725 | FOCUSED_WINDOW_LOCATION)); |
| 3726 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 3727 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3728 | |
| 3729 | const std::chrono::duration timeout = |
| 3730 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3731 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3732 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 3733 | // sequence to make it consistent |
| 3734 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3735 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3736 | mFocusedWindow->consumeMotionDown(); |
| 3737 | // This cancel is generated because the connection was unresponsive |
| 3738 | mFocusedWindow->consumeMotionCancel(); |
| 3739 | mFocusedWindow->assertNoEvents(); |
| 3740 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3741 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3742 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3743 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3744 | } |
| 3745 | |
| 3746 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 3747 | // it doesn't matter which order they should have ANR. |
| 3748 | // But we should receive ANR for both. |
| 3749 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 3750 | // Set the timeout for unfocused window to match the focused window |
| 3751 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 3752 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 3753 | |
| 3754 | tapOnFocusedWindow(); |
| 3755 | // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3756 | sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms); |
| 3757 | sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3758 | |
| 3759 | // 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] | 3760 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 3761 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 3762 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 3763 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3764 | |
| 3765 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3766 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3767 | |
| 3768 | mFocusedWindow->consumeMotionDown(); |
| 3769 | mFocusedWindow->consumeMotionUp(); |
| 3770 | mUnfocusedWindow->consumeMotionOutside(); |
| 3771 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3772 | sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken(); |
| 3773 | sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3774 | |
| 3775 | // Both applications should be marked as responsive, in any order |
| 3776 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 3777 | mFocusedWindow->getToken() == responsiveToken2); |
| 3778 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 3779 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 3780 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3781 | } |
| 3782 | |
| 3783 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 3784 | // We should also log an error to account for the dropped event (not tested here). |
| 3785 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 3786 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 3787 | tapOnFocusedWindow(); |
| 3788 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 3789 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 3790 | // Receive the events, but don't respond |
| 3791 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 3792 | ASSERT_TRUE(downEventSequenceNum); |
| 3793 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 3794 | ASSERT_TRUE(upEventSequenceNum); |
| 3795 | const std::chrono::duration timeout = |
| 3796 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3797 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3798 | |
| 3799 | // Tap once again |
| 3800 | // 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] | 3801 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3802 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3803 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3804 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3805 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3806 | FOCUSED_WINDOW_LOCATION)); |
| 3807 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 3808 | // valid touch target |
| 3809 | mUnfocusedWindow->assertNoEvents(); |
| 3810 | |
| 3811 | // Consume the first tap |
| 3812 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 3813 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 3814 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3815 | // The second tap did not go to the focused window |
| 3816 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3817 | // Since all events are finished, connection should be deemed healthy again |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3818 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3819 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3820 | } |
| 3821 | |
| 3822 | // If you tap outside of all windows, there will not be ANR |
| 3823 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3824 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3825 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3826 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 3827 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3828 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3829 | } |
| 3830 | |
| 3831 | // Since the focused window is paused, tapping on it should not produce any events |
| 3832 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 3833 | mFocusedWindow->setPaused(true); |
| 3834 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 3835 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3836 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3837 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3838 | FOCUSED_WINDOW_LOCATION)); |
| 3839 | |
| 3840 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 3841 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3842 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 3843 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3844 | |
| 3845 | mFocusedWindow->assertNoEvents(); |
| 3846 | mUnfocusedWindow->assertNoEvents(); |
| 3847 | } |
| 3848 | |
| 3849 | /** |
| 3850 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 3851 | * not to to the focused window until the motion is processed. |
| 3852 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 3853 | * |
| 3854 | * Warning!!! |
| 3855 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 3856 | * and the injection timeout that we specify when injecting the key. |
| 3857 | * We must have the injection timeout (10ms) be smaller than |
| 3858 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 3859 | * |
| 3860 | * If that value changes, this test should also change. |
| 3861 | */ |
| 3862 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 3863 | // Set a long ANR timeout to prevent it from triggering |
| 3864 | mFocusedWindow->setDispatchingTimeout(2s); |
| 3865 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 3866 | |
| 3867 | tapOnUnfocusedWindow(); |
| 3868 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 3869 | ASSERT_TRUE(downSequenceNum); |
| 3870 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 3871 | ASSERT_TRUE(upSequenceNum); |
| 3872 | // Don't finish the events yet, and send a key |
| 3873 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 3874 | // window even if motions are still being processed. |
| 3875 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3876 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3877 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3878 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 3879 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3880 | // Key will not be sent to the window, yet, because the window is still processing events |
| 3881 | // and the key remains pending, waiting for the touch events to be processed |
| 3882 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 3883 | ASSERT_FALSE(keySequenceNum); |
| 3884 | |
| 3885 | // 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] | 3886 | mFocusedWindow->setFocusable(false); |
| 3887 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3888 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3889 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3890 | |
| 3891 | // Focus events should precede the key events |
| 3892 | mUnfocusedWindow->consumeFocusEvent(true); |
| 3893 | mFocusedWindow->consumeFocusEvent(false); |
| 3894 | |
| 3895 | // Finish the tap events, which should unblock dispatcher |
| 3896 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 3897 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 3898 | |
| 3899 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 3900 | // can finally go to the newly focused "mUnfocusedWindow". |
| 3901 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3902 | mFocusedWindow->assertNoEvents(); |
| 3903 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3904 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3905 | } |
| 3906 | |
| 3907 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 3908 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 3909 | // The other window should not be affected by that. |
| 3910 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 3911 | // Touch Window 1 |
| 3912 | NotifyMotionArgs motionArgs = |
| 3913 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3914 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); |
| 3915 | mDispatcher->notifyMotion(&motionArgs); |
| 3916 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 3917 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 3918 | |
| 3919 | // Touch Window 2 |
| 3920 | int32_t actionPointerDown = |
| 3921 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 3922 | |
| 3923 | motionArgs = |
| 3924 | generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3925 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); |
| 3926 | mDispatcher->notifyMotion(&motionArgs); |
| 3927 | |
| 3928 | const std::chrono::duration timeout = |
| 3929 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3930 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3931 | |
| 3932 | mUnfocusedWindow->consumeMotionDown(); |
| 3933 | mFocusedWindow->consumeMotionDown(); |
| 3934 | // Focused window may or may not receive ACTION_MOVE |
| 3935 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 3936 | InputEvent* event; |
| 3937 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 3938 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 3939 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 3940 | ASSERT_NE(nullptr, event); |
| 3941 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 3942 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 3943 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 3944 | mFocusedWindow->consumeMotionCancel(); |
| 3945 | } else { |
| 3946 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 3947 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3948 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3949 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3950 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3951 | mUnfocusedWindow->assertNoEvents(); |
| 3952 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3953 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3954 | } |
| 3955 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 3956 | /** |
| 3957 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 3958 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 3959 | * |
| 3960 | * If the user touches another application during this time, the key should be dropped. |
| 3961 | * Next, if a new focused window comes in, without toggling the focused application, |
| 3962 | * then no ANR should occur. |
| 3963 | * |
| 3964 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 3965 | * but in some cases the policy may not update the focused application. |
| 3966 | */ |
| 3967 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 3968 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 3969 | std::make_shared<FakeApplicationHandle>(); |
| 3970 | focusedApplication->setDispatchingTimeout(60ms); |
| 3971 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 3972 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 3973 | mFocusedWindow->setFocusable(false); |
| 3974 | |
| 3975 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 3976 | mFocusedWindow->consumeFocusEvent(false); |
| 3977 | |
| 3978 | // Send a key. The ANR timer should start because there is no focused window. |
| 3979 | // 'focusedApplication' will get blamed if this timer completes. |
| 3980 | // 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] | 3981 | InputEventInjectionResult result = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 3982 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3983 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 3984 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 3985 | |
| 3986 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 3987 | // then the injected touches won't cause the focused event to get dropped. |
| 3988 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 3989 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 3990 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 3991 | // For this test, it means that the key would get delivered to the window once it becomes |
| 3992 | // focused. |
| 3993 | std::this_thread::sleep_for(10ms); |
| 3994 | |
| 3995 | // Touch unfocused window. This should force the pending key to get dropped. |
| 3996 | NotifyMotionArgs motionArgs = |
| 3997 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3998 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); |
| 3999 | mDispatcher->notifyMotion(&motionArgs); |
| 4000 | |
| 4001 | // We do not consume the motion right away, because that would require dispatcher to first |
| 4002 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 4003 | // Set the focused window first. |
| 4004 | mFocusedWindow->setFocusable(true); |
| 4005 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 4006 | setFocusedWindow(mFocusedWindow); |
| 4007 | mFocusedWindow->consumeFocusEvent(true); |
| 4008 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 4009 | // to another application. This could be a bug / behaviour in the policy. |
| 4010 | |
| 4011 | mUnfocusedWindow->consumeMotionDown(); |
| 4012 | |
| 4013 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4014 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 4015 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 4016 | } |
| 4017 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4018 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 4019 | // that has feature NO_INPUT_CHANNEL. |
| 4020 | // Layout: |
| 4021 | // Top (closest to user) |
| 4022 | // mNoInputWindow (above all windows) |
| 4023 | // mBottomWindow |
| 4024 | // Bottom (furthest from user) |
| 4025 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 4026 | virtual void SetUp() override { |
| 4027 | InputDispatcherTest::SetUp(); |
| 4028 | |
| 4029 | mApplication = std::make_shared<FakeApplicationHandle>(); |
| 4030 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, |
| 4031 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
| 4032 | std::make_optional<sp<IBinder>>(nullptr) /*token*/); |
| 4033 | |
| 4034 | mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4035 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4036 | // It's perfectly valid for this window to not have an associated input channel |
| 4037 | |
| 4038 | mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window", |
| 4039 | ADISPLAY_ID_DEFAULT); |
| 4040 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4041 | |
| 4042 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 4043 | } |
| 4044 | |
| 4045 | protected: |
| 4046 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 4047 | sp<FakeWindowHandle> mNoInputWindow; |
| 4048 | sp<FakeWindowHandle> mBottomWindow; |
| 4049 | }; |
| 4050 | |
| 4051 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 4052 | PointF touchedPoint = {10, 10}; |
| 4053 | |
| 4054 | NotifyMotionArgs motionArgs = |
| 4055 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4056 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 4057 | mDispatcher->notifyMotion(&motionArgs); |
| 4058 | |
| 4059 | mNoInputWindow->assertNoEvents(); |
| 4060 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 4061 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 4062 | // and therefore should prevent mBottomWindow from receiving touches |
| 4063 | mBottomWindow->assertNoEvents(); |
| 4064 | } |
| 4065 | |
| 4066 | /** |
| 4067 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 4068 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 4069 | */ |
| 4070 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 4071 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
| 4072 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, |
| 4073 | "Window with input channel and NO_INPUT_CHANNEL", |
| 4074 | ADISPLAY_ID_DEFAULT); |
| 4075 | |
| 4076 | mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4077 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4078 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 4079 | |
| 4080 | PointF touchedPoint = {10, 10}; |
| 4081 | |
| 4082 | NotifyMotionArgs motionArgs = |
| 4083 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4084 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 4085 | mDispatcher->notifyMotion(&motionArgs); |
| 4086 | |
| 4087 | mNoInputWindow->assertNoEvents(); |
| 4088 | mBottomWindow->assertNoEvents(); |
| 4089 | } |
| 4090 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4091 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 4092 | protected: |
| 4093 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 4094 | sp<FakeWindowHandle> mWindow; |
| 4095 | sp<FakeWindowHandle> mMirror; |
| 4096 | |
| 4097 | virtual void SetUp() override { |
| 4098 | InputDispatcherTest::SetUp(); |
| 4099 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 4100 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 4101 | mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT, |
| 4102 | mWindow->getToken()); |
| 4103 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 4104 | mWindow->setFocusable(true); |
| 4105 | mMirror->setFocusable(true); |
| 4106 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 4107 | } |
| 4108 | }; |
| 4109 | |
| 4110 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 4111 | // Request focus on a mirrored window |
| 4112 | setFocusedWindow(mMirror); |
| 4113 | |
| 4114 | // window gets focused |
| 4115 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4116 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4117 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4118 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 4119 | } |
| 4120 | |
| 4121 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 4122 | // focusable. |
| 4123 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 4124 | setFocusedWindow(mMirror); |
| 4125 | |
| 4126 | // window gets focused |
| 4127 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4128 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4129 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4130 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4131 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 4132 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4133 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 4134 | |
| 4135 | mMirror->setFocusable(false); |
| 4136 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 4137 | |
| 4138 | // window loses focus since one of the windows associated with the token in not focusable |
| 4139 | mWindow->consumeFocusEvent(false); |
| 4140 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4141 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 4142 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4143 | mWindow->assertNoEvents(); |
| 4144 | } |
| 4145 | |
| 4146 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 4147 | // invisible. |
| 4148 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 4149 | setFocusedWindow(mMirror); |
| 4150 | |
| 4151 | // window gets focused |
| 4152 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4153 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4154 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4155 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4156 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 4157 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4158 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 4159 | |
| 4160 | mMirror->setVisible(false); |
| 4161 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 4162 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4163 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4164 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4165 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4166 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 4167 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4168 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 4169 | |
| 4170 | mWindow->setVisible(false); |
| 4171 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 4172 | |
| 4173 | // window loses focus only after all windows associated with the token become invisible. |
| 4174 | mWindow->consumeFocusEvent(false); |
| 4175 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4176 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 4177 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4178 | mWindow->assertNoEvents(); |
| 4179 | } |
| 4180 | |
| 4181 | // A focused & mirrored window remains focused until both windows are removed. |
| 4182 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 4183 | setFocusedWindow(mMirror); |
| 4184 | |
| 4185 | // window gets focused |
| 4186 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4187 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4188 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4189 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4190 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 4191 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4192 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 4193 | |
| 4194 | // single window is removed but the window token remains focused |
| 4195 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 4196 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4197 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4198 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4199 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4200 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 4201 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4202 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 4203 | |
| 4204 | // Both windows are removed |
| 4205 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 4206 | mWindow->consumeFocusEvent(false); |
| 4207 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4208 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 4209 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4210 | mWindow->assertNoEvents(); |
| 4211 | } |
| 4212 | |
| 4213 | // Focus request can be pending until one window becomes visible. |
| 4214 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 4215 | // Request focus on an invisible mirror. |
| 4216 | mWindow->setVisible(false); |
| 4217 | mMirror->setVisible(false); |
| 4218 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 4219 | setFocusedWindow(mMirror); |
| 4220 | |
| 4221 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4222 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4223 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4224 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4225 | |
| 4226 | mMirror->setVisible(true); |
| 4227 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 4228 | |
| 4229 | // window gets focused |
| 4230 | mWindow->consumeFocusEvent(true); |
| 4231 | // window gets the pending key event |
| 4232 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4233 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4234 | |
| 4235 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 4236 | protected: |
| 4237 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 4238 | sp<FakeWindowHandle> mWindow; |
| 4239 | sp<FakeWindowHandle> mSecondWindow; |
| 4240 | |
| 4241 | void SetUp() override { |
| 4242 | InputDispatcherTest::SetUp(); |
| 4243 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 4244 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 4245 | mWindow->setFocusable(true); |
| 4246 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
| 4247 | mSecondWindow->setFocusable(true); |
| 4248 | |
| 4249 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 4250 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 4251 | |
| 4252 | setFocusedWindow(mWindow); |
| 4253 | mWindow->consumeFocusEvent(true); |
| 4254 | } |
| 4255 | |
| 4256 | void notifyPointerCaptureChanged(bool enabled) { |
| 4257 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled); |
| 4258 | mDispatcher->notifyPointerCaptureChanged(&args); |
| 4259 | } |
| 4260 | |
| 4261 | void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) { |
| 4262 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
| 4263 | mFakePolicy->waitForSetPointerCapture(enabled); |
| 4264 | notifyPointerCaptureChanged(enabled); |
| 4265 | window->consumeCaptureEvent(enabled); |
| 4266 | } |
| 4267 | }; |
| 4268 | |
| 4269 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 4270 | // Ensure that capture cannot be obtained for unfocused windows. |
| 4271 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 4272 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 4273 | mSecondWindow->assertNoEvents(); |
| 4274 | |
| 4275 | // Ensure that capture can be enabled from the focus window. |
| 4276 | requestAndVerifyPointerCapture(mWindow, true); |
| 4277 | |
| 4278 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 4279 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 4280 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 4281 | |
| 4282 | // Ensure that capture can be disabled from the window with capture. |
| 4283 | requestAndVerifyPointerCapture(mWindow, false); |
| 4284 | } |
| 4285 | |
| 4286 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
| 4287 | requestAndVerifyPointerCapture(mWindow, true); |
| 4288 | |
| 4289 | setFocusedWindow(mSecondWindow); |
| 4290 | |
| 4291 | // Ensure that the capture disabled event was sent first. |
| 4292 | mWindow->consumeCaptureEvent(false); |
| 4293 | mWindow->consumeFocusEvent(false); |
| 4294 | mSecondWindow->consumeFocusEvent(true); |
| 4295 | mFakePolicy->waitForSetPointerCapture(false); |
| 4296 | |
| 4297 | // Ensure that additional state changes from InputReader are not sent to the window. |
| 4298 | notifyPointerCaptureChanged(false); |
| 4299 | notifyPointerCaptureChanged(true); |
| 4300 | notifyPointerCaptureChanged(false); |
| 4301 | mWindow->assertNoEvents(); |
| 4302 | mSecondWindow->assertNoEvents(); |
| 4303 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 4304 | } |
| 4305 | |
| 4306 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
| 4307 | requestAndVerifyPointerCapture(mWindow, true); |
| 4308 | |
| 4309 | // InputReader unexpectedly disables and enables pointer capture. |
| 4310 | notifyPointerCaptureChanged(false); |
| 4311 | notifyPointerCaptureChanged(true); |
| 4312 | |
| 4313 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 4314 | mFakePolicy->waitForSetPointerCapture(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4315 | mWindow->consumeCaptureEvent(false); |
| 4316 | mWindow->assertNoEvents(); |
| 4317 | } |
| 4318 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 4319 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 4320 | requestAndVerifyPointerCapture(mWindow, true); |
| 4321 | |
| 4322 | // The first window loses focus. |
| 4323 | setFocusedWindow(mSecondWindow); |
| 4324 | mFakePolicy->waitForSetPointerCapture(false); |
| 4325 | mWindow->consumeCaptureEvent(false); |
| 4326 | |
| 4327 | // Request Pointer Capture from the second window before the notification from InputReader |
| 4328 | // arrives. |
| 4329 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 4330 | mFakePolicy->waitForSetPointerCapture(true); |
| 4331 | |
| 4332 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
| 4333 | notifyPointerCaptureChanged(false); |
| 4334 | |
| 4335 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
| 4336 | notifyPointerCaptureChanged(true); |
| 4337 | |
| 4338 | mSecondWindow->consumeFocusEvent(true); |
| 4339 | mSecondWindow->consumeCaptureEvent(true); |
| 4340 | } |
| 4341 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4342 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 4343 | protected: |
| 4344 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4345 | |
| 4346 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 4347 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 4348 | |
| 4349 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 4350 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 4351 | |
| 4352 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 4353 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 4354 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 4355 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 4356 | MAXIMUM_OBSCURING_OPACITY); |
| 4357 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4358 | static const int32_t TOUCHED_APP_UID = 10001; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4359 | static const int32_t APP_B_UID = 10002; |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4360 | static const int32_t APP_C_UID = 10003; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4361 | |
| 4362 | sp<FakeWindowHandle> mTouchWindow; |
| 4363 | |
| 4364 | virtual void SetUp() override { |
| 4365 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4366 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4367 | mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK); |
| 4368 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 4369 | } |
| 4370 | |
| 4371 | virtual void TearDown() override { |
| 4372 | InputDispatcherTest::TearDown(); |
| 4373 | mTouchWindow.clear(); |
| 4374 | } |
| 4375 | |
| 4376 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4377 | os::TouchOcclusionMode mode, float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4378 | sp<FakeWindowHandle> window = getWindow(uid, name); |
| 4379 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCHABLE); |
| 4380 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4381 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4382 | return window; |
| 4383 | } |
| 4384 | |
| 4385 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { |
| 4386 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 4387 | sp<FakeWindowHandle> window = |
| 4388 | new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
| 4389 | // Generate an arbitrary PID based on the UID |
| 4390 | window->setOwnerInfo(1777 + (uid % 10000), uid); |
| 4391 | return window; |
| 4392 | } |
| 4393 | |
| 4394 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
| 4395 | NotifyMotionArgs args = |
| 4396 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4397 | ADISPLAY_ID_DEFAULT, points); |
| 4398 | mDispatcher->notifyMotion(&args); |
| 4399 | } |
| 4400 | }; |
| 4401 | |
| 4402 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4403 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4404 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4405 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4406 | |
| 4407 | touch(); |
| 4408 | |
| 4409 | mTouchWindow->assertNoEvents(); |
| 4410 | } |
| 4411 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 4412 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4413 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 4414 | const sp<FakeWindowHandle>& w = |
| 4415 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 4416 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 4417 | |
| 4418 | touch(); |
| 4419 | |
| 4420 | mTouchWindow->assertNoEvents(); |
| 4421 | } |
| 4422 | |
| 4423 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 4424 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 4425 | const sp<FakeWindowHandle>& w = |
| 4426 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 4427 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 4428 | |
| 4429 | touch(); |
| 4430 | |
| 4431 | w->assertNoEvents(); |
| 4432 | } |
| 4433 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4434 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4435 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 4436 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4437 | |
| 4438 | touch(); |
| 4439 | |
| 4440 | mTouchWindow->consumeAnyMotionDown(); |
| 4441 | } |
| 4442 | |
| 4443 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4444 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4445 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4446 | w->setFrame(Rect(0, 0, 50, 50)); |
| 4447 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4448 | |
| 4449 | touch({PointF{100, 100}}); |
| 4450 | |
| 4451 | mTouchWindow->consumeAnyMotionDown(); |
| 4452 | } |
| 4453 | |
| 4454 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4455 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4456 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4457 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 4458 | |
| 4459 | touch(); |
| 4460 | |
| 4461 | mTouchWindow->consumeAnyMotionDown(); |
| 4462 | } |
| 4463 | |
| 4464 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 4465 | const sp<FakeWindowHandle>& w = |
| 4466 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 4467 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4468 | |
| 4469 | touch(); |
| 4470 | |
| 4471 | mTouchWindow->consumeAnyMotionDown(); |
| 4472 | } |
| 4473 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 4474 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 4475 | const sp<FakeWindowHandle>& w = |
| 4476 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 4477 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 4478 | |
| 4479 | touch(); |
| 4480 | |
| 4481 | w->assertNoEvents(); |
| 4482 | } |
| 4483 | |
| 4484 | /** |
| 4485 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 4486 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 4487 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 4488 | */ |
| 4489 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 4490 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 4491 | const sp<FakeWindowHandle>& w = |
| 4492 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 4493 | w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH); |
| 4494 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 4495 | |
| 4496 | touch(); |
| 4497 | |
| 4498 | w->consumeMotionOutside(); |
| 4499 | } |
| 4500 | |
| 4501 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 4502 | const sp<FakeWindowHandle>& w = |
| 4503 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 4504 | w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH); |
| 4505 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 4506 | |
| 4507 | touch(); |
| 4508 | |
| 4509 | InputEvent* event = w->consume(); |
| 4510 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()); |
| 4511 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 4512 | EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX()); |
| 4513 | EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY()); |
| 4514 | } |
| 4515 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4516 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4517 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4518 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 4519 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4520 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 4521 | |
| 4522 | touch(); |
| 4523 | |
| 4524 | mTouchWindow->consumeAnyMotionDown(); |
| 4525 | } |
| 4526 | |
| 4527 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 4528 | const sp<FakeWindowHandle>& w = |
| 4529 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 4530 | MAXIMUM_OBSCURING_OPACITY); |
| 4531 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4532 | |
| 4533 | touch(); |
| 4534 | |
| 4535 | mTouchWindow->consumeAnyMotionDown(); |
| 4536 | } |
| 4537 | |
| 4538 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4539 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4540 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 4541 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4542 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 4543 | |
| 4544 | touch(); |
| 4545 | |
| 4546 | mTouchWindow->assertNoEvents(); |
| 4547 | } |
| 4548 | |
| 4549 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 4550 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 4551 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4552 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 4553 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4554 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4555 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 4556 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4557 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 4558 | |
| 4559 | touch(); |
| 4560 | |
| 4561 | mTouchWindow->assertNoEvents(); |
| 4562 | } |
| 4563 | |
| 4564 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 4565 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 4566 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4567 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 4568 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4569 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4570 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 4571 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4572 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 4573 | |
| 4574 | touch(); |
| 4575 | |
| 4576 | mTouchWindow->consumeAnyMotionDown(); |
| 4577 | } |
| 4578 | |
| 4579 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 4580 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 4581 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4582 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 4583 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4584 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4585 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 4586 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4587 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 4588 | |
| 4589 | touch(); |
| 4590 | |
| 4591 | mTouchWindow->consumeAnyMotionDown(); |
| 4592 | } |
| 4593 | |
| 4594 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 4595 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4596 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 4597 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4598 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4599 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 4600 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4601 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4602 | |
| 4603 | touch(); |
| 4604 | |
| 4605 | mTouchWindow->assertNoEvents(); |
| 4606 | } |
| 4607 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4608 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 4609 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 4610 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4611 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 4612 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4613 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4614 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 4615 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4616 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 4617 | |
| 4618 | touch(); |
| 4619 | |
| 4620 | mTouchWindow->assertNoEvents(); |
| 4621 | } |
| 4622 | |
| 4623 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 4624 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 4625 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4626 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 4627 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4628 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4629 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 4630 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4631 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 4632 | |
| 4633 | touch(); |
| 4634 | |
| 4635 | mTouchWindow->consumeAnyMotionDown(); |
| 4636 | } |
| 4637 | |
| 4638 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 4639 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4640 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 4641 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4642 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 4643 | |
| 4644 | touch(); |
| 4645 | |
| 4646 | mTouchWindow->consumeAnyMotionDown(); |
| 4647 | } |
| 4648 | |
| 4649 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 4650 | const sp<FakeWindowHandle>& w = |
| 4651 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 4652 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 4653 | |
| 4654 | touch(); |
| 4655 | |
| 4656 | mTouchWindow->consumeAnyMotionDown(); |
| 4657 | } |
| 4658 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 4659 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 4660 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 4661 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 4662 | const sp<FakeWindowHandle>& w = |
| 4663 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 4664 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 4665 | |
| 4666 | touch(); |
| 4667 | |
| 4668 | mTouchWindow->assertNoEvents(); |
| 4669 | } |
| 4670 | |
| 4671 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 4672 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 4673 | const sp<FakeWindowHandle>& w = |
| 4674 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 4675 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 4676 | |
| 4677 | touch(); |
| 4678 | |
| 4679 | mTouchWindow->consumeAnyMotionDown(); |
| 4680 | } |
| 4681 | |
| 4682 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 4683 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 4684 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 4685 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4686 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 4687 | OPACITY_ABOVE_THRESHOLD); |
| 4688 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 4689 | |
| 4690 | touch(); |
| 4691 | |
| 4692 | mTouchWindow->consumeAnyMotionDown(); |
| 4693 | } |
| 4694 | |
| 4695 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 4696 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 4697 | const sp<FakeWindowHandle>& w1 = |
| 4698 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 4699 | OPACITY_BELOW_THRESHOLD); |
| 4700 | const sp<FakeWindowHandle>& w2 = |
| 4701 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 4702 | OPACITY_BELOW_THRESHOLD); |
| 4703 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 4704 | |
| 4705 | touch(); |
| 4706 | |
| 4707 | mTouchWindow->assertNoEvents(); |
| 4708 | } |
| 4709 | |
| 4710 | /** |
| 4711 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 4712 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 4713 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 4714 | */ |
| 4715 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 4716 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 4717 | const sp<FakeWindowHandle>& wB = |
| 4718 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 4719 | OPACITY_BELOW_THRESHOLD); |
| 4720 | const sp<FakeWindowHandle>& wC = |
| 4721 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 4722 | OPACITY_BELOW_THRESHOLD); |
| 4723 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 4724 | |
| 4725 | touch(); |
| 4726 | |
| 4727 | mTouchWindow->assertNoEvents(); |
| 4728 | } |
| 4729 | |
| 4730 | /** |
| 4731 | * This test is testing that a window from a different UID but with same application token doesn't |
| 4732 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 4733 | */ |
| 4734 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 4735 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 4736 | const sp<FakeWindowHandle>& w = |
| 4737 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 4738 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 4739 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 4740 | |
| 4741 | touch(); |
| 4742 | |
| 4743 | mTouchWindow->consumeAnyMotionDown(); |
| 4744 | } |
| 4745 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4746 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 4747 | protected: |
| 4748 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 4749 | sp<FakeWindowHandle> mWindow; |
| 4750 | sp<FakeWindowHandle> mSecondWindow; |
| 4751 | sp<FakeWindowHandle> mDragWindow; |
| 4752 | |
| 4753 | void SetUp() override { |
| 4754 | InputDispatcherTest::SetUp(); |
| 4755 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 4756 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 4757 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4758 | mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
| 4759 | |
| 4760 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
| 4761 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
| 4762 | mSecondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
| 4763 | |
| 4764 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 4765 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 4766 | } |
| 4767 | |
| 4768 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 4769 | void performDrag() { |
| 4770 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4771 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4772 | {50, 50})) |
| 4773 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4774 | |
| 4775 | // Window should receive motion event. |
| 4776 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4777 | |
| 4778 | // The drag window covers the entire display |
| 4779 | mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
| 4780 | mDispatcher->setInputWindows( |
| 4781 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 4782 | |
| 4783 | // Transfer touch focus to the drag window |
| 4784 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
| 4785 | true /* isDragDrop */); |
| 4786 | mWindow->consumeMotionCancel(); |
| 4787 | mDragWindow->consumeMotionDown(); |
| 4788 | } |
| 4789 | }; |
| 4790 | |
| 4791 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
| 4792 | performDrag(); |
| 4793 | |
| 4794 | // Move on window. |
| 4795 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4796 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4797 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 4798 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4799 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4800 | mWindow->consumeDragEvent(false, 50, 50); |
| 4801 | mSecondWindow->assertNoEvents(); |
| 4802 | |
| 4803 | // Move to another window. |
| 4804 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4805 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4806 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 4807 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4808 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4809 | mWindow->consumeDragEvent(true, 150, 50); |
| 4810 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 4811 | |
| 4812 | // Move back to original window. |
| 4813 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4814 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4815 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 4816 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4817 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4818 | mWindow->consumeDragEvent(false, 50, 50); |
| 4819 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 4820 | |
| 4821 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4822 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 4823 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4824 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 4825 | mWindow->assertNoEvents(); |
| 4826 | mSecondWindow->assertNoEvents(); |
| 4827 | } |
| 4828 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 4829 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
| 4830 | performDrag(); |
| 4831 | |
| 4832 | // Move on window. |
| 4833 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4834 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4835 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 4836 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4837 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4838 | mWindow->consumeDragEvent(false, 50, 50); |
| 4839 | mSecondWindow->assertNoEvents(); |
| 4840 | |
| 4841 | // Move to another window. |
| 4842 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4843 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4844 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 4845 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4846 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4847 | mWindow->consumeDragEvent(true, 150, 50); |
| 4848 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 4849 | |
| 4850 | // drop to another window. |
| 4851 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4852 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4853 | {150, 50})) |
| 4854 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4855 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 4856 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 4857 | mWindow->assertNoEvents(); |
| 4858 | mSecondWindow->assertNoEvents(); |
| 4859 | } |
| 4860 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 4861 | } // namespace android::inputdispatcher |