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