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