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