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