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