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