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