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