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