Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 17 | #include "../dispatcher/InputDispatcher.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 18 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 19 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 20 | #include <android-base/thread_annotations.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 21 | #include <binder/Binder.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 23 | #include <input/Input.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 24 | #include <linux/input.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 25 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 26 | #include <cinttypes> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 27 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 28 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 29 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 30 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 31 | using android::base::StringPrintf; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 32 | using android::os::InputEventInjectionResult; |
| 33 | using android::os::InputEventInjectionSync; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 34 | using namespace android::flag_operators; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 35 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 36 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 37 | |
| 38 | // An arbitrary time value. |
| 39 | static const nsecs_t ARBITRARY_TIME = 1234; |
| 40 | |
| 41 | // An arbitrary device id. |
| 42 | static const int32_t DEVICE_ID = 1; |
| 43 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 44 | // An arbitrary display id. |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 45 | static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 46 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 47 | // An arbitrary injector pid / uid pair that has permission to inject events. |
| 48 | static const int32_t INJECTOR_PID = 999; |
| 49 | static const int32_t INJECTOR_UID = 1001; |
| 50 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 51 | // An arbitrary pid of the gesture monitor window |
| 52 | static constexpr int32_t MONITOR_PID = 2001; |
| 53 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 54 | struct PointF { |
| 55 | float x; |
| 56 | float y; |
| 57 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 58 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 59 | /** |
| 60 | * Return a DOWN key event with KEYCODE_A. |
| 61 | */ |
| 62 | static KeyEvent getTestKeyEvent() { |
| 63 | KeyEvent event; |
| 64 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 65 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 66 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 67 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 68 | return event; |
| 69 | } |
| 70 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 71 | // --- FakeInputDispatcherPolicy --- |
| 72 | |
| 73 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 74 | InputDispatcherConfiguration mConfig; |
| 75 | |
| 76 | protected: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 77 | virtual ~FakeInputDispatcherPolicy() {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 78 | |
| 79 | public: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 80 | FakeInputDispatcherPolicy() {} |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 81 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 82 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 83 | assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action, |
| 84 | args.displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 85 | } |
| 86 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 87 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) { |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 88 | assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action, |
| 89 | args.displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 90 | } |
| 91 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 92 | void assertFilterInputEventWasNotCalled() { |
| 93 | std::scoped_lock lock(mLock); |
| 94 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 95 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 96 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 97 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 98 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 99 | ASSERT_TRUE(mConfigurationChangedTime) |
| 100 | << "Timed out waiting for configuration changed call"; |
| 101 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 102 | mConfigurationChangedTime = std::nullopt; |
| 103 | } |
| 104 | |
| 105 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 106 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 107 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 108 | // 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] | 109 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 110 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 111 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 112 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 113 | mLastNotifySwitch = std::nullopt; |
| 114 | } |
| 115 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 116 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 117 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 118 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 119 | mOnPointerDownToken.clear(); |
| 120 | } |
| 121 | |
| 122 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 123 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 124 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 125 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 128 | // This function must be called soon after the expected ANR timer starts, |
| 129 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 130 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 131 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 132 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
| 133 | std::shared_ptr<InputApplicationHandle> application; |
| 134 | { // acquire lock |
| 135 | std::unique_lock lock(mLock); |
| 136 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 137 | ASSERT_NO_FATAL_FAILURE( |
| 138 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
| 139 | } // release lock |
| 140 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 143 | void assertNotifyConnectionUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 144 | const sp<IBinder>& expectedConnectionToken) { |
| 145 | sp<IBinder> connectionToken = getUnresponsiveConnectionToken(timeout); |
| 146 | ASSERT_EQ(expectedConnectionToken, connectionToken); |
| 147 | } |
| 148 | |
| 149 | void assertNotifyConnectionResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) { |
| 150 | sp<IBinder> connectionToken = getResponsiveConnectionToken(); |
| 151 | ASSERT_EQ(expectedConnectionToken, connectionToken); |
| 152 | } |
| 153 | |
| 154 | sp<IBinder> getUnresponsiveConnectionToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 155 | std::unique_lock lock(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 156 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 157 | return getAnrTokenLockedInterruptible(timeout, mAnrConnectionTokens, lock); |
| 158 | } |
| 159 | |
| 160 | sp<IBinder> getResponsiveConnectionToken() { |
| 161 | std::unique_lock lock(mLock); |
| 162 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 163 | return getAnrTokenLockedInterruptible(0s, mResponsiveConnectionTokens, lock); |
| 164 | } |
| 165 | |
| 166 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 167 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 168 | // first entry from the container and erase it. |
| 169 | template <class T> |
| 170 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 171 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 172 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 173 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 174 | |
| 175 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 176 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 177 | // before checking if ANR was called. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 178 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 179 | // to provide it some time to act. 100ms seems reasonable. |
| 180 | mNotifyAnr.wait_for(lock, timeToWait, |
| 181 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 182 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 183 | if (storage.empty()) { |
| 184 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 185 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 186 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 187 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 188 | // the dispatcher started counting before this function was called |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 189 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 190 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 191 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 192 | << "ms, but waited " |
| 193 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 194 | << "ms instead"; |
| 195 | } |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 196 | T token = storage.front(); |
| 197 | storage.pop(); |
| 198 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void assertNotifyAnrWasNotCalled() { |
| 202 | std::scoped_lock lock(mLock); |
| 203 | ASSERT_TRUE(mAnrApplications.empty()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 204 | ASSERT_TRUE(mAnrConnectionTokens.empty()); |
| 205 | ASSERT_TRUE(mResponsiveConnectionTokens.empty()) |
| 206 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 207 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 210 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 211 | mConfig.keyRepeatTimeout = timeout; |
| 212 | mConfig.keyRepeatDelay = delay; |
| 213 | } |
| 214 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 215 | void waitForSetPointerCapture(bool enabled) { |
| 216 | std::unique_lock lock(mLock); |
| 217 | base::ScopedLockAssertion assumeLocked(mLock); |
| 218 | |
| 219 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 220 | [this, enabled]() REQUIRES(mLock) { |
| 221 | return mPointerCaptureEnabled && |
| 222 | *mPointerCaptureEnabled == |
| 223 | enabled; |
| 224 | })) { |
| 225 | FAIL() << "Timed out waiting for setPointerCapture(" << enabled << ") to be called."; |
| 226 | } |
| 227 | mPointerCaptureEnabled.reset(); |
| 228 | } |
| 229 | |
| 230 | void assertSetPointerCaptureNotCalled() { |
| 231 | std::unique_lock lock(mLock); |
| 232 | base::ScopedLockAssertion assumeLocked(mLock); |
| 233 | |
| 234 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
| 235 | FAIL() << "Expected setPointerCapture(enabled) to not be called, but was called. " |
| 236 | "enabled = " |
| 237 | << *mPointerCaptureEnabled; |
| 238 | } |
| 239 | mPointerCaptureEnabled.reset(); |
| 240 | } |
| 241 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 242 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 243 | std::mutex mLock; |
| 244 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 245 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 246 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 247 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 248 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 249 | std::condition_variable mPointerCaptureChangedCondition; |
| 250 | std::optional<bool> mPointerCaptureEnabled GUARDED_BY(mLock); |
| 251 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 252 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 253 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 254 | std::queue<sp<IBinder>> mAnrConnectionTokens GUARDED_BY(mLock); |
| 255 | std::queue<sp<IBinder>> mResponsiveConnectionTokens GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 256 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 257 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 258 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 259 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 260 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 263 | void notifyConnectionUnresponsive(const sp<IBinder>& connectionToken, |
| 264 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 265 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 266 | mAnrConnectionTokens.push(connectionToken); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 267 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void notifyConnectionResponsive(const sp<IBinder>& connectionToken) override { |
| 271 | std::scoped_lock lock(mLock); |
| 272 | mResponsiveConnectionTokens.push(connectionToken); |
| 273 | mNotifyAnr.notify_all(); |
| 274 | } |
| 275 | |
| 276 | void notifyNoFocusedWindowAnr( |
| 277 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 278 | std::scoped_lock lock(mLock); |
| 279 | mAnrApplications.push(applicationHandle); |
| 280 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 283 | void notifyInputChannelBroken(const sp<IBinder>&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 284 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 285 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 286 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 287 | void notifyUntrustedTouch(const std::string& obscuringPackage) override {} |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 288 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 289 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 290 | const std::vector<float>& values) override {} |
| 291 | |
| 292 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 293 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 294 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 295 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 296 | *outConfig = mConfig; |
| 297 | } |
| 298 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 299 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 300 | std::scoped_lock lock(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 301 | switch (inputEvent->getType()) { |
| 302 | case AINPUT_EVENT_TYPE_KEY: { |
| 303 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 304 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 305 | break; |
| 306 | } |
| 307 | |
| 308 | case AINPUT_EVENT_TYPE_MOTION: { |
| 309 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 310 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 311 | break; |
| 312 | } |
| 313 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 314 | return true; |
| 315 | } |
| 316 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 317 | void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 318 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 319 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 320 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 321 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 322 | return 0; |
| 323 | } |
| 324 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 325 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 326 | return false; |
| 327 | } |
| 328 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 329 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 330 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 331 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 332 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 333 | * essentially a passthrough for notifySwitch. |
| 334 | */ |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 335 | mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 338 | void pokeUserActivity(nsecs_t, int32_t) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 339 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 340 | bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 341 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 342 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 343 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 344 | mOnPointerDownToken = newToken; |
| 345 | } |
| 346 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 347 | void setPointerCapture(bool enabled) override { |
| 348 | std::scoped_lock lock(mLock); |
| 349 | mPointerCaptureEnabled = {enabled}; |
| 350 | mPointerCaptureChangedCondition.notify_all(); |
| 351 | } |
| 352 | |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 353 | void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action, |
| 354 | int32_t displayId) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 355 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 356 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
| 357 | ASSERT_EQ(mFilteredEvent->getType(), type); |
| 358 | |
| 359 | if (type == AINPUT_EVENT_TYPE_KEY) { |
| 360 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent); |
| 361 | EXPECT_EQ(keyEvent.getEventTime(), eventTime); |
| 362 | EXPECT_EQ(keyEvent.getAction(), action); |
| 363 | EXPECT_EQ(keyEvent.getDisplayId(), displayId); |
| 364 | } else if (type == AINPUT_EVENT_TYPE_MOTION) { |
| 365 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent); |
| 366 | EXPECT_EQ(motionEvent.getEventTime(), eventTime); |
| 367 | EXPECT_EQ(motionEvent.getAction(), action); |
| 368 | EXPECT_EQ(motionEvent.getDisplayId(), displayId); |
| 369 | } else { |
| 370 | FAIL() << "Unknown type: " << type; |
| 371 | } |
| 372 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 373 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 374 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 375 | }; |
| 376 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 377 | // --- InputDispatcherTest --- |
| 378 | |
| 379 | class InputDispatcherTest : public testing::Test { |
| 380 | protected: |
| 381 | sp<FakeInputDispatcherPolicy> mFakePolicy; |
| 382 | sp<InputDispatcher> mDispatcher; |
| 383 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 384 | virtual void SetUp() override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 385 | mFakePolicy = new FakeInputDispatcherPolicy(); |
| 386 | mDispatcher = new InputDispatcher(mFakePolicy); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 387 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 388 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 389 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 392 | virtual void TearDown() override { |
| 393 | ASSERT_EQ(OK, mDispatcher->stop()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 394 | mFakePolicy.clear(); |
| 395 | mDispatcher.clear(); |
| 396 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 397 | |
| 398 | /** |
| 399 | * Used for debugging when writing the test |
| 400 | */ |
| 401 | void dumpDispatcherState() { |
| 402 | std::string dump; |
| 403 | mDispatcher->dump(dump); |
| 404 | std::stringstream ss(dump); |
| 405 | std::string to; |
| 406 | |
| 407 | while (std::getline(ss, to, '\n')) { |
| 408 | ALOGE("%s", to.c_str()); |
| 409 | } |
| 410 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 411 | |
| 412 | void setFocusedWindow(const sp<InputWindowHandle>& window, |
| 413 | const sp<InputWindowHandle>& focusedWindow = nullptr) { |
| 414 | FocusRequest request; |
| 415 | request.token = window->getToken(); |
| 416 | if (focusedWindow) { |
| 417 | request.focusedToken = focusedWindow->getToken(); |
| 418 | } |
| 419 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 420 | request.displayId = window->getInfo()->displayId; |
| 421 | mDispatcher->setFocusedWindow(request); |
| 422 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 423 | }; |
| 424 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 425 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 426 | KeyEvent event; |
| 427 | |
| 428 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 429 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 430 | INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 431 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 432 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 433 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 434 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 435 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 436 | << "Should reject key events with undefined action."; |
| 437 | |
| 438 | // 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] | 439 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 440 | 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] | 441 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 442 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 443 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 444 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 445 | << "Should reject key events with ACTION_MULTIPLE."; |
| 446 | } |
| 447 | |
| 448 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 449 | MotionEvent event; |
| 450 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 451 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
| 452 | for (int i = 0; i <= MAX_POINTERS; i++) { |
| 453 | pointerProperties[i].clear(); |
| 454 | pointerProperties[i].id = i; |
| 455 | pointerCoords[i].clear(); |
| 456 | } |
| 457 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 458 | // Some constants commonly used below |
| 459 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 460 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 461 | constexpr int32_t metaState = AMETA_NONE; |
| 462 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 463 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 464 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 465 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 466 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 467 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, |
| 468 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 469 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 470 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 471 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 472 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 473 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 474 | << "Should reject motion events with undefined action."; |
| 475 | |
| 476 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 477 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 478 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 479 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 480 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 481 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 482 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, |
| 483 | pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 484 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 485 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 486 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 487 | << "Should reject motion events with pointer down index too large."; |
| 488 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 489 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 490 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 491 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 492 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 493 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 494 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, |
| 495 | pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 496 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 497 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 498 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 499 | << "Should reject motion events with pointer down index too small."; |
| 500 | |
| 501 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 502 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 503 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 504 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 505 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 506 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 507 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, |
| 508 | pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 509 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 510 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 511 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 512 | << "Should reject motion events with pointer up index too large."; |
| 513 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 514 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 515 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 516 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 517 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 518 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 519 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, |
| 520 | pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 521 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 522 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 523 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 524 | << "Should reject motion events with pointer up index too small."; |
| 525 | |
| 526 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 527 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 528 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 529 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 530 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 531 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 532 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 533 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 534 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 535 | << "Should reject motion events with 0 pointers."; |
| 536 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 537 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 538 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 539 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 540 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 541 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 542 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 543 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 544 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 545 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 546 | |
| 547 | // Rejects motion events with invalid pointer ids. |
| 548 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 549 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 550 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 551 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 552 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 553 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 554 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 555 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 556 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 557 | << "Should reject motion events with pointer ids less than 0."; |
| 558 | |
| 559 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 560 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 561 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 562 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 563 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 564 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 565 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 566 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 567 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 568 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 569 | |
| 570 | // Rejects motion events with duplicate pointer ids. |
| 571 | pointerProperties[0].id = 1; |
| 572 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 573 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 574 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 575 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 576 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 577 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 578 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 579 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 580 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 581 | << "Should reject motion events with duplicate pointer ids."; |
| 582 | } |
| 583 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 584 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 585 | |
| 586 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 587 | constexpr nsecs_t eventTime = 20; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 588 | NotifyConfigurationChangedArgs args(10 /*id*/, eventTime); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 589 | mDispatcher->notifyConfigurationChanged(&args); |
| 590 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 591 | |
| 592 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 593 | } |
| 594 | |
| 595 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 596 | NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/, |
| 597 | 2 /*switchMask*/); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 598 | mDispatcher->notifySwitch(&args); |
| 599 | |
| 600 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 601 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 602 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 603 | } |
| 604 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 605 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 606 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 607 | static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 608 | |
| 609 | class FakeApplicationHandle : public InputApplicationHandle { |
| 610 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 611 | FakeApplicationHandle() { |
| 612 | mInfo.name = "Fake Application"; |
| 613 | mInfo.token = new BBinder(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 614 | mInfo.dispatchingTimeoutMillis = |
| 615 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 616 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 617 | virtual ~FakeApplicationHandle() {} |
| 618 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 619 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 620 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 621 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 622 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 623 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 624 | }; |
| 625 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 626 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 627 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 628 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 629 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 630 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 633 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 634 | InputEvent* event; |
| 635 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 636 | if (!consumeSeq) { |
| 637 | return nullptr; |
| 638 | } |
| 639 | finishEvent(*consumeSeq); |
| 640 | return event; |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * Receive an event without acknowledging it. |
| 645 | * Return the sequence number that could later be used to send finished signal. |
| 646 | */ |
| 647 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 648 | uint32_t consumeSeq; |
| 649 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 650 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 651 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 652 | status_t status = WOULD_BLOCK; |
| 653 | while (status == WOULD_BLOCK) { |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 654 | status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 655 | &event); |
| 656 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 657 | if (elapsed > 100ms) { |
| 658 | break; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | if (status == WOULD_BLOCK) { |
| 663 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 664 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | if (status != OK) { |
| 668 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 669 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 670 | } |
| 671 | if (event == nullptr) { |
| 672 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 673 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 674 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 675 | if (outEvent != nullptr) { |
| 676 | *outEvent = event; |
| 677 | } |
| 678 | return consumeSeq; |
| 679 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 680 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 681 | /** |
| 682 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 683 | */ |
| 684 | void finishEvent(uint32_t consumeSeq) { |
| 685 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 686 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId, |
| 690 | int32_t expectedFlags) { |
| 691 | InputEvent* event = consume(); |
| 692 | |
| 693 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 694 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 695 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 696 | << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType) |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 697 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 698 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 699 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 700 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 701 | switch (expectedEventType) { |
| 702 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 703 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 704 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
| 705 | EXPECT_EQ(expectedFlags, keyEvent.getFlags()); |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 706 | break; |
| 707 | } |
| 708 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 709 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 710 | EXPECT_EQ(expectedAction, motionEvent.getAction()); |
| 711 | EXPECT_EQ(expectedFlags, motionEvent.getFlags()); |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 712 | break; |
| 713 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 714 | case AINPUT_EVENT_TYPE_FOCUS: { |
| 715 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 716 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 717 | case AINPUT_EVENT_TYPE_CAPTURE: { |
| 718 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 719 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 720 | default: { |
| 721 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; |
| 722 | } |
| 723 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 724 | } |
| 725 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 726 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 727 | InputEvent* event = consume(); |
| 728 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 729 | << ": consumer should have returned non-NULL event."; |
| 730 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType()) |
| 731 | << "Got " << inputEventTypeToString(event->getType()) |
| 732 | << " event instead of FOCUS event"; |
| 733 | |
| 734 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 735 | << mName.c_str() << ": event displayId should always be NONE."; |
| 736 | |
| 737 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 738 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
| 739 | EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode()); |
| 740 | } |
| 741 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 742 | void consumeCaptureEvent(bool hasCapture) { |
| 743 | const InputEvent* event = consume(); |
| 744 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 745 | << ": consumer should have returned non-NULL event."; |
| 746 | ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType()) |
| 747 | << "Got " << inputEventTypeToString(event->getType()) |
| 748 | << " event instead of CAPTURE event"; |
| 749 | |
| 750 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 751 | << mName.c_str() << ": event displayId should always be NONE."; |
| 752 | |
| 753 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 754 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 755 | } |
| 756 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 757 | void assertNoEvents() { |
| 758 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 759 | if (event == nullptr) { |
| 760 | return; |
| 761 | } |
| 762 | if (event->getType() == AINPUT_EVENT_TYPE_KEY) { |
| 763 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 764 | ADD_FAILURE() << "Received key event " |
| 765 | << KeyEvent::actionToString(keyEvent.getAction()); |
| 766 | } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) { |
| 767 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 768 | ADD_FAILURE() << "Received motion event " |
| 769 | << MotionEvent::actionToString(motionEvent.getAction()); |
| 770 | } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) { |
| 771 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 772 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 773 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 774 | } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) { |
| 775 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 776 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 777 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 778 | } |
| 779 | FAIL() << mName.c_str() |
| 780 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 784 | |
| 785 | protected: |
| 786 | std::unique_ptr<InputConsumer> mConsumer; |
| 787 | PreallocatedInputEventFactory mEventFactory; |
| 788 | |
| 789 | std::string mName; |
| 790 | }; |
| 791 | |
| 792 | class FakeWindowHandle : public InputWindowHandle { |
| 793 | public: |
| 794 | static const int32_t WIDTH = 600; |
| 795 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 796 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 797 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 798 | const sp<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 799 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 800 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 801 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 802 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 803 | dispatcher->createInputChannel(name); |
| 804 | token = (*channel)->getConnectionToken(); |
| 805 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | inputApplicationHandle->updateInfo(); |
| 809 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 810 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 811 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 812 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 813 | mInfo.name = name; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 814 | mInfo.type = InputWindowInfo::Type::APPLICATION; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 815 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 816 | mInfo.frameLeft = 0; |
| 817 | mInfo.frameTop = 0; |
| 818 | mInfo.frameRight = WIDTH; |
| 819 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 820 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 821 | mInfo.globalScaleFactor = 1.0; |
| 822 | mInfo.touchableRegion.clear(); |
| 823 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
| 824 | mInfo.visible = true; |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 825 | mInfo.focusable = false; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 826 | mInfo.hasWallpaper = false; |
| 827 | mInfo.paused = false; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 828 | mInfo.ownerPid = INJECTOR_PID; |
| 829 | mInfo.ownerUid = INJECTOR_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 830 | mInfo.displayId = displayId; |
| 831 | } |
| 832 | |
| 833 | virtual bool updateInfo() { return true; } |
| 834 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 835 | void setFocusable(bool focusable) { mInfo.focusable = focusable; } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 836 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 837 | void setVisible(bool visible) { mInfo.visible = visible; } |
| 838 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 839 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 840 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 843 | void setPaused(bool paused) { mInfo.paused = paused; } |
| 844 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 845 | void setFrame(const Rect& frame) { |
| 846 | mInfo.frameLeft = frame.left; |
| 847 | mInfo.frameTop = frame.top; |
| 848 | mInfo.frameRight = frame.right; |
| 849 | mInfo.frameBottom = frame.bottom; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 850 | mInfo.transform.set(frame.left, frame.top); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 851 | mInfo.touchableRegion.clear(); |
| 852 | mInfo.addTouchableRegion(frame); |
| 853 | } |
| 854 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 855 | void setFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags = flags; } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 856 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 857 | void setInputFeatures(InputWindowInfo::Feature features) { mInfo.inputFeatures = features; } |
| 858 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 859 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 860 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 861 | } |
| 862 | |
| 863 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 864 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 865 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 866 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 867 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 868 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 869 | expectedFlags); |
| 870 | } |
| 871 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 872 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 873 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
| 874 | } |
| 875 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 876 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 877 | int32_t expectedFlags = 0) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 878 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId, |
| 879 | expectedFlags); |
| 880 | } |
| 881 | |
| 882 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 883 | int32_t expectedFlags = 0) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 884 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId, |
| 885 | expectedFlags); |
| 886 | } |
| 887 | |
| 888 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 889 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 890 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
| 891 | expectedFlags); |
| 892 | } |
| 893 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 894 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 895 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 896 | int32_t expectedFlags = 0) { |
| 897 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 898 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 899 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 900 | } |
| 901 | |
| 902 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 903 | int32_t expectedFlags = 0) { |
| 904 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 905 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 906 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 907 | } |
| 908 | |
| 909 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 910 | int32_t expectedFlags = 0) { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 911 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
| 912 | expectedFlags); |
| 913 | } |
| 914 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 915 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 916 | int32_t expectedFlags = 0) { |
| 917 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
| 918 | expectedFlags); |
| 919 | } |
| 920 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 921 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 922 | ASSERT_NE(mInputReceiver, nullptr) |
| 923 | << "Cannot consume events from a window with no receiver"; |
| 924 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 925 | } |
| 926 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 927 | void consumeCaptureEvent(bool hasCapture) { |
| 928 | ASSERT_NE(mInputReceiver, nullptr) |
| 929 | << "Cannot consume events from a window with no receiver"; |
| 930 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 931 | } |
| 932 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 933 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId, |
| 934 | int32_t expectedFlags) { |
| 935 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 936 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 937 | expectedFlags); |
| 938 | } |
| 939 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 940 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 941 | if (mInputReceiver == nullptr) { |
| 942 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 943 | return std::nullopt; |
| 944 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 945 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | void finishEvent(uint32_t sequenceNum) { |
| 949 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 950 | mInputReceiver->finishEvent(sequenceNum); |
| 951 | } |
| 952 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 953 | InputEvent* consume() { |
| 954 | if (mInputReceiver == nullptr) { |
| 955 | return nullptr; |
| 956 | } |
| 957 | return mInputReceiver->consume(); |
| 958 | } |
| 959 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 960 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 961 | if (mInputReceiver == nullptr && |
| 962 | mInfo.inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL)) { |
| 963 | return; // Can't receive events if the window does not have input channel |
| 964 | } |
| 965 | ASSERT_NE(nullptr, mInputReceiver) |
| 966 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 967 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 968 | } |
| 969 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 970 | sp<IBinder> getToken() { return mInfo.token; } |
| 971 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 972 | const std::string& getName() { return mName; } |
| 973 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 974 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { |
| 975 | mInfo.ownerPid = ownerPid; |
| 976 | mInfo.ownerUid = ownerUid; |
| 977 | } |
| 978 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 979 | private: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 980 | const std::string mName; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 981 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 982 | 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] | 983 | }; |
| 984 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 985 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 986 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 987 | static InputEventInjectionResult injectKey( |
| 988 | const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
| 989 | int32_t displayId = ADISPLAY_ID_NONE, |
| 990 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
| 991 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 992 | KeyEvent event; |
| 993 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 994 | |
| 995 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 996 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 997 | INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE, |
| 998 | repeatCount, currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 999 | |
| 1000 | // Inject event until dispatch out. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1001 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode, |
| 1002 | injectionTimeout, |
| 1003 | POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1004 | } |
| 1005 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1006 | static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher, |
| 1007 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1008 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId); |
| 1009 | } |
| 1010 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1011 | static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher, |
| 1012 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1013 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId); |
| 1014 | } |
| 1015 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1016 | class PointerBuilder { |
| 1017 | public: |
| 1018 | PointerBuilder(int32_t id, int32_t toolType) { |
| 1019 | mProperties.clear(); |
| 1020 | mProperties.id = id; |
| 1021 | mProperties.toolType = toolType; |
| 1022 | mCoords.clear(); |
| 1023 | } |
| 1024 | |
| 1025 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } |
| 1026 | |
| 1027 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } |
| 1028 | |
| 1029 | PointerBuilder& axis(int32_t axis, float value) { |
| 1030 | mCoords.setAxisValue(axis, value); |
| 1031 | return *this; |
| 1032 | } |
| 1033 | |
| 1034 | PointerProperties buildProperties() const { return mProperties; } |
| 1035 | |
| 1036 | PointerCoords buildCoords() const { return mCoords; } |
| 1037 | |
| 1038 | private: |
| 1039 | PointerProperties mProperties; |
| 1040 | PointerCoords mCoords; |
| 1041 | }; |
| 1042 | |
| 1043 | class MotionEventBuilder { |
| 1044 | public: |
| 1045 | MotionEventBuilder(int32_t action, int32_t source) { |
| 1046 | mAction = action; |
| 1047 | mSource = source; |
| 1048 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1049 | } |
| 1050 | |
| 1051 | MotionEventBuilder& eventTime(nsecs_t eventTime) { |
| 1052 | mEventTime = eventTime; |
| 1053 | return *this; |
| 1054 | } |
| 1055 | |
| 1056 | MotionEventBuilder& displayId(int32_t displayId) { |
| 1057 | mDisplayId = displayId; |
| 1058 | return *this; |
| 1059 | } |
| 1060 | |
| 1061 | MotionEventBuilder& actionButton(int32_t actionButton) { |
| 1062 | mActionButton = actionButton; |
| 1063 | return *this; |
| 1064 | } |
| 1065 | |
| 1066 | MotionEventBuilder& buttonState(int32_t actionButton) { |
| 1067 | mActionButton = actionButton; |
| 1068 | return *this; |
| 1069 | } |
| 1070 | |
| 1071 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 1072 | mRawXCursorPosition = rawXCursorPosition; |
| 1073 | return *this; |
| 1074 | } |
| 1075 | |
| 1076 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 1077 | mRawYCursorPosition = rawYCursorPosition; |
| 1078 | return *this; |
| 1079 | } |
| 1080 | |
| 1081 | MotionEventBuilder& pointer(PointerBuilder pointer) { |
| 1082 | mPointers.push_back(pointer); |
| 1083 | return *this; |
| 1084 | } |
| 1085 | |
| 1086 | MotionEvent build() { |
| 1087 | std::vector<PointerProperties> pointerProperties; |
| 1088 | std::vector<PointerCoords> pointerCoords; |
| 1089 | for (const PointerBuilder& pointer : mPointers) { |
| 1090 | pointerProperties.push_back(pointer.buildProperties()); |
| 1091 | pointerCoords.push_back(pointer.buildCoords()); |
| 1092 | } |
| 1093 | |
| 1094 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 1095 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 1096 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 1097 | mPointers.size() == 1) { |
| 1098 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 1099 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 1100 | } |
| 1101 | |
| 1102 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1103 | ui::Transform identityTransform; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1104 | event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC, |
| 1105 | mAction, mActionButton, /* flags */ 0, /* edgeFlags */ 0, AMETA_NONE, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1106 | mButtonState, MotionClassification::NONE, identityTransform, |
| 1107 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, |
| 1108 | mRawYCursorPosition, mEventTime, mEventTime, mPointers.size(), |
| 1109 | pointerProperties.data(), pointerCoords.data()); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1110 | |
| 1111 | return event; |
| 1112 | } |
| 1113 | |
| 1114 | private: |
| 1115 | int32_t mAction; |
| 1116 | int32_t mSource; |
| 1117 | nsecs_t mEventTime; |
| 1118 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1119 | int32_t mActionButton{0}; |
| 1120 | int32_t mButtonState{0}; |
| 1121 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1122 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1123 | |
| 1124 | std::vector<PointerBuilder> mPointers; |
| 1125 | }; |
| 1126 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1127 | static InputEventInjectionResult injectMotionEvent( |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1128 | const sp<InputDispatcher>& dispatcher, const MotionEvent& event, |
| 1129 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1130 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1131 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode, |
| 1132 | injectionTimeout, |
| 1133 | POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
| 1134 | } |
| 1135 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1136 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1137 | const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId, |
| 1138 | const PointF& position, |
| 1139 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1140 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1141 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1142 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1143 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1144 | MotionEvent event = MotionEventBuilder(action, source) |
| 1145 | .displayId(displayId) |
| 1146 | .eventTime(eventTime) |
| 1147 | .rawXCursorPosition(cursorPosition.x) |
| 1148 | .rawYCursorPosition(cursorPosition.y) |
| 1149 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1150 | .x(position.x) |
| 1151 | .y(position.y)) |
| 1152 | .build(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1153 | |
| 1154 | // Inject event until dispatch out. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1155 | return injectMotionEvent(dispatcher, event); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1156 | } |
| 1157 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1158 | static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher, |
| 1159 | int32_t source, int32_t displayId, |
| 1160 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1161 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1164 | static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher, |
| 1165 | int32_t source, int32_t displayId, |
| 1166 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1167 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1170 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1171 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1172 | // Define a valid key event. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1173 | NotifyKeyArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
| 1174 | POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, KEY_A, |
| 1175 | AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1176 | |
| 1177 | return args; |
| 1178 | } |
| 1179 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1180 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, |
| 1181 | const std::vector<PointF>& points) { |
| 1182 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1183 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1184 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1185 | } |
| 1186 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1187 | PointerProperties pointerProperties[pointerCount]; |
| 1188 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1189 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1190 | for (size_t i = 0; i < pointerCount; i++) { |
| 1191 | pointerProperties[i].clear(); |
| 1192 | pointerProperties[i].id = i; |
| 1193 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1194 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1195 | pointerCoords[i].clear(); |
| 1196 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1197 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1198 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1199 | |
| 1200 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1201 | // Define a valid motion event. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1202 | NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1203 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 1204 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1205 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 1206 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1207 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 1208 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1209 | |
| 1210 | return args; |
| 1211 | } |
| 1212 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1213 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1214 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1215 | } |
| 1216 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1217 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(bool enabled) { |
| 1218 | return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), enabled); |
| 1219 | } |
| 1220 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1221 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1222 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1223 | sp<FakeWindowHandle> window = |
| 1224 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1225 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1226 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1227 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1228 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1229 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1230 | |
| 1231 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1232 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1233 | } |
| 1234 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1235 | /** |
| 1236 | * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues. |
| 1237 | * To ensure that window receives only events that were directly inside of it, add |
| 1238 | * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input |
| 1239 | * when finding touched windows. |
| 1240 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1241 | * called twice. |
| 1242 | */ |
| 1243 | TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1244 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1245 | sp<FakeWindowHandle> window = |
| 1246 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1247 | window->setFrame(Rect(0, 0, 100, 100)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1248 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1249 | |
| 1250 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1251 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1252 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1253 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1254 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1255 | |
| 1256 | // Window should receive motion event. |
| 1257 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1258 | } |
| 1259 | |
| 1260 | /** |
| 1261 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
| 1262 | * To ensure that window receives only events that were directly inside of it, add |
| 1263 | * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input |
| 1264 | * when finding touched windows. |
| 1265 | */ |
| 1266 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1267 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1268 | sp<FakeWindowHandle> window = |
| 1269 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1270 | window->setFrame(Rect(0, 0, 100, 100)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1271 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1272 | |
| 1273 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1274 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1275 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1276 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1277 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1278 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1279 | |
| 1280 | // Window should receive motion event. |
| 1281 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1282 | } |
| 1283 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1284 | // The foreground window should receive the first touch down event. |
| 1285 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1286 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1287 | sp<FakeWindowHandle> windowTop = |
| 1288 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 1289 | sp<FakeWindowHandle> windowSecond = |
| 1290 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1291 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1292 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1293 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1294 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1295 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1296 | |
| 1297 | // 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] | 1298 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1299 | windowSecond->assertNoEvents(); |
| 1300 | } |
| 1301 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1302 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1303 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1304 | sp<FakeWindowHandle> windowLeft = |
| 1305 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1306 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1307 | windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1308 | sp<FakeWindowHandle> windowRight = |
| 1309 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1310 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1311 | windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1312 | |
| 1313 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1314 | |
| 1315 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 1316 | |
| 1317 | // 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] | 1318 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1319 | injectMotionEvent(mDispatcher, |
| 1320 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1321 | AINPUT_SOURCE_MOUSE) |
| 1322 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1323 | .x(900) |
| 1324 | .y(400)) |
| 1325 | .build())); |
| 1326 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1327 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1328 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1329 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1330 | |
| 1331 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1332 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1333 | injectMotionEvent(mDispatcher, |
| 1334 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1335 | AINPUT_SOURCE_MOUSE) |
| 1336 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1337 | .x(300) |
| 1338 | .y(400)) |
| 1339 | .build())); |
| 1340 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1341 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1342 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1343 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1344 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1345 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1346 | |
| 1347 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1348 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1349 | injectMotionEvent(mDispatcher, |
| 1350 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 1351 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1352 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1353 | .x(300) |
| 1354 | .y(400)) |
| 1355 | .build())); |
| 1356 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1357 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1358 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1359 | injectMotionEvent(mDispatcher, |
| 1360 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1361 | AINPUT_SOURCE_MOUSE) |
| 1362 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1363 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1364 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1365 | .x(300) |
| 1366 | .y(400)) |
| 1367 | .build())); |
| 1368 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1369 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1370 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1371 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1372 | injectMotionEvent(mDispatcher, |
| 1373 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1374 | AINPUT_SOURCE_MOUSE) |
| 1375 | .buttonState(0) |
| 1376 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1377 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1378 | .x(300) |
| 1379 | .y(400)) |
| 1380 | .build())); |
| 1381 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1382 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1383 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1384 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1385 | injectMotionEvent(mDispatcher, |
| 1386 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 1387 | .buttonState(0) |
| 1388 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1389 | .x(300) |
| 1390 | .y(400)) |
| 1391 | .build())); |
| 1392 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1393 | |
| 1394 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1395 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1396 | injectMotionEvent(mDispatcher, |
| 1397 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1398 | AINPUT_SOURCE_MOUSE) |
| 1399 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1400 | .x(900) |
| 1401 | .y(400)) |
| 1402 | .build())); |
| 1403 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1404 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1405 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1406 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1407 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1408 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1409 | } |
| 1410 | |
| 1411 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 1412 | // directly in this test. |
| 1413 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1414 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1415 | sp<FakeWindowHandle> window = |
| 1416 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1417 | window->setFrame(Rect(0, 0, 1200, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1418 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1419 | |
| 1420 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1421 | |
| 1422 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1423 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1424 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1425 | injectMotionEvent(mDispatcher, |
| 1426 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1427 | AINPUT_SOURCE_MOUSE) |
| 1428 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1429 | .x(300) |
| 1430 | .y(400)) |
| 1431 | .build())); |
| 1432 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1433 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1434 | |
| 1435 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1436 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1437 | injectMotionEvent(mDispatcher, |
| 1438 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 1439 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1440 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1441 | .x(300) |
| 1442 | .y(400)) |
| 1443 | .build())); |
| 1444 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1445 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1446 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1447 | injectMotionEvent(mDispatcher, |
| 1448 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1449 | AINPUT_SOURCE_MOUSE) |
| 1450 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1451 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1452 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1453 | .x(300) |
| 1454 | .y(400)) |
| 1455 | .build())); |
| 1456 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1457 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1458 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1459 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1460 | injectMotionEvent(mDispatcher, |
| 1461 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1462 | AINPUT_SOURCE_MOUSE) |
| 1463 | .buttonState(0) |
| 1464 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1465 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1466 | .x(300) |
| 1467 | .y(400)) |
| 1468 | .build())); |
| 1469 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1470 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1471 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1472 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1473 | injectMotionEvent(mDispatcher, |
| 1474 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 1475 | .buttonState(0) |
| 1476 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1477 | .x(300) |
| 1478 | .y(400)) |
| 1479 | .build())); |
| 1480 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1481 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1482 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1483 | injectMotionEvent(mDispatcher, |
| 1484 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1485 | AINPUT_SOURCE_MOUSE) |
| 1486 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1487 | .x(300) |
| 1488 | .y(400)) |
| 1489 | .build())); |
| 1490 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1491 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1492 | } |
| 1493 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1494 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1495 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1496 | |
| 1497 | sp<FakeWindowHandle> windowLeft = |
| 1498 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1499 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1500 | windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1501 | sp<FakeWindowHandle> windowRight = |
| 1502 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1503 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1504 | windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1505 | |
| 1506 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1507 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1508 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1509 | |
| 1510 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 1511 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1512 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1513 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1514 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1515 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1516 | windowRight->assertNoEvents(); |
| 1517 | } |
| 1518 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1519 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1520 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1521 | sp<FakeWindowHandle> window = |
| 1522 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1523 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1524 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1525 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1526 | setFocusedWindow(window); |
| 1527 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1528 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1529 | |
| 1530 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1531 | mDispatcher->notifyKey(&keyArgs); |
| 1532 | |
| 1533 | // Window should receive key down event. |
| 1534 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 1535 | |
| 1536 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 1537 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1538 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1539 | mDispatcher->notifyDeviceReset(&args); |
| 1540 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 1541 | AKEY_EVENT_FLAG_CANCELED); |
| 1542 | } |
| 1543 | |
| 1544 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1545 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1546 | sp<FakeWindowHandle> window = |
| 1547 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1548 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1549 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1550 | |
| 1551 | NotifyMotionArgs motionArgs = |
| 1552 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1553 | ADISPLAY_ID_DEFAULT); |
| 1554 | mDispatcher->notifyMotion(&motionArgs); |
| 1555 | |
| 1556 | // Window should receive motion down event. |
| 1557 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1558 | |
| 1559 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 1560 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1561 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1562 | mDispatcher->notifyDeviceReset(&args); |
| 1563 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, |
| 1564 | 0 /*expectedFlags*/); |
| 1565 | } |
| 1566 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1567 | TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1568 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1569 | |
| 1570 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1571 | sp<FakeWindowHandle> firstWindow = |
| 1572 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 1573 | sp<FakeWindowHandle> secondWindow = |
| 1574 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1575 | |
| 1576 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1577 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1578 | |
| 1579 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1580 | NotifyMotionArgs downMotionArgs = |
| 1581 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1582 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1583 | mDispatcher->notifyMotion(&downMotionArgs); |
| 1584 | // Only the first window should get the down event |
| 1585 | firstWindow->consumeMotionDown(); |
| 1586 | secondWindow->assertNoEvents(); |
| 1587 | |
| 1588 | // Transfer touch focus to the second window |
| 1589 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 1590 | // The first window gets cancel and the second gets down |
| 1591 | firstWindow->consumeMotionCancel(); |
| 1592 | secondWindow->consumeMotionDown(); |
| 1593 | |
| 1594 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1595 | NotifyMotionArgs upMotionArgs = |
| 1596 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 1597 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1598 | mDispatcher->notifyMotion(&upMotionArgs); |
| 1599 | // The first window gets no events and the second gets up |
| 1600 | firstWindow->assertNoEvents(); |
| 1601 | secondWindow->consumeMotionUp(); |
| 1602 | } |
| 1603 | |
| 1604 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1605 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1606 | |
| 1607 | PointF touchPoint = {10, 10}; |
| 1608 | |
| 1609 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1610 | sp<FakeWindowHandle> firstWindow = |
| 1611 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 1612 | sp<FakeWindowHandle> secondWindow = |
| 1613 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1614 | |
| 1615 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1616 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1617 | |
| 1618 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1619 | NotifyMotionArgs downMotionArgs = |
| 1620 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1621 | ADISPLAY_ID_DEFAULT, {touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1622 | mDispatcher->notifyMotion(&downMotionArgs); |
| 1623 | // Only the first window should get the down event |
| 1624 | firstWindow->consumeMotionDown(); |
| 1625 | secondWindow->assertNoEvents(); |
| 1626 | |
| 1627 | // Send pointer down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1628 | NotifyMotionArgs pointerDownMotionArgs = |
| 1629 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1630 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1631 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1632 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1633 | mDispatcher->notifyMotion(&pointerDownMotionArgs); |
| 1634 | // Only the first window should get the pointer down event |
| 1635 | firstWindow->consumeMotionPointerDown(1); |
| 1636 | secondWindow->assertNoEvents(); |
| 1637 | |
| 1638 | // Transfer touch focus to the second window |
| 1639 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 1640 | // The first window gets cancel and the second gets down and pointer down |
| 1641 | firstWindow->consumeMotionCancel(); |
| 1642 | secondWindow->consumeMotionDown(); |
| 1643 | secondWindow->consumeMotionPointerDown(1); |
| 1644 | |
| 1645 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1646 | NotifyMotionArgs pointerUpMotionArgs = |
| 1647 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | |
| 1648 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1649 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1650 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1651 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 1652 | // The first window gets nothing and the second gets pointer up |
| 1653 | firstWindow->assertNoEvents(); |
| 1654 | secondWindow->consumeMotionPointerUp(1); |
| 1655 | |
| 1656 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1657 | NotifyMotionArgs upMotionArgs = |
| 1658 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 1659 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1660 | mDispatcher->notifyMotion(&upMotionArgs); |
| 1661 | // The first window gets nothing and the second gets up |
| 1662 | firstWindow->assertNoEvents(); |
| 1663 | secondWindow->consumeMotionUp(); |
| 1664 | } |
| 1665 | |
| 1666 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1667 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1668 | |
| 1669 | // Create a non touch modal window that supports split touch |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1670 | sp<FakeWindowHandle> firstWindow = |
| 1671 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1672 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1673 | firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 1674 | InputWindowInfo::Flag::SPLIT_TOUCH); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1675 | |
| 1676 | // Create a non touch modal window that supports split touch |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1677 | sp<FakeWindowHandle> secondWindow = |
| 1678 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1679 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1680 | secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 1681 | InputWindowInfo::Flag::SPLIT_TOUCH); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1682 | |
| 1683 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1684 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1685 | |
| 1686 | PointF pointInFirst = {300, 200}; |
| 1687 | PointF pointInSecond = {300, 600}; |
| 1688 | |
| 1689 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1690 | NotifyMotionArgs firstDownMotionArgs = |
| 1691 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1692 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1693 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 1694 | // Only the first window should get the down event |
| 1695 | firstWindow->consumeMotionDown(); |
| 1696 | secondWindow->assertNoEvents(); |
| 1697 | |
| 1698 | // Send down to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1699 | NotifyMotionArgs secondDownMotionArgs = |
| 1700 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1701 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1702 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1703 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1704 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 1705 | // The first window gets a move and the second a down |
| 1706 | firstWindow->consumeMotionMove(); |
| 1707 | secondWindow->consumeMotionDown(); |
| 1708 | |
| 1709 | // Transfer touch focus to the second window |
| 1710 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 1711 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 1712 | firstWindow->consumeMotionCancel(); |
| 1713 | secondWindow->consumeMotionPointerDown(1); |
| 1714 | |
| 1715 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1716 | NotifyMotionArgs pointerUpMotionArgs = |
| 1717 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | |
| 1718 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1719 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1720 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1721 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 1722 | // The first window gets nothing and the second gets pointer up |
| 1723 | firstWindow->assertNoEvents(); |
| 1724 | secondWindow->consumeMotionPointerUp(1); |
| 1725 | |
| 1726 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1727 | NotifyMotionArgs upMotionArgs = |
| 1728 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 1729 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1730 | mDispatcher->notifyMotion(&upMotionArgs); |
| 1731 | // The first window gets nothing and the second gets up |
| 1732 | firstWindow->assertNoEvents(); |
| 1733 | secondWindow->consumeMotionUp(); |
| 1734 | } |
| 1735 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1736 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1737 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1738 | sp<FakeWindowHandle> window = |
| 1739 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1740 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1741 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1742 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1743 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1744 | |
| 1745 | window->consumeFocusEvent(true); |
| 1746 | |
| 1747 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1748 | mDispatcher->notifyKey(&keyArgs); |
| 1749 | |
| 1750 | // Window should receive key down event. |
| 1751 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 1752 | } |
| 1753 | |
| 1754 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1755 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1756 | sp<FakeWindowHandle> window = |
| 1757 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1758 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1759 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1760 | |
| 1761 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1762 | mDispatcher->notifyKey(&keyArgs); |
| 1763 | mDispatcher->waitForIdle(); |
| 1764 | |
| 1765 | window->assertNoEvents(); |
| 1766 | } |
| 1767 | |
| 1768 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 1769 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1770 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1771 | sp<FakeWindowHandle> window = |
| 1772 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1773 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1774 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1775 | |
| 1776 | // Send key |
| 1777 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1778 | mDispatcher->notifyKey(&keyArgs); |
| 1779 | // Send motion |
| 1780 | NotifyMotionArgs motionArgs = |
| 1781 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1782 | ADISPLAY_ID_DEFAULT); |
| 1783 | mDispatcher->notifyMotion(&motionArgs); |
| 1784 | |
| 1785 | // Window should receive only the motion event |
| 1786 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1787 | window->assertNoEvents(); // Key event or focus event will not be received |
| 1788 | } |
| 1789 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 1790 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 1791 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1792 | |
| 1793 | // Create first non touch modal window that supports split touch |
| 1794 | sp<FakeWindowHandle> firstWindow = |
| 1795 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 1796 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
| 1797 | firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 1798 | InputWindowInfo::Flag::SPLIT_TOUCH); |
| 1799 | |
| 1800 | // Create second non touch modal window that supports split touch |
| 1801 | sp<FakeWindowHandle> secondWindow = |
| 1802 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
| 1803 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
| 1804 | secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 1805 | InputWindowInfo::Flag::SPLIT_TOUCH); |
| 1806 | |
| 1807 | // Add the windows to the dispatcher |
| 1808 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 1809 | |
| 1810 | PointF pointInFirst = {300, 200}; |
| 1811 | PointF pointInSecond = {300, 600}; |
| 1812 | |
| 1813 | // Send down to the first window |
| 1814 | NotifyMotionArgs firstDownMotionArgs = |
| 1815 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1816 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 1817 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 1818 | // Only the first window should get the down event |
| 1819 | firstWindow->consumeMotionDown(); |
| 1820 | secondWindow->assertNoEvents(); |
| 1821 | |
| 1822 | // Send down to the second window |
| 1823 | NotifyMotionArgs secondDownMotionArgs = |
| 1824 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1825 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1826 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1827 | {pointInFirst, pointInSecond}); |
| 1828 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 1829 | // The first window gets a move and the second a down |
| 1830 | firstWindow->consumeMotionMove(); |
| 1831 | secondWindow->consumeMotionDown(); |
| 1832 | |
| 1833 | // Send pointer cancel to the second window |
| 1834 | NotifyMotionArgs pointerUpMotionArgs = |
| 1835 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | |
| 1836 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1837 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1838 | {pointInFirst, pointInSecond}); |
| 1839 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 1840 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 1841 | // The first window gets move and the second gets cancel. |
| 1842 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 1843 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 1844 | |
| 1845 | // Send up event. |
| 1846 | NotifyMotionArgs upMotionArgs = |
| 1847 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 1848 | ADISPLAY_ID_DEFAULT); |
| 1849 | mDispatcher->notifyMotion(&upMotionArgs); |
| 1850 | // The first window gets up and the second gets nothing. |
| 1851 | firstWindow->consumeMotionUp(); |
| 1852 | secondWindow->assertNoEvents(); |
| 1853 | } |
| 1854 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1855 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1856 | public: |
| 1857 | FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1858 | int32_t displayId, bool isGestureMonitor = false) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1859 | base::Result<std::unique_ptr<InputChannel>> channel = |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 1860 | dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1861 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1864 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 1865 | |
| 1866 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1867 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, |
| 1868 | expectedDisplayId, expectedFlags); |
| 1869 | } |
| 1870 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1871 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 1872 | |
| 1873 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 1874 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1875 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1876 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 1877 | expectedDisplayId, expectedFlags); |
| 1878 | } |
| 1879 | |
| 1880 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1881 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, |
| 1882 | expectedDisplayId, expectedFlags); |
| 1883 | } |
| 1884 | |
| 1885 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 1886 | |
| 1887 | private: |
| 1888 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1889 | }; |
| 1890 | |
| 1891 | // Tests for gesture monitors |
| 1892 | TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1893 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1894 | sp<FakeWindowHandle> window = |
| 1895 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1896 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1897 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1898 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 1899 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1900 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1901 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1902 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1903 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1904 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1905 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1909 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1910 | sp<FakeWindowHandle> window = |
| 1911 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1912 | |
| 1913 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1914 | window->setFocusable(true); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1915 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1916 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1917 | setFocusedWindow(window); |
| 1918 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1919 | window->consumeFocusEvent(true); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1920 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1921 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 1922 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1923 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1924 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 1925 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1926 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1927 | monitor.assertNoEvents(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1931 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1932 | sp<FakeWindowHandle> window = |
| 1933 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1934 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1935 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1936 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 1937 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1938 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1939 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1940 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1941 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1942 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1943 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1944 | |
| 1945 | window->releaseChannel(); |
| 1946 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1947 | mDispatcher->pilferPointers(monitor.getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1948 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1949 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1950 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1951 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1952 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1955 | TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) { |
| 1956 | FakeMonitorReceiver monitor = |
| 1957 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, |
| 1958 | true /*isGestureMonitor*/); |
| 1959 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1960 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1961 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
| 1962 | std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 1963 | ASSERT_TRUE(consumeSeq); |
| 1964 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1965 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(DISPATCHING_TIMEOUT, |
| 1966 | monitor.getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1967 | monitor.finishEvent(*consumeSeq); |
| 1968 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1969 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(monitor.getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1970 | } |
| 1971 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1972 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1973 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1974 | sp<FakeWindowHandle> window = |
| 1975 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1976 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1977 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1978 | |
| 1979 | NotifyMotionArgs motionArgs = |
| 1980 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1981 | ADISPLAY_ID_DEFAULT); |
| 1982 | |
| 1983 | mDispatcher->notifyMotion(&motionArgs); |
| 1984 | // Window should receive motion down event. |
| 1985 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1986 | |
| 1987 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1988 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1989 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1990 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 1991 | motionArgs.pointerCoords[0].getX() - 10); |
| 1992 | |
| 1993 | mDispatcher->notifyMotion(&motionArgs); |
| 1994 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
| 1995 | 0 /*expectedFlags*/); |
| 1996 | } |
| 1997 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1998 | /** |
| 1999 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 2000 | * the device default right away. In the test scenario, we check both the default value, |
| 2001 | * and the action of enabling / disabling. |
| 2002 | */ |
| 2003 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2004 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2005 | sp<FakeWindowHandle> window = |
| 2006 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 2007 | |
| 2008 | // Set focused application. |
| 2009 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2010 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2011 | |
| 2012 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2013 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2014 | setFocusedWindow(window); |
| 2015 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2016 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 2017 | |
| 2018 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2019 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2020 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2021 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); |
| 2022 | |
| 2023 | SCOPED_TRACE("Disable touch mode"); |
| 2024 | mDispatcher->setInTouchMode(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2025 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2026 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2027 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2028 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); |
| 2029 | |
| 2030 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2031 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2032 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2033 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); |
| 2034 | |
| 2035 | SCOPED_TRACE("Enable touch mode again"); |
| 2036 | mDispatcher->setInTouchMode(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2037 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2038 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2039 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2040 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 2041 | |
| 2042 | window->assertNoEvents(); |
| 2043 | } |
| 2044 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2045 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2046 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2047 | sp<FakeWindowHandle> window = |
| 2048 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 2049 | |
| 2050 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2051 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2052 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2053 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2054 | setFocusedWindow(window); |
| 2055 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2056 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 2057 | |
| 2058 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 2059 | mDispatcher->notifyKey(&keyArgs); |
| 2060 | |
| 2061 | InputEvent* event = window->consume(); |
| 2062 | ASSERT_NE(event, nullptr); |
| 2063 | |
| 2064 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 2065 | ASSERT_NE(verified, nullptr); |
| 2066 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 2067 | |
| 2068 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 2069 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 2070 | ASSERT_EQ(keyArgs.source, verified->source); |
| 2071 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 2072 | |
| 2073 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 2074 | |
| 2075 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
| 2076 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2077 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
| 2078 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 2079 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 2080 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 2081 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 2082 | } |
| 2083 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2084 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2085 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2086 | sp<FakeWindowHandle> window = |
| 2087 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 2088 | |
| 2089 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2090 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2091 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2092 | |
| 2093 | NotifyMotionArgs motionArgs = |
| 2094 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2095 | ADISPLAY_ID_DEFAULT); |
| 2096 | mDispatcher->notifyMotion(&motionArgs); |
| 2097 | |
| 2098 | InputEvent* event = window->consume(); |
| 2099 | ASSERT_NE(event, nullptr); |
| 2100 | |
| 2101 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 2102 | ASSERT_NE(verified, nullptr); |
| 2103 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 2104 | |
| 2105 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 2106 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 2107 | EXPECT_EQ(motionArgs.source, verified->source); |
| 2108 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 2109 | |
| 2110 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 2111 | |
| 2112 | EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX); |
| 2113 | EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY); |
| 2114 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
| 2115 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
| 2116 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
| 2117 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 2118 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 2119 | } |
| 2120 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2121 | TEST_F(InputDispatcherTest, NonPointerMotionEvent_JoystickNotTransformed) { |
| 2122 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2123 | sp<FakeWindowHandle> window = |
| 2124 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 2125 | const std::string name = window->getName(); |
| 2126 | |
| 2127 | // Window gets transformed by offset values. |
| 2128 | window->setWindowOffset(500.0f, 500.0f); |
| 2129 | |
| 2130 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2131 | window->setFocusable(true); |
| 2132 | |
| 2133 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2134 | |
| 2135 | // First, we set focused window so that focusedWindowHandle is not null. |
| 2136 | setFocusedWindow(window); |
| 2137 | |
| 2138 | // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked. |
| 2139 | window->consumeFocusEvent(true); |
| 2140 | |
| 2141 | NotifyMotionArgs motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
| 2142 | AINPUT_SOURCE_JOYSTICK, ADISPLAY_ID_DEFAULT); |
| 2143 | mDispatcher->notifyMotion(&motionArgs); |
| 2144 | |
| 2145 | // Third, we consume motion event. |
| 2146 | InputEvent* event = window->consume(); |
| 2147 | ASSERT_NE(event, nullptr); |
| 2148 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 2149 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 2150 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 2151 | |
| 2152 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 2153 | EXPECT_EQ(AINPUT_EVENT_TYPE_MOTION, motionEvent.getAction()); |
| 2154 | |
| 2155 | float expectedX = motionArgs.pointerCoords[0].getX(); |
| 2156 | float expectedY = motionArgs.pointerCoords[0].getY(); |
| 2157 | |
| 2158 | // Finally we test if the axis values from the final motion event are not transformed |
| 2159 | EXPECT_EQ(expectedX, motionEvent.getX(0)) << "expected " << expectedX << " for x coord of " |
| 2160 | << name.c_str() << ", got " << motionEvent.getX(0); |
| 2161 | EXPECT_EQ(expectedY, motionEvent.getY(0)) << "expected " << expectedY << " for y coord of " |
| 2162 | << name.c_str() << ", got " << motionEvent.getY(0); |
| 2163 | } |
| 2164 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2165 | /** |
| 2166 | * Ensure that separate calls to sign the same data are generating the same key. |
| 2167 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 2168 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 2169 | * tests. |
| 2170 | */ |
| 2171 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 2172 | KeyEvent event = getTestKeyEvent(); |
| 2173 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 2174 | |
| 2175 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 2176 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 2177 | ASSERT_EQ(hmac1, hmac2); |
| 2178 | } |
| 2179 | |
| 2180 | /** |
| 2181 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 2182 | */ |
| 2183 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 2184 | KeyEvent event = getTestKeyEvent(); |
| 2185 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 2186 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 2187 | |
| 2188 | verifiedEvent.deviceId += 1; |
| 2189 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2190 | |
| 2191 | verifiedEvent.source += 1; |
| 2192 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2193 | |
| 2194 | verifiedEvent.eventTimeNanos += 1; |
| 2195 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2196 | |
| 2197 | verifiedEvent.displayId += 1; |
| 2198 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2199 | |
| 2200 | verifiedEvent.action += 1; |
| 2201 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2202 | |
| 2203 | verifiedEvent.downTimeNanos += 1; |
| 2204 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2205 | |
| 2206 | verifiedEvent.flags += 1; |
| 2207 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2208 | |
| 2209 | verifiedEvent.keyCode += 1; |
| 2210 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2211 | |
| 2212 | verifiedEvent.scanCode += 1; |
| 2213 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2214 | |
| 2215 | verifiedEvent.metaState += 1; |
| 2216 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2217 | |
| 2218 | verifiedEvent.repeatCount += 1; |
| 2219 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2220 | } |
| 2221 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2222 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 2223 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2224 | sp<FakeWindowHandle> windowTop = |
| 2225 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 2226 | sp<FakeWindowHandle> windowSecond = |
| 2227 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2228 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2229 | |
| 2230 | // Top window is also focusable but is not granted focus. |
| 2231 | windowTop->setFocusable(true); |
| 2232 | windowSecond->setFocusable(true); |
| 2233 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 2234 | setFocusedWindow(windowSecond); |
| 2235 | |
| 2236 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2237 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 2238 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2239 | |
| 2240 | // Focused window should receive event. |
| 2241 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 2242 | windowTop->assertNoEvents(); |
| 2243 | } |
| 2244 | |
| 2245 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 2246 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2247 | sp<FakeWindowHandle> window = |
| 2248 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 2249 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2250 | |
| 2251 | window->setFocusable(true); |
| 2252 | // Release channel for window is no longer valid. |
| 2253 | window->releaseChannel(); |
| 2254 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2255 | setFocusedWindow(window); |
| 2256 | |
| 2257 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2258 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 2259 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2260 | |
| 2261 | // window channel is invalid, so it should not receive any input event. |
| 2262 | window->assertNoEvents(); |
| 2263 | } |
| 2264 | |
| 2265 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 2266 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2267 | sp<FakeWindowHandle> window = |
| 2268 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 2269 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2270 | |
| 2271 | // Window is not focusable. |
| 2272 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2273 | setFocusedWindow(window); |
| 2274 | |
| 2275 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2276 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 2277 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2278 | |
| 2279 | // window is invalid, so it should not receive any input event. |
| 2280 | window->assertNoEvents(); |
| 2281 | } |
| 2282 | |
| 2283 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 2284 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2285 | sp<FakeWindowHandle> windowTop = |
| 2286 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 2287 | sp<FakeWindowHandle> windowSecond = |
| 2288 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2289 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2290 | |
| 2291 | windowTop->setFocusable(true); |
| 2292 | windowSecond->setFocusable(true); |
| 2293 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 2294 | setFocusedWindow(windowTop); |
| 2295 | windowTop->consumeFocusEvent(true); |
| 2296 | |
| 2297 | setFocusedWindow(windowSecond, windowTop); |
| 2298 | windowSecond->consumeFocusEvent(true); |
| 2299 | windowTop->consumeFocusEvent(false); |
| 2300 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2301 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 2302 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2303 | |
| 2304 | // Focused window should receive event. |
| 2305 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 2306 | } |
| 2307 | |
| 2308 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { |
| 2309 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2310 | sp<FakeWindowHandle> windowTop = |
| 2311 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 2312 | sp<FakeWindowHandle> windowSecond = |
| 2313 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2314 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2315 | |
| 2316 | windowTop->setFocusable(true); |
| 2317 | windowSecond->setFocusable(true); |
| 2318 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 2319 | setFocusedWindow(windowSecond, windowTop); |
| 2320 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2321 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 2322 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2323 | |
| 2324 | // Event should be dropped. |
| 2325 | windowTop->assertNoEvents(); |
| 2326 | windowSecond->assertNoEvents(); |
| 2327 | } |
| 2328 | |
| 2329 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 2330 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2331 | sp<FakeWindowHandle> window = |
| 2332 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 2333 | sp<FakeWindowHandle> previousFocusedWindow = |
| 2334 | new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow", |
| 2335 | ADISPLAY_ID_DEFAULT); |
| 2336 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2337 | |
| 2338 | window->setFocusable(true); |
| 2339 | previousFocusedWindow->setFocusable(true); |
| 2340 | window->setVisible(false); |
| 2341 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 2342 | setFocusedWindow(previousFocusedWindow); |
| 2343 | previousFocusedWindow->consumeFocusEvent(true); |
| 2344 | |
| 2345 | // Requesting focus on invisible window takes focus from currently focused window. |
| 2346 | setFocusedWindow(window); |
| 2347 | previousFocusedWindow->consumeFocusEvent(false); |
| 2348 | |
| 2349 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2350 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2351 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2352 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2353 | |
| 2354 | // Window does not get focus event or key down. |
| 2355 | window->assertNoEvents(); |
| 2356 | |
| 2357 | // Window becomes visible. |
| 2358 | window->setVisible(true); |
| 2359 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2360 | |
| 2361 | // Window receives focus event. |
| 2362 | window->consumeFocusEvent(true); |
| 2363 | // Focused window receives key down. |
| 2364 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2365 | } |
| 2366 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2367 | /** |
| 2368 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 2369 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 2370 | * of the 'slipperyEnterWindow'. |
| 2371 | * |
| 2372 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 2373 | * a way so that the touched location is no longer covered by the top window. |
| 2374 | * |
| 2375 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 2376 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 2377 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 2378 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 2379 | * with ACTION_DOWN). |
| 2380 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 2381 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 2382 | * |
| 2383 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 2384 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 2385 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 2386 | * |
| 2387 | * In this test, we ensure that the event received by the bottom window has |
| 2388 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 2389 | */ |
| 2390 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
| 2391 | constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1; |
| 2392 | constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1; |
| 2393 | |
| 2394 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2395 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2396 | |
| 2397 | sp<FakeWindowHandle> slipperyExitWindow = |
| 2398 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 2399 | slipperyExitWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 2400 | InputWindowInfo::Flag::SLIPPERY); |
| 2401 | // Make sure this one overlaps the bottom window |
| 2402 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 2403 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 2404 | // one. Windows with the same owner are not considered to be occluding each other. |
| 2405 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 2406 | |
| 2407 | sp<FakeWindowHandle> slipperyEnterWindow = |
| 2408 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2409 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 2410 | |
| 2411 | mDispatcher->setInputWindows( |
| 2412 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 2413 | |
| 2414 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
| 2415 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2416 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 2417 | mDispatcher->notifyMotion(&args); |
| 2418 | slipperyExitWindow->consumeMotionDown(); |
| 2419 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 2420 | mDispatcher->setInputWindows( |
| 2421 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 2422 | |
| 2423 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2424 | ADISPLAY_ID_DEFAULT, {{51, 51}}); |
| 2425 | mDispatcher->notifyMotion(&args); |
| 2426 | |
| 2427 | slipperyExitWindow->consumeMotionCancel(); |
| 2428 | |
| 2429 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 2430 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 2431 | } |
| 2432 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2433 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 2434 | protected: |
| 2435 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 2436 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 2437 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2438 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2439 | sp<FakeWindowHandle> mWindow; |
| 2440 | |
| 2441 | virtual void SetUp() override { |
| 2442 | mFakePolicy = new FakeInputDispatcherPolicy(); |
| 2443 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
| 2444 | mDispatcher = new InputDispatcher(mFakePolicy); |
| 2445 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 2446 | ASSERT_EQ(OK, mDispatcher->start()); |
| 2447 | |
| 2448 | setUpWindow(); |
| 2449 | } |
| 2450 | |
| 2451 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2452 | mApp = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2453 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2454 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2455 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2456 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2457 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2458 | mWindow->consumeFocusEvent(true); |
| 2459 | } |
| 2460 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2461 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2462 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2463 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2464 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
| 2465 | mDispatcher->notifyKey(&keyArgs); |
| 2466 | |
| 2467 | // Window should receive key down event. |
| 2468 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2469 | } |
| 2470 | |
| 2471 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 2472 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 2473 | InputEvent* repeatEvent = mWindow->consume(); |
| 2474 | ASSERT_NE(nullptr, repeatEvent); |
| 2475 | |
| 2476 | uint32_t eventType = repeatEvent->getType(); |
| 2477 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); |
| 2478 | |
| 2479 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 2480 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 2481 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 2482 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 2483 | } |
| 2484 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2485 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2486 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2487 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2488 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
| 2489 | mDispatcher->notifyKey(&keyArgs); |
| 2490 | |
| 2491 | // Window should receive key down event. |
| 2492 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 2493 | 0 /*expectedFlags*/); |
| 2494 | } |
| 2495 | }; |
| 2496 | |
| 2497 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2498 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 2499 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2500 | expectKeyRepeatOnce(repeatCount); |
| 2501 | } |
| 2502 | } |
| 2503 | |
| 2504 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
| 2505 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 2506 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2507 | expectKeyRepeatOnce(repeatCount); |
| 2508 | } |
| 2509 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 2510 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2511 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2512 | expectKeyRepeatOnce(repeatCount); |
| 2513 | } |
| 2514 | } |
| 2515 | |
| 2516 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2517 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2518 | expectKeyRepeatOnce(1 /*repeatCount*/); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2519 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 2520 | mWindow->assertNoEvents(); |
| 2521 | } |
| 2522 | |
| 2523 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
| 2524 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 2525 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 2526 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 2527 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 2528 | // Stale key up from device 1. |
| 2529 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 2530 | // Device 2 is still down, keep repeating |
| 2531 | expectKeyRepeatOnce(2 /*repeatCount*/); |
| 2532 | expectKeyRepeatOnce(3 /*repeatCount*/); |
| 2533 | // Device 2 key up |
| 2534 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 2535 | mWindow->assertNoEvents(); |
| 2536 | } |
| 2537 | |
| 2538 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
| 2539 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 2540 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 2541 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 2542 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 2543 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. |
| 2544 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 2545 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2546 | mWindow->assertNoEvents(); |
| 2547 | } |
| 2548 | |
| 2549 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2550 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2551 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2552 | InputEvent* repeatEvent = mWindow->consume(); |
| 2553 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 2554 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 2555 | IdGenerator::getSource(repeatEvent->getId())); |
| 2556 | } |
| 2557 | } |
| 2558 | |
| 2559 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2560 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2561 | |
| 2562 | std::unordered_set<int32_t> idSet; |
| 2563 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2564 | InputEvent* repeatEvent = mWindow->consume(); |
| 2565 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 2566 | int32_t id = repeatEvent->getId(); |
| 2567 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 2568 | idSet.insert(id); |
| 2569 | } |
| 2570 | } |
| 2571 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2572 | /* Test InputDispatcher for MultiDisplay */ |
| 2573 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 2574 | public: |
| 2575 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2576 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2577 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2578 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2579 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2580 | windowInPrimary = |
| 2581 | new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2582 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2583 | // Set focus window for primary display, but focused display would be second one. |
| 2584 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2585 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2586 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2587 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2588 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2589 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2590 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2591 | windowInSecondary = |
| 2592 | new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2593 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2594 | // Set focus display to second one. |
| 2595 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 2596 | // Set focus window for second display. |
| 2597 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2598 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2599 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2600 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2601 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2602 | } |
| 2603 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2604 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2605 | InputDispatcherTest::TearDown(); |
| 2606 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2607 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2608 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2609 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2610 | windowInSecondary.clear(); |
| 2611 | } |
| 2612 | |
| 2613 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2614 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2615 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2616 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2617 | sp<FakeWindowHandle> windowInSecondary; |
| 2618 | }; |
| 2619 | |
| 2620 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 2621 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2622 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2623 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 2624 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2625 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2626 | windowInSecondary->assertNoEvents(); |
| 2627 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2628 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2629 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2630 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 2631 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2632 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2633 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2634 | } |
| 2635 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2636 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2637 | // Test inject a key down with display id specified. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2638 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 2639 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2640 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2641 | windowInSecondary->assertNoEvents(); |
| 2642 | |
| 2643 | // Test inject a key down without display id specified. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2644 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 2645 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2646 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2647 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2648 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2649 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2650 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2651 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2652 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2653 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
| 2654 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2655 | |
| 2656 | // Test inject a key down, should timeout because of no target window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2657 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 2658 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2659 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2660 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2661 | windowInSecondary->assertNoEvents(); |
| 2662 | } |
| 2663 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2664 | // Test per-display input monitors for motion event. |
| 2665 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2666 | FakeMonitorReceiver monitorInPrimary = |
| 2667 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 2668 | FakeMonitorReceiver monitorInSecondary = |
| 2669 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2670 | |
| 2671 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2672 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2673 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 2674 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2675 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2676 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2677 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2678 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2679 | |
| 2680 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2681 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2682 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 2683 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2684 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2685 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2686 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2687 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2688 | |
| 2689 | // Test inject a non-pointer motion event. |
| 2690 | // If specific a display, it will dispatch to the focused window of particular display, |
| 2691 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2692 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2693 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 2694 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2695 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2696 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2697 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2698 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | // Test per-display input monitors for key event. |
| 2702 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2703 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2704 | FakeMonitorReceiver monitorInPrimary = |
| 2705 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 2706 | FakeMonitorReceiver monitorInSecondary = |
| 2707 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2708 | |
| 2709 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2710 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 2711 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2712 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2713 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2714 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2715 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2716 | } |
| 2717 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2718 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 2719 | sp<FakeWindowHandle> secondWindowInPrimary = |
| 2720 | new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
| 2721 | secondWindowInPrimary->setFocusable(true); |
| 2722 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 2723 | setFocusedWindow(secondWindowInPrimary); |
| 2724 | windowInPrimary->consumeFocusEvent(false); |
| 2725 | secondWindowInPrimary->consumeFocusEvent(true); |
| 2726 | |
| 2727 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2728 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 2729 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2730 | windowInPrimary->assertNoEvents(); |
| 2731 | windowInSecondary->assertNoEvents(); |
| 2732 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2733 | } |
| 2734 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2735 | class InputFilterTest : public InputDispatcherTest { |
| 2736 | protected: |
| 2737 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
| 2738 | |
| 2739 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) { |
| 2740 | NotifyMotionArgs motionArgs; |
| 2741 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2742 | motionArgs = |
| 2743 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2744 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2745 | motionArgs = |
| 2746 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2747 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2748 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2749 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 2750 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2751 | } else { |
| 2752 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 2753 | } |
| 2754 | } |
| 2755 | |
| 2756 | void testNotifyKey(bool expectToBeFiltered) { |
| 2757 | NotifyKeyArgs keyArgs; |
| 2758 | |
| 2759 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 2760 | mDispatcher->notifyKey(&keyArgs); |
| 2761 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 2762 | mDispatcher->notifyKey(&keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2763 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2764 | |
| 2765 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 2766 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2767 | } else { |
| 2768 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 2769 | } |
| 2770 | } |
| 2771 | }; |
| 2772 | |
| 2773 | // Test InputFilter for MotionEvent |
| 2774 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 2775 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 2776 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 2777 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 2778 | |
| 2779 | // Enable InputFilter |
| 2780 | mDispatcher->setInputFilterEnabled(true); |
| 2781 | // Test touch on both primary and second display, and check if both events are filtered. |
| 2782 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 2783 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 2784 | |
| 2785 | // Disable InputFilter |
| 2786 | mDispatcher->setInputFilterEnabled(false); |
| 2787 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 2788 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 2789 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 2790 | } |
| 2791 | |
| 2792 | // Test InputFilter for KeyEvent |
| 2793 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 2794 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 2795 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 2796 | |
| 2797 | // Enable InputFilter |
| 2798 | mDispatcher->setInputFilterEnabled(true); |
| 2799 | // Send a key event, and check if it is filtered. |
| 2800 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 2801 | |
| 2802 | // Disable InputFilter |
| 2803 | mDispatcher->setInputFilterEnabled(false); |
| 2804 | // Send a key event, and check if it isn't filtered. |
| 2805 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 2806 | } |
| 2807 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2808 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2809 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2810 | InputDispatcherTest::SetUp(); |
| 2811 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2812 | std::shared_ptr<FakeApplicationHandle> application = |
| 2813 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2814 | mUnfocusedWindow = |
| 2815 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2816 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
| 2817 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 2818 | // window. |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2819 | mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2820 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2821 | mFocusedWindow = |
| 2822 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2823 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2824 | mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2825 | |
| 2826 | // Set focused application. |
| 2827 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2828 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2829 | |
| 2830 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2831 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2832 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2833 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2834 | } |
| 2835 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2836 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2837 | InputDispatcherTest::TearDown(); |
| 2838 | |
| 2839 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2840 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2841 | } |
| 2842 | |
| 2843 | protected: |
| 2844 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2845 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 2846 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2847 | }; |
| 2848 | |
| 2849 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 2850 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 2851 | // the onPointerDownOutsideFocus callback. |
| 2852 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2853 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 2854 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2855 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2856 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 2857 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2858 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2859 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2860 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 2861 | } |
| 2862 | |
| 2863 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 2864 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 2865 | // onPointerDownOutsideFocus callback. |
| 2866 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2867 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 2868 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2869 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 2870 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2871 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2872 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 2873 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2874 | } |
| 2875 | |
| 2876 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 2877 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 2878 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2879 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 2880 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 2881 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2882 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2883 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 2884 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2885 | } |
| 2886 | |
| 2887 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 2888 | // DOWN on the window that already has focus. Ensure no window received the |
| 2889 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2890 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2891 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2892 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 2893 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2894 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 2895 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2896 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2897 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 2898 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2899 | } |
| 2900 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2901 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 2902 | // windows that point to the same client token. |
| 2903 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 2904 | virtual void SetUp() override { |
| 2905 | InputDispatcherTest::SetUp(); |
| 2906 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2907 | std::shared_ptr<FakeApplicationHandle> application = |
| 2908 | std::make_shared<FakeApplicationHandle>(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2909 | mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1", |
| 2910 | ADISPLAY_ID_DEFAULT); |
| 2911 | // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window. |
| 2912 | // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows. |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2913 | mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 2914 | InputWindowInfo::Flag::SPLIT_TOUCH); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2915 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 2916 | |
| 2917 | mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2", |
| 2918 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2919 | mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 2920 | InputWindowInfo::Flag::SPLIT_TOUCH); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2921 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 2922 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2923 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2924 | } |
| 2925 | |
| 2926 | protected: |
| 2927 | sp<FakeWindowHandle> mWindow1; |
| 2928 | sp<FakeWindowHandle> mWindow2; |
| 2929 | |
| 2930 | // Helper function to convert the point from screen coordinates into the window's space |
| 2931 | static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2932 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 2933 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2934 | } |
| 2935 | |
| 2936 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 2937 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2938 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2939 | InputEvent* event = window->consume(); |
| 2940 | |
| 2941 | ASSERT_NE(nullptr, event) << name.c_str() |
| 2942 | << ": consumer should have returned non-NULL event."; |
| 2943 | |
| 2944 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 2945 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 2946 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 2947 | |
| 2948 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 2949 | EXPECT_EQ(expectedAction, motionEvent.getAction()); |
| 2950 | |
| 2951 | for (size_t i = 0; i < points.size(); i++) { |
| 2952 | float expectedX = points[i].x; |
| 2953 | float expectedY = points[i].y; |
| 2954 | |
| 2955 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 2956 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 2957 | << ", got " << motionEvent.getX(i); |
| 2958 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 2959 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 2960 | << ", got " << motionEvent.getY(i); |
| 2961 | } |
| 2962 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2963 | |
| 2964 | void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints, |
| 2965 | std::vector<PointF> expectedPoints) { |
| 2966 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 2967 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2968 | mDispatcher->notifyMotion(&motionArgs); |
| 2969 | |
| 2970 | // Always consume from window1 since it's the window that has the InputReceiver |
| 2971 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 2972 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2973 | }; |
| 2974 | |
| 2975 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 2976 | // Touch Window 1 |
| 2977 | PointF touchedPoint = {10, 10}; |
| 2978 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2979 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2980 | |
| 2981 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2982 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2983 | |
| 2984 | // Touch Window 2 |
| 2985 | touchedPoint = {150, 150}; |
| 2986 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2987 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2988 | } |
| 2989 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2990 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 2991 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2992 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 2993 | |
| 2994 | // Touch Window 1 |
| 2995 | PointF touchedPoint = {10, 10}; |
| 2996 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2997 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2998 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2999 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3000 | |
| 3001 | // Touch Window 2 |
| 3002 | touchedPoint = {150, 150}; |
| 3003 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3004 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 3005 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3006 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3007 | // Update the transform so rotation is set |
| 3008 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 3009 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 3010 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3011 | } |
| 3012 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3013 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3014 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 3015 | |
| 3016 | // Touch Window 1 |
| 3017 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 3018 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3019 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3020 | |
| 3021 | // Touch Window 2 |
| 3022 | int32_t actionPointerDown = |
| 3023 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3024 | touchedPoints.push_back(PointF{150, 150}); |
| 3025 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 3026 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3027 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3028 | // Release Window 2 |
| 3029 | int32_t actionPointerUp = |
| 3030 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 3031 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); |
| 3032 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3033 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3034 | // Update the transform so rotation is set for Window 2 |
| 3035 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 3036 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 3037 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3038 | } |
| 3039 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3040 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3041 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 3042 | |
| 3043 | // Touch Window 1 |
| 3044 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 3045 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3046 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3047 | |
| 3048 | // Touch Window 2 |
| 3049 | int32_t actionPointerDown = |
| 3050 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3051 | touchedPoints.push_back(PointF{150, 150}); |
| 3052 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3053 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3054 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3055 | |
| 3056 | // Move both windows |
| 3057 | touchedPoints = {{20, 20}, {175, 175}}; |
| 3058 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 3059 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 3060 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3061 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3062 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3063 | // Release Window 2 |
| 3064 | int32_t actionPointerUp = |
| 3065 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 3066 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); |
| 3067 | expectedPoints.pop_back(); |
| 3068 | |
| 3069 | // Touch Window 2 |
| 3070 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 3071 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 3072 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
| 3073 | |
| 3074 | // Move both windows |
| 3075 | touchedPoints = {{20, 20}, {175, 175}}; |
| 3076 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 3077 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 3078 | |
| 3079 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 3083 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 3084 | |
| 3085 | // Touch Window 1 |
| 3086 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 3087 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3088 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3089 | |
| 3090 | // Touch Window 2 |
| 3091 | int32_t actionPointerDown = |
| 3092 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3093 | touchedPoints.push_back(PointF{150, 150}); |
| 3094 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3095 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3096 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3097 | |
| 3098 | // Move both windows |
| 3099 | touchedPoints = {{20, 20}, {175, 175}}; |
| 3100 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 3101 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 3102 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3103 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3106 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 3107 | virtual void SetUp() override { |
| 3108 | InputDispatcherTest::SetUp(); |
| 3109 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3110 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3111 | mApplication->setDispatchingTimeout(20ms); |
| 3112 | mWindow = |
| 3113 | new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 3114 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 3115 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3116 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3117 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 3118 | // window. |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3119 | mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3120 | |
| 3121 | // Set focused application. |
| 3122 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 3123 | |
| 3124 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3125 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3126 | mWindow->consumeFocusEvent(true); |
| 3127 | } |
| 3128 | |
| 3129 | virtual void TearDown() override { |
| 3130 | InputDispatcherTest::TearDown(); |
| 3131 | mWindow.clear(); |
| 3132 | } |
| 3133 | |
| 3134 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3135 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3136 | sp<FakeWindowHandle> mWindow; |
| 3137 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 3138 | |
| 3139 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3140 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3141 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3142 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3143 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3144 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3145 | WINDOW_LOCATION)); |
| 3146 | } |
| 3147 | }; |
| 3148 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3149 | // Send a tap and respond, which should not cause an ANR. |
| 3150 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 3151 | tapOnWindow(); |
| 3152 | mWindow->consumeMotionDown(); |
| 3153 | mWindow->consumeMotionUp(); |
| 3154 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3155 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3156 | } |
| 3157 | |
| 3158 | // Send a regular key and respond, which should not cause an ANR. |
| 3159 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3160 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3161 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 3162 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3163 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3164 | } |
| 3165 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3166 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 3167 | mWindow->setFocusable(false); |
| 3168 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3169 | mWindow->consumeFocusEvent(false); |
| 3170 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3171 | InputEventInjectionResult result = |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3172 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3173 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 3174 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3175 | // Key will not go to window because we have no focused window. |
| 3176 | // The 'no focused window' ANR timer should start instead. |
| 3177 | |
| 3178 | // Now, the focused application goes away. |
| 3179 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 3180 | // The key should get dropped and there should be no ANR. |
| 3181 | |
| 3182 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3183 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3184 | } |
| 3185 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3186 | // 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] | 3187 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 3188 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3189 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3190 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3191 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3192 | WINDOW_LOCATION)); |
| 3193 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3194 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 3195 | ASSERT_TRUE(sequenceNum); |
| 3196 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3197 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3198 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3199 | mWindow->finishEvent(*sequenceNum); |
| 3200 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 3201 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3202 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3203 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | // Send a key to the app and have the app not respond right away. |
| 3207 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 3208 | // Inject a key, and don't respond - expect that ANR is called. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3209 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3210 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 3211 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3212 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3213 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3214 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3215 | } |
| 3216 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3217 | // We have a focused application, but no focused window |
| 3218 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3219 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3220 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3221 | mWindow->consumeFocusEvent(false); |
| 3222 | |
| 3223 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3224 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3225 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3226 | WINDOW_LOCATION)); |
| 3227 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 3228 | mDispatcher->waitForIdle(); |
| 3229 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3230 | |
| 3231 | // Once a focused event arrives, we get an ANR for this application |
| 3232 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 3233 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3234 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3235 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3236 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 3237 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3238 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3239 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3240 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3241 | } |
| 3242 | |
| 3243 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3244 | // Make sure that we don't notify policy twice about the same ANR. |
| 3245 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3246 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3247 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3248 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3249 | |
| 3250 | // Once a focused event arrives, we get an ANR for this application |
| 3251 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 3252 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3253 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3254 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3255 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 3256 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3257 | const std::chrono::duration appTimeout = |
| 3258 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3259 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3260 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3261 | std::this_thread::sleep_for(appTimeout); |
| 3262 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 3263 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3264 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3265 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 3266 | // '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] | 3267 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3268 | } |
| 3269 | |
| 3270 | // We have a focused application, but no focused window |
| 3271 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3272 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3273 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3274 | mWindow->consumeFocusEvent(false); |
| 3275 | |
| 3276 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3277 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3278 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3279 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 3280 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3281 | |
| 3282 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3283 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3284 | |
| 3285 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3286 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3287 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3288 | mWindow->assertNoEvents(); |
| 3289 | } |
| 3290 | |
| 3291 | /** |
| 3292 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 3293 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 3294 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 3295 | * the ANR mechanism should still work. |
| 3296 | * |
| 3297 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 3298 | * DOWN event, while not responding on the second one. |
| 3299 | */ |
| 3300 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 3301 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 3302 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3303 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 3304 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 3305 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3306 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3307 | |
| 3308 | // Now send ACTION_UP, with identical timestamp |
| 3309 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3310 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 3311 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 3312 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3313 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3314 | |
| 3315 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 3316 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3317 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3318 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3319 | } |
| 3320 | |
| 3321 | // If an app is not responding to a key event, gesture monitors should continue to receive |
| 3322 | // new motion events |
| 3323 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) { |
| 3324 | FakeMonitorReceiver monitor = |
| 3325 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, |
| 3326 | true /*isGestureMonitor*/); |
| 3327 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3328 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3329 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3330 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3331 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3332 | |
| 3333 | // Stuck on the ACTION_UP |
| 3334 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3335 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3336 | |
| 3337 | // New tap will go to the gesture monitor, but not to the window |
| 3338 | tapOnWindow(); |
| 3339 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3340 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3341 | |
| 3342 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 3343 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3344 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3345 | mWindow->assertNoEvents(); |
| 3346 | monitor.assertNoEvents(); |
| 3347 | } |
| 3348 | |
| 3349 | // If an app is not responding to a motion event, gesture monitors should continue to receive |
| 3350 | // new motion events |
| 3351 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) { |
| 3352 | FakeMonitorReceiver monitor = |
| 3353 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, |
| 3354 | true /*isGestureMonitor*/); |
| 3355 | |
| 3356 | tapOnWindow(); |
| 3357 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3358 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3359 | |
| 3360 | mWindow->consumeMotionDown(); |
| 3361 | // Stuck on the ACTION_UP |
| 3362 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3363 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3364 | |
| 3365 | // New tap will go to the gesture monitor, but not to the window |
| 3366 | tapOnWindow(); |
| 3367 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3368 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3369 | |
| 3370 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 3371 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3372 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3373 | mWindow->assertNoEvents(); |
| 3374 | monitor.assertNoEvents(); |
| 3375 | } |
| 3376 | |
| 3377 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 3378 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 3379 | // get an ANR again. |
| 3380 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 3381 | // 2. consume all pending events (= queue becomes healthy again) |
| 3382 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 3383 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 3384 | tapOnWindow(); |
| 3385 | |
| 3386 | mWindow->consumeMotionDown(); |
| 3387 | // Block on ACTION_UP |
| 3388 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3389 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3390 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 3391 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3392 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3393 | mWindow->assertNoEvents(); |
| 3394 | |
| 3395 | tapOnWindow(); |
| 3396 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3397 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3398 | mWindow->consumeMotionUp(); |
| 3399 | |
| 3400 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3401 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken()); |
| 3402 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3403 | mWindow->assertNoEvents(); |
| 3404 | } |
| 3405 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3406 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 3407 | // it. |
| 3408 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3409 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3410 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3411 | WINDOW_LOCATION)); |
| 3412 | |
| 3413 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3414 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(windowTimeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3415 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3416 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 3417 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3418 | // 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] | 3419 | mWindow->consumeMotionDown(); |
| 3420 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 3421 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 3422 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3423 | mDispatcher->waitForIdle(); |
| 3424 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken()); |
| 3425 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3426 | } |
| 3427 | |
| 3428 | /** |
| 3429 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 3430 | * not to to the focused window until the motion is processed. |
| 3431 | * |
| 3432 | * Warning!!! |
| 3433 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 3434 | * and the injection timeout that we specify when injecting the key. |
| 3435 | * We must have the injection timeout (10ms) be smaller than |
| 3436 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 3437 | * |
| 3438 | * If that value changes, this test should also change. |
| 3439 | */ |
| 3440 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 3441 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 3442 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3443 | |
| 3444 | tapOnWindow(); |
| 3445 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 3446 | ASSERT_TRUE(downSequenceNum); |
| 3447 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 3448 | ASSERT_TRUE(upSequenceNum); |
| 3449 | // Don't finish the events yet, and send a key |
| 3450 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 3451 | // window even if motions are still being processed. But because the injection timeout is short, |
| 3452 | // we will receive INJECTION_TIMED_OUT as the result. |
| 3453 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3454 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3455 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3456 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 3457 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3458 | // Key will not be sent to the window, yet, because the window is still processing events |
| 3459 | // and the key remains pending, waiting for the touch events to be processed |
| 3460 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 3461 | ASSERT_FALSE(keySequenceNum); |
| 3462 | |
| 3463 | std::this_thread::sleep_for(500ms); |
| 3464 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 3465 | // to the focused window, even though we have not yet finished the motion event |
| 3466 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3467 | mWindow->finishEvent(*downSequenceNum); |
| 3468 | mWindow->finishEvent(*upSequenceNum); |
| 3469 | } |
| 3470 | |
| 3471 | /** |
| 3472 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 3473 | * not go to the focused window until the motion is processed. |
| 3474 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 3475 | * focused window right away. |
| 3476 | */ |
| 3477 | TEST_F(InputDispatcherSingleWindowAnr, |
| 3478 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 3479 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 3480 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3481 | |
| 3482 | tapOnWindow(); |
| 3483 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 3484 | ASSERT_TRUE(downSequenceNum); |
| 3485 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 3486 | ASSERT_TRUE(upSequenceNum); |
| 3487 | // Don't finish the events yet, and send a key |
| 3488 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3489 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3490 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3491 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3492 | // At this point, key is still pending, and should not be sent to the application yet. |
| 3493 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 3494 | ASSERT_FALSE(keySequenceNum); |
| 3495 | |
| 3496 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 3497 | tapOnWindow(); |
| 3498 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 3499 | // the other events yet. We can finish events in any order. |
| 3500 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 3501 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 3502 | mWindow->consumeMotionDown(); |
| 3503 | mWindow->consumeMotionUp(); |
| 3504 | mWindow->assertNoEvents(); |
| 3505 | } |
| 3506 | |
| 3507 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 3508 | virtual void SetUp() override { |
| 3509 | InputDispatcherTest::SetUp(); |
| 3510 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3511 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3512 | mApplication->setDispatchingTimeout(10ms); |
| 3513 | mUnfocusedWindow = |
| 3514 | new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT); |
| 3515 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
| 3516 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 3517 | // window. |
| 3518 | // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3519 | mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 3520 | InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH | |
| 3521 | InputWindowInfo::Flag::SPLIT_TOUCH); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3522 | |
| 3523 | mFocusedWindow = |
| 3524 | new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 3525 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3526 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3527 | mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 3528 | InputWindowInfo::Flag::SPLIT_TOUCH); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3529 | |
| 3530 | // Set focused application. |
| 3531 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3532 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3533 | |
| 3534 | // Expect one focus window exist in display. |
| 3535 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3536 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3537 | mFocusedWindow->consumeFocusEvent(true); |
| 3538 | } |
| 3539 | |
| 3540 | virtual void TearDown() override { |
| 3541 | InputDispatcherTest::TearDown(); |
| 3542 | |
| 3543 | mUnfocusedWindow.clear(); |
| 3544 | mFocusedWindow.clear(); |
| 3545 | } |
| 3546 | |
| 3547 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3548 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3549 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 3550 | sp<FakeWindowHandle> mFocusedWindow; |
| 3551 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 3552 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 3553 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 3554 | |
| 3555 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 3556 | |
| 3557 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 3558 | |
| 3559 | private: |
| 3560 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3561 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3562 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3563 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3564 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3565 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3566 | location)); |
| 3567 | } |
| 3568 | }; |
| 3569 | |
| 3570 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 3571 | // should be ANR'd first. |
| 3572 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3573 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3574 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3575 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3576 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3577 | mFocusedWindow->consumeMotionDown(); |
| 3578 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 3579 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 3580 | // We consumed all events, so no ANR |
| 3581 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3582 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3583 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3584 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3585 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3586 | FOCUSED_WINDOW_LOCATION)); |
| 3587 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 3588 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3589 | |
| 3590 | const std::chrono::duration timeout = |
| 3591 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3592 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); |
| 3593 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 3594 | // sequence to make it consistent |
| 3595 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3596 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3597 | mFocusedWindow->consumeMotionDown(); |
| 3598 | // This cancel is generated because the connection was unresponsive |
| 3599 | mFocusedWindow->consumeMotionCancel(); |
| 3600 | mFocusedWindow->assertNoEvents(); |
| 3601 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3602 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3603 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mFocusedWindow->getToken()); |
| 3604 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3605 | } |
| 3606 | |
| 3607 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 3608 | // it doesn't matter which order they should have ANR. |
| 3609 | // But we should receive ANR for both. |
| 3610 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 3611 | // Set the timeout for unfocused window to match the focused window |
| 3612 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 3613 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 3614 | |
| 3615 | tapOnFocusedWindow(); |
| 3616 | // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3617 | sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveConnectionToken(10ms); |
| 3618 | sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveConnectionToken(0ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3619 | |
| 3620 | // 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] | 3621 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 3622 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 3623 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 3624 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3625 | |
| 3626 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3627 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3628 | |
| 3629 | mFocusedWindow->consumeMotionDown(); |
| 3630 | mFocusedWindow->consumeMotionUp(); |
| 3631 | mUnfocusedWindow->consumeMotionOutside(); |
| 3632 | |
| 3633 | sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveConnectionToken(); |
| 3634 | sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveConnectionToken(); |
| 3635 | |
| 3636 | // Both applications should be marked as responsive, in any order |
| 3637 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 3638 | mFocusedWindow->getToken() == responsiveToken2); |
| 3639 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 3640 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 3641 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3642 | } |
| 3643 | |
| 3644 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 3645 | // We should also log an error to account for the dropped event (not tested here). |
| 3646 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 3647 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 3648 | tapOnFocusedWindow(); |
| 3649 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 3650 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 3651 | // Receive the events, but don't respond |
| 3652 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 3653 | ASSERT_TRUE(downEventSequenceNum); |
| 3654 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 3655 | ASSERT_TRUE(upEventSequenceNum); |
| 3656 | const std::chrono::duration timeout = |
| 3657 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3658 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3659 | |
| 3660 | // Tap once again |
| 3661 | // 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] | 3662 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3663 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3664 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3665 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3666 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3667 | FOCUSED_WINDOW_LOCATION)); |
| 3668 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 3669 | // valid touch target |
| 3670 | mUnfocusedWindow->assertNoEvents(); |
| 3671 | |
| 3672 | // Consume the first tap |
| 3673 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 3674 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 3675 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3676 | // The second tap did not go to the focused window |
| 3677 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3678 | // Since all events are finished, connection should be deemed healthy again |
| 3679 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mFocusedWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3680 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3681 | } |
| 3682 | |
| 3683 | // If you tap outside of all windows, there will not be ANR |
| 3684 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3685 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3686 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3687 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 3688 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3689 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3690 | } |
| 3691 | |
| 3692 | // Since the focused window is paused, tapping on it should not produce any events |
| 3693 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 3694 | mFocusedWindow->setPaused(true); |
| 3695 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 3696 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3697 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3698 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3699 | FOCUSED_WINDOW_LOCATION)); |
| 3700 | |
| 3701 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 3702 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3703 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 3704 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3705 | |
| 3706 | mFocusedWindow->assertNoEvents(); |
| 3707 | mUnfocusedWindow->assertNoEvents(); |
| 3708 | } |
| 3709 | |
| 3710 | /** |
| 3711 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 3712 | * not to to the focused window until the motion is processed. |
| 3713 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 3714 | * |
| 3715 | * Warning!!! |
| 3716 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 3717 | * and the injection timeout that we specify when injecting the key. |
| 3718 | * We must have the injection timeout (10ms) be smaller than |
| 3719 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 3720 | * |
| 3721 | * If that value changes, this test should also change. |
| 3722 | */ |
| 3723 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 3724 | // Set a long ANR timeout to prevent it from triggering |
| 3725 | mFocusedWindow->setDispatchingTimeout(2s); |
| 3726 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 3727 | |
| 3728 | tapOnUnfocusedWindow(); |
| 3729 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 3730 | ASSERT_TRUE(downSequenceNum); |
| 3731 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 3732 | ASSERT_TRUE(upSequenceNum); |
| 3733 | // Don't finish the events yet, and send a key |
| 3734 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 3735 | // window even if motions are still being processed. |
| 3736 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3737 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3738 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3739 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 3740 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3741 | // Key will not be sent to the window, yet, because the window is still processing events |
| 3742 | // and the key remains pending, waiting for the touch events to be processed |
| 3743 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 3744 | ASSERT_FALSE(keySequenceNum); |
| 3745 | |
| 3746 | // 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] | 3747 | mFocusedWindow->setFocusable(false); |
| 3748 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3749 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3750 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3751 | |
| 3752 | // Focus events should precede the key events |
| 3753 | mUnfocusedWindow->consumeFocusEvent(true); |
| 3754 | mFocusedWindow->consumeFocusEvent(false); |
| 3755 | |
| 3756 | // Finish the tap events, which should unblock dispatcher |
| 3757 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 3758 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 3759 | |
| 3760 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 3761 | // can finally go to the newly focused "mUnfocusedWindow". |
| 3762 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3763 | mFocusedWindow->assertNoEvents(); |
| 3764 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3765 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3766 | } |
| 3767 | |
| 3768 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 3769 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 3770 | // The other window should not be affected by that. |
| 3771 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 3772 | // Touch Window 1 |
| 3773 | NotifyMotionArgs motionArgs = |
| 3774 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3775 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); |
| 3776 | mDispatcher->notifyMotion(&motionArgs); |
| 3777 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 3778 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 3779 | |
| 3780 | // Touch Window 2 |
| 3781 | int32_t actionPointerDown = |
| 3782 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 3783 | |
| 3784 | motionArgs = |
| 3785 | generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3786 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); |
| 3787 | mDispatcher->notifyMotion(&motionArgs); |
| 3788 | |
| 3789 | const std::chrono::duration timeout = |
| 3790 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3791 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3792 | |
| 3793 | mUnfocusedWindow->consumeMotionDown(); |
| 3794 | mFocusedWindow->consumeMotionDown(); |
| 3795 | // Focused window may or may not receive ACTION_MOVE |
| 3796 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 3797 | InputEvent* event; |
| 3798 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 3799 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 3800 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 3801 | ASSERT_NE(nullptr, event); |
| 3802 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 3803 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 3804 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 3805 | mFocusedWindow->consumeMotionCancel(); |
| 3806 | } else { |
| 3807 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 3808 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3809 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3810 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mFocusedWindow->getToken()); |
| 3811 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3812 | mUnfocusedWindow->assertNoEvents(); |
| 3813 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3814 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3815 | } |
| 3816 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 3817 | /** |
| 3818 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 3819 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 3820 | * |
| 3821 | * If the user touches another application during this time, the key should be dropped. |
| 3822 | * Next, if a new focused window comes in, without toggling the focused application, |
| 3823 | * then no ANR should occur. |
| 3824 | * |
| 3825 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 3826 | * but in some cases the policy may not update the focused application. |
| 3827 | */ |
| 3828 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 3829 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 3830 | std::make_shared<FakeApplicationHandle>(); |
| 3831 | focusedApplication->setDispatchingTimeout(60ms); |
| 3832 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 3833 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 3834 | mFocusedWindow->setFocusable(false); |
| 3835 | |
| 3836 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 3837 | mFocusedWindow->consumeFocusEvent(false); |
| 3838 | |
| 3839 | // Send a key. The ANR timer should start because there is no focused window. |
| 3840 | // 'focusedApplication' will get blamed if this timer completes. |
| 3841 | // 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] | 3842 | InputEventInjectionResult result = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 3843 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3844 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 3845 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 3846 | |
| 3847 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 3848 | // then the injected touches won't cause the focused event to get dropped. |
| 3849 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 3850 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 3851 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 3852 | // For this test, it means that the key would get delivered to the window once it becomes |
| 3853 | // focused. |
| 3854 | std::this_thread::sleep_for(10ms); |
| 3855 | |
| 3856 | // Touch unfocused window. This should force the pending key to get dropped. |
| 3857 | NotifyMotionArgs motionArgs = |
| 3858 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3859 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); |
| 3860 | mDispatcher->notifyMotion(&motionArgs); |
| 3861 | |
| 3862 | // We do not consume the motion right away, because that would require dispatcher to first |
| 3863 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 3864 | // Set the focused window first. |
| 3865 | mFocusedWindow->setFocusable(true); |
| 3866 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 3867 | setFocusedWindow(mFocusedWindow); |
| 3868 | mFocusedWindow->consumeFocusEvent(true); |
| 3869 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 3870 | // to another application. This could be a bug / behaviour in the policy. |
| 3871 | |
| 3872 | mUnfocusedWindow->consumeMotionDown(); |
| 3873 | |
| 3874 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3875 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 3876 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 3877 | } |
| 3878 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 3879 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 3880 | // that has feature NO_INPUT_CHANNEL. |
| 3881 | // Layout: |
| 3882 | // Top (closest to user) |
| 3883 | // mNoInputWindow (above all windows) |
| 3884 | // mBottomWindow |
| 3885 | // Bottom (furthest from user) |
| 3886 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 3887 | virtual void SetUp() override { |
| 3888 | InputDispatcherTest::SetUp(); |
| 3889 | |
| 3890 | mApplication = std::make_shared<FakeApplicationHandle>(); |
| 3891 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, |
| 3892 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
| 3893 | std::make_optional<sp<IBinder>>(nullptr) /*token*/); |
| 3894 | |
| 3895 | mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3896 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3897 | // It's perfectly valid for this window to not have an associated input channel |
| 3898 | |
| 3899 | mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window", |
| 3900 | ADISPLAY_ID_DEFAULT); |
| 3901 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3902 | |
| 3903 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 3904 | } |
| 3905 | |
| 3906 | protected: |
| 3907 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 3908 | sp<FakeWindowHandle> mNoInputWindow; |
| 3909 | sp<FakeWindowHandle> mBottomWindow; |
| 3910 | }; |
| 3911 | |
| 3912 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 3913 | PointF touchedPoint = {10, 10}; |
| 3914 | |
| 3915 | NotifyMotionArgs motionArgs = |
| 3916 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3917 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 3918 | mDispatcher->notifyMotion(&motionArgs); |
| 3919 | |
| 3920 | mNoInputWindow->assertNoEvents(); |
| 3921 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 3922 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 3923 | // and therefore should prevent mBottomWindow from receiving touches |
| 3924 | mBottomWindow->assertNoEvents(); |
| 3925 | } |
| 3926 | |
| 3927 | /** |
| 3928 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 3929 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 3930 | */ |
| 3931 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 3932 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
| 3933 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, |
| 3934 | "Window with input channel and NO_INPUT_CHANNEL", |
| 3935 | ADISPLAY_ID_DEFAULT); |
| 3936 | |
| 3937 | mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3938 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3939 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 3940 | |
| 3941 | PointF touchedPoint = {10, 10}; |
| 3942 | |
| 3943 | NotifyMotionArgs motionArgs = |
| 3944 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3945 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 3946 | mDispatcher->notifyMotion(&motionArgs); |
| 3947 | |
| 3948 | mNoInputWindow->assertNoEvents(); |
| 3949 | mBottomWindow->assertNoEvents(); |
| 3950 | } |
| 3951 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3952 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 3953 | protected: |
| 3954 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 3955 | sp<FakeWindowHandle> mWindow; |
| 3956 | sp<FakeWindowHandle> mMirror; |
| 3957 | |
| 3958 | virtual void SetUp() override { |
| 3959 | InputDispatcherTest::SetUp(); |
| 3960 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 3961 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 3962 | mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT, |
| 3963 | mWindow->getToken()); |
| 3964 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 3965 | mWindow->setFocusable(true); |
| 3966 | mMirror->setFocusable(true); |
| 3967 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 3968 | } |
| 3969 | }; |
| 3970 | |
| 3971 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 3972 | // Request focus on a mirrored window |
| 3973 | setFocusedWindow(mMirror); |
| 3974 | |
| 3975 | // window gets focused |
| 3976 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3977 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3978 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3979 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 3980 | } |
| 3981 | |
| 3982 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 3983 | // focusable. |
| 3984 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 3985 | setFocusedWindow(mMirror); |
| 3986 | |
| 3987 | // window gets focused |
| 3988 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3989 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3990 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3991 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3992 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 3993 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3994 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 3995 | |
| 3996 | mMirror->setFocusable(false); |
| 3997 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 3998 | |
| 3999 | // window loses focus since one of the windows associated with the token in not focusable |
| 4000 | mWindow->consumeFocusEvent(false); |
| 4001 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4002 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 4003 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4004 | mWindow->assertNoEvents(); |
| 4005 | } |
| 4006 | |
| 4007 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 4008 | // invisible. |
| 4009 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 4010 | setFocusedWindow(mMirror); |
| 4011 | |
| 4012 | // window gets focused |
| 4013 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4014 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4015 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4016 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4017 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 4018 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4019 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 4020 | |
| 4021 | mMirror->setVisible(false); |
| 4022 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 4023 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4024 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4025 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4026 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4027 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 4028 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4029 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 4030 | |
| 4031 | mWindow->setVisible(false); |
| 4032 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 4033 | |
| 4034 | // window loses focus only after all windows associated with the token become invisible. |
| 4035 | mWindow->consumeFocusEvent(false); |
| 4036 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4037 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 4038 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4039 | mWindow->assertNoEvents(); |
| 4040 | } |
| 4041 | |
| 4042 | // A focused & mirrored window remains focused until both windows are removed. |
| 4043 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 4044 | setFocusedWindow(mMirror); |
| 4045 | |
| 4046 | // window gets focused |
| 4047 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4048 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4049 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4050 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4051 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 4052 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4053 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 4054 | |
| 4055 | // single window is removed but the window token remains focused |
| 4056 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 4057 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4058 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4059 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4060 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4061 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 4062 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4063 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 4064 | |
| 4065 | // Both windows are removed |
| 4066 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 4067 | mWindow->consumeFocusEvent(false); |
| 4068 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4069 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 4070 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4071 | mWindow->assertNoEvents(); |
| 4072 | } |
| 4073 | |
| 4074 | // Focus request can be pending until one window becomes visible. |
| 4075 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 4076 | // Request focus on an invisible mirror. |
| 4077 | mWindow->setVisible(false); |
| 4078 | mMirror->setVisible(false); |
| 4079 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 4080 | setFocusedWindow(mMirror); |
| 4081 | |
| 4082 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4083 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4084 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4085 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4086 | |
| 4087 | mMirror->setVisible(true); |
| 4088 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 4089 | |
| 4090 | // window gets focused |
| 4091 | mWindow->consumeFocusEvent(true); |
| 4092 | // window gets the pending key event |
| 4093 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4094 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4095 | |
| 4096 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 4097 | protected: |
| 4098 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 4099 | sp<FakeWindowHandle> mWindow; |
| 4100 | sp<FakeWindowHandle> mSecondWindow; |
| 4101 | |
| 4102 | void SetUp() override { |
| 4103 | InputDispatcherTest::SetUp(); |
| 4104 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 4105 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 4106 | mWindow->setFocusable(true); |
| 4107 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
| 4108 | mSecondWindow->setFocusable(true); |
| 4109 | |
| 4110 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 4111 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 4112 | |
| 4113 | setFocusedWindow(mWindow); |
| 4114 | mWindow->consumeFocusEvent(true); |
| 4115 | } |
| 4116 | |
| 4117 | void notifyPointerCaptureChanged(bool enabled) { |
| 4118 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled); |
| 4119 | mDispatcher->notifyPointerCaptureChanged(&args); |
| 4120 | } |
| 4121 | |
| 4122 | void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) { |
| 4123 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
| 4124 | mFakePolicy->waitForSetPointerCapture(enabled); |
| 4125 | notifyPointerCaptureChanged(enabled); |
| 4126 | window->consumeCaptureEvent(enabled); |
| 4127 | } |
| 4128 | }; |
| 4129 | |
| 4130 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 4131 | // Ensure that capture cannot be obtained for unfocused windows. |
| 4132 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 4133 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 4134 | mSecondWindow->assertNoEvents(); |
| 4135 | |
| 4136 | // Ensure that capture can be enabled from the focus window. |
| 4137 | requestAndVerifyPointerCapture(mWindow, true); |
| 4138 | |
| 4139 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 4140 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 4141 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 4142 | |
| 4143 | // Ensure that capture can be disabled from the window with capture. |
| 4144 | requestAndVerifyPointerCapture(mWindow, false); |
| 4145 | } |
| 4146 | |
| 4147 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
| 4148 | requestAndVerifyPointerCapture(mWindow, true); |
| 4149 | |
| 4150 | setFocusedWindow(mSecondWindow); |
| 4151 | |
| 4152 | // Ensure that the capture disabled event was sent first. |
| 4153 | mWindow->consumeCaptureEvent(false); |
| 4154 | mWindow->consumeFocusEvent(false); |
| 4155 | mSecondWindow->consumeFocusEvent(true); |
| 4156 | mFakePolicy->waitForSetPointerCapture(false); |
| 4157 | |
| 4158 | // Ensure that additional state changes from InputReader are not sent to the window. |
| 4159 | notifyPointerCaptureChanged(false); |
| 4160 | notifyPointerCaptureChanged(true); |
| 4161 | notifyPointerCaptureChanged(false); |
| 4162 | mWindow->assertNoEvents(); |
| 4163 | mSecondWindow->assertNoEvents(); |
| 4164 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 4165 | } |
| 4166 | |
| 4167 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
| 4168 | requestAndVerifyPointerCapture(mWindow, true); |
| 4169 | |
| 4170 | // InputReader unexpectedly disables and enables pointer capture. |
| 4171 | notifyPointerCaptureChanged(false); |
| 4172 | notifyPointerCaptureChanged(true); |
| 4173 | |
| 4174 | // Ensure that Pointer Capture is disabled. |
| 4175 | mWindow->consumeCaptureEvent(false); |
| 4176 | mWindow->assertNoEvents(); |
| 4177 | } |
| 4178 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 4179 | } // namespace android::inputdispatcher |