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 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1790 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1791 | public: |
| 1792 | FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1793 | int32_t displayId, bool isGestureMonitor = false) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1794 | base::Result<std::unique_ptr<InputChannel>> channel = |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 1795 | dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1796 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1799 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 1800 | |
| 1801 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1802 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, |
| 1803 | expectedDisplayId, expectedFlags); |
| 1804 | } |
| 1805 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1806 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 1807 | |
| 1808 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 1809 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1810 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1811 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 1812 | expectedDisplayId, expectedFlags); |
| 1813 | } |
| 1814 | |
| 1815 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1816 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, |
| 1817 | expectedDisplayId, expectedFlags); |
| 1818 | } |
| 1819 | |
| 1820 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 1821 | |
| 1822 | private: |
| 1823 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1824 | }; |
| 1825 | |
| 1826 | // Tests for gesture monitors |
| 1827 | TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1828 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1829 | sp<FakeWindowHandle> window = |
| 1830 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1831 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1832 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1833 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 1834 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1835 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1836 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1837 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1838 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1839 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1840 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1844 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1845 | sp<FakeWindowHandle> window = |
| 1846 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1847 | |
| 1848 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1849 | window->setFocusable(true); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1850 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1851 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1852 | setFocusedWindow(window); |
| 1853 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1854 | window->consumeFocusEvent(true); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1855 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1856 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 1857 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1858 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1859 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 1860 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1861 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1862 | monitor.assertNoEvents(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1866 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1867 | sp<FakeWindowHandle> window = |
| 1868 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1869 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1870 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1871 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 1872 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1873 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1874 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1875 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1876 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1877 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1878 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1879 | |
| 1880 | window->releaseChannel(); |
| 1881 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1882 | mDispatcher->pilferPointers(monitor.getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1883 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1884 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1885 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1886 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1887 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1890 | TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) { |
| 1891 | FakeMonitorReceiver monitor = |
| 1892 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, |
| 1893 | true /*isGestureMonitor*/); |
| 1894 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1895 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1896 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
| 1897 | std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 1898 | ASSERT_TRUE(consumeSeq); |
| 1899 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1900 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(DISPATCHING_TIMEOUT, |
| 1901 | monitor.getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1902 | monitor.finishEvent(*consumeSeq); |
| 1903 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1904 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(monitor.getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1905 | } |
| 1906 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1907 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1908 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1909 | sp<FakeWindowHandle> window = |
| 1910 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1911 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1912 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1913 | |
| 1914 | NotifyMotionArgs motionArgs = |
| 1915 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1916 | ADISPLAY_ID_DEFAULT); |
| 1917 | |
| 1918 | mDispatcher->notifyMotion(&motionArgs); |
| 1919 | // Window should receive motion down event. |
| 1920 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1921 | |
| 1922 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1923 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1924 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1925 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 1926 | motionArgs.pointerCoords[0].getX() - 10); |
| 1927 | |
| 1928 | mDispatcher->notifyMotion(&motionArgs); |
| 1929 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
| 1930 | 0 /*expectedFlags*/); |
| 1931 | } |
| 1932 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1933 | /** |
| 1934 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 1935 | * the device default right away. In the test scenario, we check both the default value, |
| 1936 | * and the action of enabling / disabling. |
| 1937 | */ |
| 1938 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1939 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1940 | sp<FakeWindowHandle> window = |
| 1941 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 1942 | |
| 1943 | // Set focused application. |
| 1944 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1945 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1946 | |
| 1947 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1948 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1949 | setFocusedWindow(window); |
| 1950 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1951 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 1952 | |
| 1953 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1954 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1955 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1956 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); |
| 1957 | |
| 1958 | SCOPED_TRACE("Disable touch mode"); |
| 1959 | mDispatcher->setInTouchMode(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1960 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1961 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1962 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1963 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); |
| 1964 | |
| 1965 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1966 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1967 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1968 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); |
| 1969 | |
| 1970 | SCOPED_TRACE("Enable touch mode again"); |
| 1971 | mDispatcher->setInTouchMode(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1972 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1973 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1974 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1975 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 1976 | |
| 1977 | window->assertNoEvents(); |
| 1978 | } |
| 1979 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 1980 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1981 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 1982 | sp<FakeWindowHandle> window = |
| 1983 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 1984 | |
| 1985 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1986 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 1987 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1988 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1989 | setFocusedWindow(window); |
| 1990 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 1991 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 1992 | |
| 1993 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 1994 | mDispatcher->notifyKey(&keyArgs); |
| 1995 | |
| 1996 | InputEvent* event = window->consume(); |
| 1997 | ASSERT_NE(event, nullptr); |
| 1998 | |
| 1999 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 2000 | ASSERT_NE(verified, nullptr); |
| 2001 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 2002 | |
| 2003 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 2004 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 2005 | ASSERT_EQ(keyArgs.source, verified->source); |
| 2006 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 2007 | |
| 2008 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 2009 | |
| 2010 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
| 2011 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2012 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
| 2013 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 2014 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 2015 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 2016 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 2017 | } |
| 2018 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2019 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2020 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2021 | sp<FakeWindowHandle> window = |
| 2022 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 2023 | |
| 2024 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2025 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2026 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2027 | |
| 2028 | NotifyMotionArgs motionArgs = |
| 2029 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2030 | ADISPLAY_ID_DEFAULT); |
| 2031 | mDispatcher->notifyMotion(&motionArgs); |
| 2032 | |
| 2033 | InputEvent* event = window->consume(); |
| 2034 | ASSERT_NE(event, nullptr); |
| 2035 | |
| 2036 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 2037 | ASSERT_NE(verified, nullptr); |
| 2038 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 2039 | |
| 2040 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 2041 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 2042 | EXPECT_EQ(motionArgs.source, verified->source); |
| 2043 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 2044 | |
| 2045 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 2046 | |
| 2047 | EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX); |
| 2048 | EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY); |
| 2049 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
| 2050 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
| 2051 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
| 2052 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 2053 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 2054 | } |
| 2055 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame^] | 2056 | TEST_F(InputDispatcherTest, NonPointerMotionEvent_JoystickNotTransformed) { |
| 2057 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2058 | sp<FakeWindowHandle> window = |
| 2059 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 2060 | const std::string name = window->getName(); |
| 2061 | |
| 2062 | // Window gets transformed by offset values. |
| 2063 | window->setWindowOffset(500.0f, 500.0f); |
| 2064 | |
| 2065 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2066 | window->setFocusable(true); |
| 2067 | |
| 2068 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2069 | |
| 2070 | // First, we set focused window so that focusedWindowHandle is not null. |
| 2071 | setFocusedWindow(window); |
| 2072 | |
| 2073 | // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked. |
| 2074 | window->consumeFocusEvent(true); |
| 2075 | |
| 2076 | NotifyMotionArgs motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
| 2077 | AINPUT_SOURCE_JOYSTICK, ADISPLAY_ID_DEFAULT); |
| 2078 | mDispatcher->notifyMotion(&motionArgs); |
| 2079 | |
| 2080 | // Third, we consume motion event. |
| 2081 | InputEvent* event = window->consume(); |
| 2082 | ASSERT_NE(event, nullptr); |
| 2083 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 2084 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 2085 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 2086 | |
| 2087 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 2088 | EXPECT_EQ(AINPUT_EVENT_TYPE_MOTION, motionEvent.getAction()); |
| 2089 | |
| 2090 | float expectedX = motionArgs.pointerCoords[0].getX(); |
| 2091 | float expectedY = motionArgs.pointerCoords[0].getY(); |
| 2092 | |
| 2093 | // Finally we test if the axis values from the final motion event are not transformed |
| 2094 | EXPECT_EQ(expectedX, motionEvent.getX(0)) << "expected " << expectedX << " for x coord of " |
| 2095 | << name.c_str() << ", got " << motionEvent.getX(0); |
| 2096 | EXPECT_EQ(expectedY, motionEvent.getY(0)) << "expected " << expectedY << " for y coord of " |
| 2097 | << name.c_str() << ", got " << motionEvent.getY(0); |
| 2098 | } |
| 2099 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2100 | /** |
| 2101 | * Ensure that separate calls to sign the same data are generating the same key. |
| 2102 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 2103 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 2104 | * tests. |
| 2105 | */ |
| 2106 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 2107 | KeyEvent event = getTestKeyEvent(); |
| 2108 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 2109 | |
| 2110 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 2111 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 2112 | ASSERT_EQ(hmac1, hmac2); |
| 2113 | } |
| 2114 | |
| 2115 | /** |
| 2116 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 2117 | */ |
| 2118 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 2119 | KeyEvent event = getTestKeyEvent(); |
| 2120 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 2121 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 2122 | |
| 2123 | verifiedEvent.deviceId += 1; |
| 2124 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2125 | |
| 2126 | verifiedEvent.source += 1; |
| 2127 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2128 | |
| 2129 | verifiedEvent.eventTimeNanos += 1; |
| 2130 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2131 | |
| 2132 | verifiedEvent.displayId += 1; |
| 2133 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2134 | |
| 2135 | verifiedEvent.action += 1; |
| 2136 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2137 | |
| 2138 | verifiedEvent.downTimeNanos += 1; |
| 2139 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2140 | |
| 2141 | verifiedEvent.flags += 1; |
| 2142 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2143 | |
| 2144 | verifiedEvent.keyCode += 1; |
| 2145 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2146 | |
| 2147 | verifiedEvent.scanCode += 1; |
| 2148 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2149 | |
| 2150 | verifiedEvent.metaState += 1; |
| 2151 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2152 | |
| 2153 | verifiedEvent.repeatCount += 1; |
| 2154 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 2155 | } |
| 2156 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2157 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 2158 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2159 | sp<FakeWindowHandle> windowTop = |
| 2160 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 2161 | sp<FakeWindowHandle> windowSecond = |
| 2162 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2163 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2164 | |
| 2165 | // Top window is also focusable but is not granted focus. |
| 2166 | windowTop->setFocusable(true); |
| 2167 | windowSecond->setFocusable(true); |
| 2168 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 2169 | setFocusedWindow(windowSecond); |
| 2170 | |
| 2171 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2172 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 2173 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2174 | |
| 2175 | // Focused window should receive event. |
| 2176 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 2177 | windowTop->assertNoEvents(); |
| 2178 | } |
| 2179 | |
| 2180 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 2181 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2182 | sp<FakeWindowHandle> window = |
| 2183 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 2184 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2185 | |
| 2186 | window->setFocusable(true); |
| 2187 | // Release channel for window is no longer valid. |
| 2188 | window->releaseChannel(); |
| 2189 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2190 | setFocusedWindow(window); |
| 2191 | |
| 2192 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2193 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 2194 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2195 | |
| 2196 | // window channel is invalid, so it should not receive any input event. |
| 2197 | window->assertNoEvents(); |
| 2198 | } |
| 2199 | |
| 2200 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 2201 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2202 | sp<FakeWindowHandle> window = |
| 2203 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 2204 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2205 | |
| 2206 | // Window is not focusable. |
| 2207 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2208 | setFocusedWindow(window); |
| 2209 | |
| 2210 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2211 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 2212 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2213 | |
| 2214 | // window is invalid, so it should not receive any input event. |
| 2215 | window->assertNoEvents(); |
| 2216 | } |
| 2217 | |
| 2218 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 2219 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2220 | sp<FakeWindowHandle> windowTop = |
| 2221 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 2222 | sp<FakeWindowHandle> windowSecond = |
| 2223 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2224 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2225 | |
| 2226 | windowTop->setFocusable(true); |
| 2227 | windowSecond->setFocusable(true); |
| 2228 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 2229 | setFocusedWindow(windowTop); |
| 2230 | windowTop->consumeFocusEvent(true); |
| 2231 | |
| 2232 | setFocusedWindow(windowSecond, windowTop); |
| 2233 | windowSecond->consumeFocusEvent(true); |
| 2234 | windowTop->consumeFocusEvent(false); |
| 2235 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2236 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 2237 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2238 | |
| 2239 | // Focused window should receive event. |
| 2240 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 2241 | } |
| 2242 | |
| 2243 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { |
| 2244 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2245 | sp<FakeWindowHandle> windowTop = |
| 2246 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 2247 | sp<FakeWindowHandle> windowSecond = |
| 2248 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2249 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2250 | |
| 2251 | windowTop->setFocusable(true); |
| 2252 | windowSecond->setFocusable(true); |
| 2253 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 2254 | setFocusedWindow(windowSecond, windowTop); |
| 2255 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2256 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 2257 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2258 | |
| 2259 | // Event should be dropped. |
| 2260 | windowTop->assertNoEvents(); |
| 2261 | windowSecond->assertNoEvents(); |
| 2262 | } |
| 2263 | |
| 2264 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 2265 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2266 | sp<FakeWindowHandle> window = |
| 2267 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 2268 | sp<FakeWindowHandle> previousFocusedWindow = |
| 2269 | new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow", |
| 2270 | ADISPLAY_ID_DEFAULT); |
| 2271 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2272 | |
| 2273 | window->setFocusable(true); |
| 2274 | previousFocusedWindow->setFocusable(true); |
| 2275 | window->setVisible(false); |
| 2276 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 2277 | setFocusedWindow(previousFocusedWindow); |
| 2278 | previousFocusedWindow->consumeFocusEvent(true); |
| 2279 | |
| 2280 | // Requesting focus on invisible window takes focus from currently focused window. |
| 2281 | setFocusedWindow(window); |
| 2282 | previousFocusedWindow->consumeFocusEvent(false); |
| 2283 | |
| 2284 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2285 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2286 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2287 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2288 | |
| 2289 | // Window does not get focus event or key down. |
| 2290 | window->assertNoEvents(); |
| 2291 | |
| 2292 | // Window becomes visible. |
| 2293 | window->setVisible(true); |
| 2294 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2295 | |
| 2296 | // Window receives focus event. |
| 2297 | window->consumeFocusEvent(true); |
| 2298 | // Focused window receives key down. |
| 2299 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2300 | } |
| 2301 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2302 | /** |
| 2303 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 2304 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 2305 | * of the 'slipperyEnterWindow'. |
| 2306 | * |
| 2307 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 2308 | * a way so that the touched location is no longer covered by the top window. |
| 2309 | * |
| 2310 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 2311 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 2312 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 2313 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 2314 | * with ACTION_DOWN). |
| 2315 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 2316 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 2317 | * |
| 2318 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 2319 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 2320 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 2321 | * |
| 2322 | * In this test, we ensure that the event received by the bottom window has |
| 2323 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 2324 | */ |
| 2325 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
| 2326 | constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1; |
| 2327 | constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1; |
| 2328 | |
| 2329 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2330 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2331 | |
| 2332 | sp<FakeWindowHandle> slipperyExitWindow = |
| 2333 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 2334 | slipperyExitWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 2335 | InputWindowInfo::Flag::SLIPPERY); |
| 2336 | // Make sure this one overlaps the bottom window |
| 2337 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 2338 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 2339 | // one. Windows with the same owner are not considered to be occluding each other. |
| 2340 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 2341 | |
| 2342 | sp<FakeWindowHandle> slipperyEnterWindow = |
| 2343 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2344 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 2345 | |
| 2346 | mDispatcher->setInputWindows( |
| 2347 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 2348 | |
| 2349 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
| 2350 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2351 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 2352 | mDispatcher->notifyMotion(&args); |
| 2353 | slipperyExitWindow->consumeMotionDown(); |
| 2354 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 2355 | mDispatcher->setInputWindows( |
| 2356 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 2357 | |
| 2358 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2359 | ADISPLAY_ID_DEFAULT, {{51, 51}}); |
| 2360 | mDispatcher->notifyMotion(&args); |
| 2361 | |
| 2362 | slipperyExitWindow->consumeMotionCancel(); |
| 2363 | |
| 2364 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 2365 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 2366 | } |
| 2367 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2368 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 2369 | protected: |
| 2370 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 2371 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 2372 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2373 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2374 | sp<FakeWindowHandle> mWindow; |
| 2375 | |
| 2376 | virtual void SetUp() override { |
| 2377 | mFakePolicy = new FakeInputDispatcherPolicy(); |
| 2378 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
| 2379 | mDispatcher = new InputDispatcher(mFakePolicy); |
| 2380 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 2381 | ASSERT_EQ(OK, mDispatcher->start()); |
| 2382 | |
| 2383 | setUpWindow(); |
| 2384 | } |
| 2385 | |
| 2386 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2387 | mApp = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2388 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2389 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2390 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2391 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2392 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2393 | mWindow->consumeFocusEvent(true); |
| 2394 | } |
| 2395 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2396 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2397 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2398 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2399 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
| 2400 | mDispatcher->notifyKey(&keyArgs); |
| 2401 | |
| 2402 | // Window should receive key down event. |
| 2403 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2404 | } |
| 2405 | |
| 2406 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 2407 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 2408 | InputEvent* repeatEvent = mWindow->consume(); |
| 2409 | ASSERT_NE(nullptr, repeatEvent); |
| 2410 | |
| 2411 | uint32_t eventType = repeatEvent->getType(); |
| 2412 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); |
| 2413 | |
| 2414 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 2415 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 2416 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 2417 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 2418 | } |
| 2419 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2420 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2421 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2422 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2423 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
| 2424 | mDispatcher->notifyKey(&keyArgs); |
| 2425 | |
| 2426 | // Window should receive key down event. |
| 2427 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 2428 | 0 /*expectedFlags*/); |
| 2429 | } |
| 2430 | }; |
| 2431 | |
| 2432 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2433 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 2434 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2435 | expectKeyRepeatOnce(repeatCount); |
| 2436 | } |
| 2437 | } |
| 2438 | |
| 2439 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
| 2440 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 2441 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2442 | expectKeyRepeatOnce(repeatCount); |
| 2443 | } |
| 2444 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 2445 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2446 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2447 | expectKeyRepeatOnce(repeatCount); |
| 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2452 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2453 | expectKeyRepeatOnce(1 /*repeatCount*/); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2454 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 2455 | mWindow->assertNoEvents(); |
| 2456 | } |
| 2457 | |
| 2458 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
| 2459 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 2460 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 2461 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 2462 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 2463 | // Stale key up from device 1. |
| 2464 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 2465 | // Device 2 is still down, keep repeating |
| 2466 | expectKeyRepeatOnce(2 /*repeatCount*/); |
| 2467 | expectKeyRepeatOnce(3 /*repeatCount*/); |
| 2468 | // Device 2 key up |
| 2469 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 2470 | mWindow->assertNoEvents(); |
| 2471 | } |
| 2472 | |
| 2473 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
| 2474 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 2475 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 2476 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 2477 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 2478 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. |
| 2479 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 2480 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2481 | mWindow->assertNoEvents(); |
| 2482 | } |
| 2483 | |
| 2484 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2485 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2486 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2487 | InputEvent* repeatEvent = mWindow->consume(); |
| 2488 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 2489 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 2490 | IdGenerator::getSource(repeatEvent->getId())); |
| 2491 | } |
| 2492 | } |
| 2493 | |
| 2494 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2495 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2496 | |
| 2497 | std::unordered_set<int32_t> idSet; |
| 2498 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2499 | InputEvent* repeatEvent = mWindow->consume(); |
| 2500 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 2501 | int32_t id = repeatEvent->getId(); |
| 2502 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 2503 | idSet.insert(id); |
| 2504 | } |
| 2505 | } |
| 2506 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2507 | /* Test InputDispatcher for MultiDisplay */ |
| 2508 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 2509 | public: |
| 2510 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2511 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2512 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2513 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2514 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2515 | windowInPrimary = |
| 2516 | new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2517 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2518 | // Set focus window for primary display, but focused display would be second one. |
| 2519 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2520 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2521 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2522 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2523 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2524 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2525 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2526 | windowInSecondary = |
| 2527 | new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2528 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2529 | // Set focus display to second one. |
| 2530 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 2531 | // Set focus window for second display. |
| 2532 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2533 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2534 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2535 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2536 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2537 | } |
| 2538 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2539 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2540 | InputDispatcherTest::TearDown(); |
| 2541 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2542 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2543 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2544 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2545 | windowInSecondary.clear(); |
| 2546 | } |
| 2547 | |
| 2548 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2549 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2550 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2551 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2552 | sp<FakeWindowHandle> windowInSecondary; |
| 2553 | }; |
| 2554 | |
| 2555 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 2556 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2557 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2558 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 2559 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2560 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2561 | windowInSecondary->assertNoEvents(); |
| 2562 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2563 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2564 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2565 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 2566 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2567 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2568 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2569 | } |
| 2570 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2571 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2572 | // Test inject a key down with display id specified. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2573 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 2574 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2575 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2576 | windowInSecondary->assertNoEvents(); |
| 2577 | |
| 2578 | // Test inject a key down without display id specified. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2579 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 2580 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2581 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2582 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2583 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2584 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2585 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2586 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2587 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2588 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
| 2589 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2590 | |
| 2591 | // Test inject a key down, should timeout because of no target window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2592 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 2593 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2594 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2595 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2596 | windowInSecondary->assertNoEvents(); |
| 2597 | } |
| 2598 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2599 | // Test per-display input monitors for motion event. |
| 2600 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2601 | FakeMonitorReceiver monitorInPrimary = |
| 2602 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 2603 | FakeMonitorReceiver monitorInSecondary = |
| 2604 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2605 | |
| 2606 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2607 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2608 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 2609 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2610 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2611 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2612 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2613 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2614 | |
| 2615 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2616 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2617 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 2618 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2619 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2620 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2621 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2622 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2623 | |
| 2624 | // Test inject a non-pointer motion event. |
| 2625 | // If specific a display, it will dispatch to the focused window of particular display, |
| 2626 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2627 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2628 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 2629 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2630 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2631 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2632 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2633 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | // Test per-display input monitors for key event. |
| 2637 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2638 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2639 | FakeMonitorReceiver monitorInPrimary = |
| 2640 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 2641 | FakeMonitorReceiver monitorInSecondary = |
| 2642 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2643 | |
| 2644 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2645 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 2646 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2647 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2648 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2649 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2650 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2651 | } |
| 2652 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2653 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 2654 | sp<FakeWindowHandle> secondWindowInPrimary = |
| 2655 | new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
| 2656 | secondWindowInPrimary->setFocusable(true); |
| 2657 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 2658 | setFocusedWindow(secondWindowInPrimary); |
| 2659 | windowInPrimary->consumeFocusEvent(false); |
| 2660 | secondWindowInPrimary->consumeFocusEvent(true); |
| 2661 | |
| 2662 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2663 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 2664 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2665 | windowInPrimary->assertNoEvents(); |
| 2666 | windowInSecondary->assertNoEvents(); |
| 2667 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2668 | } |
| 2669 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2670 | class InputFilterTest : public InputDispatcherTest { |
| 2671 | protected: |
| 2672 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
| 2673 | |
| 2674 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) { |
| 2675 | NotifyMotionArgs motionArgs; |
| 2676 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2677 | motionArgs = |
| 2678 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2679 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2680 | motionArgs = |
| 2681 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2682 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2683 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2684 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 2685 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2686 | } else { |
| 2687 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 2688 | } |
| 2689 | } |
| 2690 | |
| 2691 | void testNotifyKey(bool expectToBeFiltered) { |
| 2692 | NotifyKeyArgs keyArgs; |
| 2693 | |
| 2694 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 2695 | mDispatcher->notifyKey(&keyArgs); |
| 2696 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 2697 | mDispatcher->notifyKey(&keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2698 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2699 | |
| 2700 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 2701 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2702 | } else { |
| 2703 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 2704 | } |
| 2705 | } |
| 2706 | }; |
| 2707 | |
| 2708 | // Test InputFilter for MotionEvent |
| 2709 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 2710 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 2711 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 2712 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 2713 | |
| 2714 | // Enable InputFilter |
| 2715 | mDispatcher->setInputFilterEnabled(true); |
| 2716 | // Test touch on both primary and second display, and check if both events are filtered. |
| 2717 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 2718 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 2719 | |
| 2720 | // Disable InputFilter |
| 2721 | mDispatcher->setInputFilterEnabled(false); |
| 2722 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 2723 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 2724 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 2725 | } |
| 2726 | |
| 2727 | // Test InputFilter for KeyEvent |
| 2728 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 2729 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 2730 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 2731 | |
| 2732 | // Enable InputFilter |
| 2733 | mDispatcher->setInputFilterEnabled(true); |
| 2734 | // Send a key event, and check if it is filtered. |
| 2735 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 2736 | |
| 2737 | // Disable InputFilter |
| 2738 | mDispatcher->setInputFilterEnabled(false); |
| 2739 | // Send a key event, and check if it isn't filtered. |
| 2740 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 2741 | } |
| 2742 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2743 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2744 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2745 | InputDispatcherTest::SetUp(); |
| 2746 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2747 | std::shared_ptr<FakeApplicationHandle> application = |
| 2748 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2749 | mUnfocusedWindow = |
| 2750 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2751 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
| 2752 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 2753 | // window. |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2754 | mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2755 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2756 | mFocusedWindow = |
| 2757 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2758 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2759 | mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2760 | |
| 2761 | // Set focused application. |
| 2762 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2763 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2764 | |
| 2765 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2766 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2767 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2768 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2769 | } |
| 2770 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2771 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2772 | InputDispatcherTest::TearDown(); |
| 2773 | |
| 2774 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2775 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | protected: |
| 2779 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2780 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 2781 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2782 | }; |
| 2783 | |
| 2784 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 2785 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 2786 | // the onPointerDownOutsideFocus callback. |
| 2787 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2788 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 2789 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2790 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2791 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 2792 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2793 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2794 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2795 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 2796 | } |
| 2797 | |
| 2798 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 2799 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 2800 | // onPointerDownOutsideFocus callback. |
| 2801 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2802 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 2803 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2804 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 2805 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2806 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2807 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 2808 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 2812 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 2813 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2814 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 2815 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 2816 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2817 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2818 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 2819 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 2823 | // DOWN on the window that already has focus. Ensure no window received the |
| 2824 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2825 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2826 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2827 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 2828 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2829 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 2830 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2831 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2832 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 2833 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2834 | } |
| 2835 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2836 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 2837 | // windows that point to the same client token. |
| 2838 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 2839 | virtual void SetUp() override { |
| 2840 | InputDispatcherTest::SetUp(); |
| 2841 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2842 | std::shared_ptr<FakeApplicationHandle> application = |
| 2843 | std::make_shared<FakeApplicationHandle>(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2844 | mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1", |
| 2845 | ADISPLAY_ID_DEFAULT); |
| 2846 | // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window. |
| 2847 | // 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] | 2848 | mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 2849 | InputWindowInfo::Flag::SPLIT_TOUCH); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2850 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 2851 | |
| 2852 | mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2", |
| 2853 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2854 | mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 2855 | InputWindowInfo::Flag::SPLIT_TOUCH); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2856 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 2857 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2858 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2859 | } |
| 2860 | |
| 2861 | protected: |
| 2862 | sp<FakeWindowHandle> mWindow1; |
| 2863 | sp<FakeWindowHandle> mWindow2; |
| 2864 | |
| 2865 | // Helper function to convert the point from screen coordinates into the window's space |
| 2866 | static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2867 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 2868 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2869 | } |
| 2870 | |
| 2871 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 2872 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2873 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2874 | InputEvent* event = window->consume(); |
| 2875 | |
| 2876 | ASSERT_NE(nullptr, event) << name.c_str() |
| 2877 | << ": consumer should have returned non-NULL event."; |
| 2878 | |
| 2879 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 2880 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 2881 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 2882 | |
| 2883 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 2884 | EXPECT_EQ(expectedAction, motionEvent.getAction()); |
| 2885 | |
| 2886 | for (size_t i = 0; i < points.size(); i++) { |
| 2887 | float expectedX = points[i].x; |
| 2888 | float expectedY = points[i].y; |
| 2889 | |
| 2890 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 2891 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 2892 | << ", got " << motionEvent.getX(i); |
| 2893 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 2894 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 2895 | << ", got " << motionEvent.getY(i); |
| 2896 | } |
| 2897 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2898 | |
| 2899 | void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints, |
| 2900 | std::vector<PointF> expectedPoints) { |
| 2901 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 2902 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2903 | mDispatcher->notifyMotion(&motionArgs); |
| 2904 | |
| 2905 | // Always consume from window1 since it's the window that has the InputReceiver |
| 2906 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 2907 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2908 | }; |
| 2909 | |
| 2910 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 2911 | // Touch Window 1 |
| 2912 | PointF touchedPoint = {10, 10}; |
| 2913 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2914 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2915 | |
| 2916 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2917 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2918 | |
| 2919 | // Touch Window 2 |
| 2920 | touchedPoint = {150, 150}; |
| 2921 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2922 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2923 | } |
| 2924 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2925 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 2926 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2927 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 2928 | |
| 2929 | // Touch Window 1 |
| 2930 | PointF touchedPoint = {10, 10}; |
| 2931 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2932 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2933 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2934 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2935 | |
| 2936 | // Touch Window 2 |
| 2937 | touchedPoint = {150, 150}; |
| 2938 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2939 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 2940 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2941 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2942 | // Update the transform so rotation is set |
| 2943 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 2944 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 2945 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2946 | } |
| 2947 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2948 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2949 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 2950 | |
| 2951 | // Touch Window 1 |
| 2952 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 2953 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2954 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2955 | |
| 2956 | // Touch Window 2 |
| 2957 | int32_t actionPointerDown = |
| 2958 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2959 | touchedPoints.push_back(PointF{150, 150}); |
| 2960 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 2961 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2962 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2963 | // Release Window 2 |
| 2964 | int32_t actionPointerUp = |
| 2965 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 2966 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); |
| 2967 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2968 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2969 | // Update the transform so rotation is set for Window 2 |
| 2970 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 2971 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 2972 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2975 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2976 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 2977 | |
| 2978 | // Touch Window 1 |
| 2979 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 2980 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2981 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2982 | |
| 2983 | // Touch Window 2 |
| 2984 | int32_t actionPointerDown = |
| 2985 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2986 | touchedPoints.push_back(PointF{150, 150}); |
| 2987 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2988 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2989 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2990 | |
| 2991 | // Move both windows |
| 2992 | touchedPoints = {{20, 20}, {175, 175}}; |
| 2993 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 2994 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 2995 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2996 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2997 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2998 | // Release Window 2 |
| 2999 | int32_t actionPointerUp = |
| 3000 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 3001 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); |
| 3002 | expectedPoints.pop_back(); |
| 3003 | |
| 3004 | // Touch Window 2 |
| 3005 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 3006 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 3007 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
| 3008 | |
| 3009 | // Move both windows |
| 3010 | touchedPoints = {{20, 20}, {175, 175}}; |
| 3011 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 3012 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 3013 | |
| 3014 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 3018 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 3019 | |
| 3020 | // Touch Window 1 |
| 3021 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 3022 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3023 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3024 | |
| 3025 | // Touch Window 2 |
| 3026 | int32_t actionPointerDown = |
| 3027 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3028 | touchedPoints.push_back(PointF{150, 150}); |
| 3029 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3030 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3031 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3032 | |
| 3033 | // Move both windows |
| 3034 | touchedPoints = {{20, 20}, {175, 175}}; |
| 3035 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 3036 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 3037 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3038 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3041 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 3042 | virtual void SetUp() override { |
| 3043 | InputDispatcherTest::SetUp(); |
| 3044 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3045 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3046 | mApplication->setDispatchingTimeout(20ms); |
| 3047 | mWindow = |
| 3048 | new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 3049 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 3050 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3051 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3052 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 3053 | // window. |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3054 | mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3055 | |
| 3056 | // Set focused application. |
| 3057 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 3058 | |
| 3059 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3060 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3061 | mWindow->consumeFocusEvent(true); |
| 3062 | } |
| 3063 | |
| 3064 | virtual void TearDown() override { |
| 3065 | InputDispatcherTest::TearDown(); |
| 3066 | mWindow.clear(); |
| 3067 | } |
| 3068 | |
| 3069 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3070 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3071 | sp<FakeWindowHandle> mWindow; |
| 3072 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 3073 | |
| 3074 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3075 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3076 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3077 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3078 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3079 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3080 | WINDOW_LOCATION)); |
| 3081 | } |
| 3082 | }; |
| 3083 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3084 | // Send a tap and respond, which should not cause an ANR. |
| 3085 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 3086 | tapOnWindow(); |
| 3087 | mWindow->consumeMotionDown(); |
| 3088 | mWindow->consumeMotionUp(); |
| 3089 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3090 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3091 | } |
| 3092 | |
| 3093 | // Send a regular key and respond, which should not cause an ANR. |
| 3094 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3095 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3096 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 3097 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3098 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3099 | } |
| 3100 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3101 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 3102 | mWindow->setFocusable(false); |
| 3103 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3104 | mWindow->consumeFocusEvent(false); |
| 3105 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3106 | InputEventInjectionResult result = |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3107 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3108 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 3109 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3110 | // Key will not go to window because we have no focused window. |
| 3111 | // The 'no focused window' ANR timer should start instead. |
| 3112 | |
| 3113 | // Now, the focused application goes away. |
| 3114 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 3115 | // The key should get dropped and there should be no ANR. |
| 3116 | |
| 3117 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3118 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3119 | } |
| 3120 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3121 | // 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] | 3122 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 3123 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3124 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3125 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3126 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3127 | WINDOW_LOCATION)); |
| 3128 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3129 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 3130 | ASSERT_TRUE(sequenceNum); |
| 3131 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3132 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3133 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3134 | mWindow->finishEvent(*sequenceNum); |
| 3135 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 3136 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3137 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3138 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | // Send a key to the app and have the app not respond right away. |
| 3142 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 3143 | // Inject a key, and don't respond - expect that ANR is called. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3144 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3145 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 3146 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3147 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3148 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3149 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3150 | } |
| 3151 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3152 | // We have a focused application, but no focused window |
| 3153 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3154 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3155 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3156 | mWindow->consumeFocusEvent(false); |
| 3157 | |
| 3158 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3159 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3160 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3161 | WINDOW_LOCATION)); |
| 3162 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 3163 | mDispatcher->waitForIdle(); |
| 3164 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3165 | |
| 3166 | // Once a focused event arrives, we get an ANR for this application |
| 3167 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 3168 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3169 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3170 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3171 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 3172 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3173 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3174 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3175 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3176 | } |
| 3177 | |
| 3178 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3179 | // Make sure that we don't notify policy twice about the same ANR. |
| 3180 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3181 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3182 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3183 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3184 | |
| 3185 | // Once a focused event arrives, we get an ANR for this application |
| 3186 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 3187 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3188 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3189 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3190 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 3191 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3192 | const std::chrono::duration appTimeout = |
| 3193 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3194 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3195 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3196 | std::this_thread::sleep_for(appTimeout); |
| 3197 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 3198 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3199 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3200 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 3201 | // '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] | 3202 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3203 | } |
| 3204 | |
| 3205 | // We have a focused application, but no focused window |
| 3206 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3207 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3208 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3209 | mWindow->consumeFocusEvent(false); |
| 3210 | |
| 3211 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3212 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3213 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3214 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 3215 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3216 | |
| 3217 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3218 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3219 | |
| 3220 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3221 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3222 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3223 | mWindow->assertNoEvents(); |
| 3224 | } |
| 3225 | |
| 3226 | /** |
| 3227 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 3228 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 3229 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 3230 | * the ANR mechanism should still work. |
| 3231 | * |
| 3232 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 3233 | * DOWN event, while not responding on the second one. |
| 3234 | */ |
| 3235 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 3236 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 3237 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3238 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 3239 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 3240 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3241 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3242 | |
| 3243 | // Now send ACTION_UP, with identical timestamp |
| 3244 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3245 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 3246 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 3247 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3248 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3249 | |
| 3250 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 3251 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3252 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3253 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | // If an app is not responding to a key event, gesture monitors should continue to receive |
| 3257 | // new motion events |
| 3258 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) { |
| 3259 | FakeMonitorReceiver monitor = |
| 3260 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, |
| 3261 | true /*isGestureMonitor*/); |
| 3262 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3263 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3264 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3265 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3266 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3267 | |
| 3268 | // Stuck on the ACTION_UP |
| 3269 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3270 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3271 | |
| 3272 | // New tap will go to the gesture monitor, but not to the window |
| 3273 | tapOnWindow(); |
| 3274 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3275 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3276 | |
| 3277 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 3278 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3279 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3280 | mWindow->assertNoEvents(); |
| 3281 | monitor.assertNoEvents(); |
| 3282 | } |
| 3283 | |
| 3284 | // If an app is not responding to a motion event, gesture monitors should continue to receive |
| 3285 | // new motion events |
| 3286 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) { |
| 3287 | FakeMonitorReceiver monitor = |
| 3288 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, |
| 3289 | true /*isGestureMonitor*/); |
| 3290 | |
| 3291 | tapOnWindow(); |
| 3292 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3293 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3294 | |
| 3295 | mWindow->consumeMotionDown(); |
| 3296 | // Stuck on the ACTION_UP |
| 3297 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3298 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3299 | |
| 3300 | // New tap will go to the gesture monitor, but not to the window |
| 3301 | tapOnWindow(); |
| 3302 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3303 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3304 | |
| 3305 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 3306 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3307 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3308 | mWindow->assertNoEvents(); |
| 3309 | monitor.assertNoEvents(); |
| 3310 | } |
| 3311 | |
| 3312 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 3313 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 3314 | // get an ANR again. |
| 3315 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 3316 | // 2. consume all pending events (= queue becomes healthy again) |
| 3317 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 3318 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 3319 | tapOnWindow(); |
| 3320 | |
| 3321 | mWindow->consumeMotionDown(); |
| 3322 | // Block on ACTION_UP |
| 3323 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3324 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3325 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 3326 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3327 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3328 | mWindow->assertNoEvents(); |
| 3329 | |
| 3330 | tapOnWindow(); |
| 3331 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3332 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3333 | mWindow->consumeMotionUp(); |
| 3334 | |
| 3335 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3336 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken()); |
| 3337 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3338 | mWindow->assertNoEvents(); |
| 3339 | } |
| 3340 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3341 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 3342 | // it. |
| 3343 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3344 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3345 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3346 | WINDOW_LOCATION)); |
| 3347 | |
| 3348 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3349 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(windowTimeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3350 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3351 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 3352 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3353 | // 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] | 3354 | mWindow->consumeMotionDown(); |
| 3355 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 3356 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 3357 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3358 | mDispatcher->waitForIdle(); |
| 3359 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mWindow->getToken()); |
| 3360 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | /** |
| 3364 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 3365 | * not to to the focused window until the motion is processed. |
| 3366 | * |
| 3367 | * Warning!!! |
| 3368 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 3369 | * and the injection timeout that we specify when injecting the key. |
| 3370 | * We must have the injection timeout (10ms) be smaller than |
| 3371 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 3372 | * |
| 3373 | * If that value changes, this test should also change. |
| 3374 | */ |
| 3375 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 3376 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 3377 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3378 | |
| 3379 | tapOnWindow(); |
| 3380 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 3381 | ASSERT_TRUE(downSequenceNum); |
| 3382 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 3383 | ASSERT_TRUE(upSequenceNum); |
| 3384 | // Don't finish the events yet, and send a key |
| 3385 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 3386 | // window even if motions are still being processed. But because the injection timeout is short, |
| 3387 | // we will receive INJECTION_TIMED_OUT as the result. |
| 3388 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3389 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3390 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3391 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 3392 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3393 | // Key will not be sent to the window, yet, because the window is still processing events |
| 3394 | // and the key remains pending, waiting for the touch events to be processed |
| 3395 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 3396 | ASSERT_FALSE(keySequenceNum); |
| 3397 | |
| 3398 | std::this_thread::sleep_for(500ms); |
| 3399 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 3400 | // to the focused window, even though we have not yet finished the motion event |
| 3401 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3402 | mWindow->finishEvent(*downSequenceNum); |
| 3403 | mWindow->finishEvent(*upSequenceNum); |
| 3404 | } |
| 3405 | |
| 3406 | /** |
| 3407 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 3408 | * not go to the focused window until the motion is processed. |
| 3409 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 3410 | * focused window right away. |
| 3411 | */ |
| 3412 | TEST_F(InputDispatcherSingleWindowAnr, |
| 3413 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 3414 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 3415 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3416 | |
| 3417 | tapOnWindow(); |
| 3418 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 3419 | ASSERT_TRUE(downSequenceNum); |
| 3420 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 3421 | ASSERT_TRUE(upSequenceNum); |
| 3422 | // Don't finish the events yet, and send a key |
| 3423 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3424 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3425 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3426 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3427 | // At this point, key is still pending, and should not be sent to the application yet. |
| 3428 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 3429 | ASSERT_FALSE(keySequenceNum); |
| 3430 | |
| 3431 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 3432 | tapOnWindow(); |
| 3433 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 3434 | // the other events yet. We can finish events in any order. |
| 3435 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 3436 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 3437 | mWindow->consumeMotionDown(); |
| 3438 | mWindow->consumeMotionUp(); |
| 3439 | mWindow->assertNoEvents(); |
| 3440 | } |
| 3441 | |
| 3442 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 3443 | virtual void SetUp() override { |
| 3444 | InputDispatcherTest::SetUp(); |
| 3445 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3446 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3447 | mApplication->setDispatchingTimeout(10ms); |
| 3448 | mUnfocusedWindow = |
| 3449 | new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT); |
| 3450 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
| 3451 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 3452 | // window. |
| 3453 | // 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] | 3454 | mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 3455 | InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH | |
| 3456 | InputWindowInfo::Flag::SPLIT_TOUCH); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3457 | |
| 3458 | mFocusedWindow = |
| 3459 | new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 3460 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3461 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3462 | mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 3463 | InputWindowInfo::Flag::SPLIT_TOUCH); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3464 | |
| 3465 | // Set focused application. |
| 3466 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3467 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3468 | |
| 3469 | // Expect one focus window exist in display. |
| 3470 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3471 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3472 | mFocusedWindow->consumeFocusEvent(true); |
| 3473 | } |
| 3474 | |
| 3475 | virtual void TearDown() override { |
| 3476 | InputDispatcherTest::TearDown(); |
| 3477 | |
| 3478 | mUnfocusedWindow.clear(); |
| 3479 | mFocusedWindow.clear(); |
| 3480 | } |
| 3481 | |
| 3482 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3483 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3484 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 3485 | sp<FakeWindowHandle> mFocusedWindow; |
| 3486 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 3487 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 3488 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 3489 | |
| 3490 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 3491 | |
| 3492 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 3493 | |
| 3494 | private: |
| 3495 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3496 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3497 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3498 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3499 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3500 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3501 | location)); |
| 3502 | } |
| 3503 | }; |
| 3504 | |
| 3505 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 3506 | // should be ANR'd first. |
| 3507 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3508 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3509 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3510 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3511 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3512 | mFocusedWindow->consumeMotionDown(); |
| 3513 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 3514 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 3515 | // We consumed all events, so no ANR |
| 3516 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3517 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3518 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3519 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3520 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3521 | FOCUSED_WINDOW_LOCATION)); |
| 3522 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 3523 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3524 | |
| 3525 | const std::chrono::duration timeout = |
| 3526 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3527 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); |
| 3528 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 3529 | // sequence to make it consistent |
| 3530 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3531 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3532 | mFocusedWindow->consumeMotionDown(); |
| 3533 | // This cancel is generated because the connection was unresponsive |
| 3534 | mFocusedWindow->consumeMotionCancel(); |
| 3535 | mFocusedWindow->assertNoEvents(); |
| 3536 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3537 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3538 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mFocusedWindow->getToken()); |
| 3539 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 3543 | // it doesn't matter which order they should have ANR. |
| 3544 | // But we should receive ANR for both. |
| 3545 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 3546 | // Set the timeout for unfocused window to match the focused window |
| 3547 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 3548 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 3549 | |
| 3550 | tapOnFocusedWindow(); |
| 3551 | // 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] | 3552 | sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveConnectionToken(10ms); |
| 3553 | sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveConnectionToken(0ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3554 | |
| 3555 | // 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] | 3556 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 3557 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 3558 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 3559 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3560 | |
| 3561 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3562 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3563 | |
| 3564 | mFocusedWindow->consumeMotionDown(); |
| 3565 | mFocusedWindow->consumeMotionUp(); |
| 3566 | mUnfocusedWindow->consumeMotionOutside(); |
| 3567 | |
| 3568 | sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveConnectionToken(); |
| 3569 | sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveConnectionToken(); |
| 3570 | |
| 3571 | // Both applications should be marked as responsive, in any order |
| 3572 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 3573 | mFocusedWindow->getToken() == responsiveToken2); |
| 3574 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 3575 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 3576 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3577 | } |
| 3578 | |
| 3579 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 3580 | // We should also log an error to account for the dropped event (not tested here). |
| 3581 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 3582 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 3583 | tapOnFocusedWindow(); |
| 3584 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 3585 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 3586 | // Receive the events, but don't respond |
| 3587 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 3588 | ASSERT_TRUE(downEventSequenceNum); |
| 3589 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 3590 | ASSERT_TRUE(upEventSequenceNum); |
| 3591 | const std::chrono::duration timeout = |
| 3592 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3593 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3594 | |
| 3595 | // Tap once again |
| 3596 | // 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] | 3597 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3598 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3599 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3600 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3601 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3602 | FOCUSED_WINDOW_LOCATION)); |
| 3603 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 3604 | // valid touch target |
| 3605 | mUnfocusedWindow->assertNoEvents(); |
| 3606 | |
| 3607 | // Consume the first tap |
| 3608 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 3609 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 3610 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3611 | // The second tap did not go to the focused window |
| 3612 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3613 | // Since all events are finished, connection should be deemed healthy again |
| 3614 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mFocusedWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3615 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3616 | } |
| 3617 | |
| 3618 | // If you tap outside of all windows, there will not be ANR |
| 3619 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3620 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3621 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3622 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 3623 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3624 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3625 | } |
| 3626 | |
| 3627 | // Since the focused window is paused, tapping on it should not produce any events |
| 3628 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 3629 | mFocusedWindow->setPaused(true); |
| 3630 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 3631 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3632 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3633 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3634 | FOCUSED_WINDOW_LOCATION)); |
| 3635 | |
| 3636 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 3637 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3638 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 3639 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3640 | |
| 3641 | mFocusedWindow->assertNoEvents(); |
| 3642 | mUnfocusedWindow->assertNoEvents(); |
| 3643 | } |
| 3644 | |
| 3645 | /** |
| 3646 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 3647 | * not to to the focused window until the motion is processed. |
| 3648 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 3649 | * |
| 3650 | * Warning!!! |
| 3651 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 3652 | * and the injection timeout that we specify when injecting the key. |
| 3653 | * We must have the injection timeout (10ms) be smaller than |
| 3654 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 3655 | * |
| 3656 | * If that value changes, this test should also change. |
| 3657 | */ |
| 3658 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 3659 | // Set a long ANR timeout to prevent it from triggering |
| 3660 | mFocusedWindow->setDispatchingTimeout(2s); |
| 3661 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 3662 | |
| 3663 | tapOnUnfocusedWindow(); |
| 3664 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 3665 | ASSERT_TRUE(downSequenceNum); |
| 3666 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 3667 | ASSERT_TRUE(upSequenceNum); |
| 3668 | // Don't finish the events yet, and send a key |
| 3669 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 3670 | // window even if motions are still being processed. |
| 3671 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3672 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3673 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3674 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 3675 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3676 | // Key will not be sent to the window, yet, because the window is still processing events |
| 3677 | // and the key remains pending, waiting for the touch events to be processed |
| 3678 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 3679 | ASSERT_FALSE(keySequenceNum); |
| 3680 | |
| 3681 | // 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] | 3682 | mFocusedWindow->setFocusable(false); |
| 3683 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3684 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3685 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3686 | |
| 3687 | // Focus events should precede the key events |
| 3688 | mUnfocusedWindow->consumeFocusEvent(true); |
| 3689 | mFocusedWindow->consumeFocusEvent(false); |
| 3690 | |
| 3691 | // Finish the tap events, which should unblock dispatcher |
| 3692 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 3693 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 3694 | |
| 3695 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 3696 | // can finally go to the newly focused "mUnfocusedWindow". |
| 3697 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3698 | mFocusedWindow->assertNoEvents(); |
| 3699 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3700 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3701 | } |
| 3702 | |
| 3703 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 3704 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 3705 | // The other window should not be affected by that. |
| 3706 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 3707 | // Touch Window 1 |
| 3708 | NotifyMotionArgs motionArgs = |
| 3709 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3710 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); |
| 3711 | mDispatcher->notifyMotion(&motionArgs); |
| 3712 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 3713 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 3714 | |
| 3715 | // Touch Window 2 |
| 3716 | int32_t actionPointerDown = |
| 3717 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 3718 | |
| 3719 | motionArgs = |
| 3720 | generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3721 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); |
| 3722 | mDispatcher->notifyMotion(&motionArgs); |
| 3723 | |
| 3724 | const std::chrono::duration timeout = |
| 3725 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3726 | mFakePolicy->assertNotifyConnectionUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3727 | |
| 3728 | mUnfocusedWindow->consumeMotionDown(); |
| 3729 | mFocusedWindow->consumeMotionDown(); |
| 3730 | // Focused window may or may not receive ACTION_MOVE |
| 3731 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 3732 | InputEvent* event; |
| 3733 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 3734 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 3735 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 3736 | ASSERT_NE(nullptr, event); |
| 3737 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 3738 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 3739 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 3740 | mFocusedWindow->consumeMotionCancel(); |
| 3741 | } else { |
| 3742 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 3743 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3744 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3745 | mFakePolicy->assertNotifyConnectionResponsiveWasCalled(mFocusedWindow->getToken()); |
| 3746 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3747 | mUnfocusedWindow->assertNoEvents(); |
| 3748 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3749 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3750 | } |
| 3751 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 3752 | /** |
| 3753 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 3754 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 3755 | * |
| 3756 | * If the user touches another application during this time, the key should be dropped. |
| 3757 | * Next, if a new focused window comes in, without toggling the focused application, |
| 3758 | * then no ANR should occur. |
| 3759 | * |
| 3760 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 3761 | * but in some cases the policy may not update the focused application. |
| 3762 | */ |
| 3763 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 3764 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 3765 | std::make_shared<FakeApplicationHandle>(); |
| 3766 | focusedApplication->setDispatchingTimeout(60ms); |
| 3767 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 3768 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 3769 | mFocusedWindow->setFocusable(false); |
| 3770 | |
| 3771 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 3772 | mFocusedWindow->consumeFocusEvent(false); |
| 3773 | |
| 3774 | // Send a key. The ANR timer should start because there is no focused window. |
| 3775 | // 'focusedApplication' will get blamed if this timer completes. |
| 3776 | // 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] | 3777 | InputEventInjectionResult result = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 3778 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3779 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 3780 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 3781 | |
| 3782 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 3783 | // then the injected touches won't cause the focused event to get dropped. |
| 3784 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 3785 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 3786 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 3787 | // For this test, it means that the key would get delivered to the window once it becomes |
| 3788 | // focused. |
| 3789 | std::this_thread::sleep_for(10ms); |
| 3790 | |
| 3791 | // Touch unfocused window. This should force the pending key to get dropped. |
| 3792 | NotifyMotionArgs motionArgs = |
| 3793 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3794 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); |
| 3795 | mDispatcher->notifyMotion(&motionArgs); |
| 3796 | |
| 3797 | // We do not consume the motion right away, because that would require dispatcher to first |
| 3798 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 3799 | // Set the focused window first. |
| 3800 | mFocusedWindow->setFocusable(true); |
| 3801 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 3802 | setFocusedWindow(mFocusedWindow); |
| 3803 | mFocusedWindow->consumeFocusEvent(true); |
| 3804 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 3805 | // to another application. This could be a bug / behaviour in the policy. |
| 3806 | |
| 3807 | mUnfocusedWindow->consumeMotionDown(); |
| 3808 | |
| 3809 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3810 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 3811 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 3812 | } |
| 3813 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 3814 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 3815 | // that has feature NO_INPUT_CHANNEL. |
| 3816 | // Layout: |
| 3817 | // Top (closest to user) |
| 3818 | // mNoInputWindow (above all windows) |
| 3819 | // mBottomWindow |
| 3820 | // Bottom (furthest from user) |
| 3821 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 3822 | virtual void SetUp() override { |
| 3823 | InputDispatcherTest::SetUp(); |
| 3824 | |
| 3825 | mApplication = std::make_shared<FakeApplicationHandle>(); |
| 3826 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, |
| 3827 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
| 3828 | std::make_optional<sp<IBinder>>(nullptr) /*token*/); |
| 3829 | |
| 3830 | mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3831 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3832 | // It's perfectly valid for this window to not have an associated input channel |
| 3833 | |
| 3834 | mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window", |
| 3835 | ADISPLAY_ID_DEFAULT); |
| 3836 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3837 | |
| 3838 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 3839 | } |
| 3840 | |
| 3841 | protected: |
| 3842 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 3843 | sp<FakeWindowHandle> mNoInputWindow; |
| 3844 | sp<FakeWindowHandle> mBottomWindow; |
| 3845 | }; |
| 3846 | |
| 3847 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 3848 | PointF touchedPoint = {10, 10}; |
| 3849 | |
| 3850 | NotifyMotionArgs motionArgs = |
| 3851 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3852 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 3853 | mDispatcher->notifyMotion(&motionArgs); |
| 3854 | |
| 3855 | mNoInputWindow->assertNoEvents(); |
| 3856 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 3857 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 3858 | // and therefore should prevent mBottomWindow from receiving touches |
| 3859 | mBottomWindow->assertNoEvents(); |
| 3860 | } |
| 3861 | |
| 3862 | /** |
| 3863 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 3864 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 3865 | */ |
| 3866 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 3867 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
| 3868 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, |
| 3869 | "Window with input channel and NO_INPUT_CHANNEL", |
| 3870 | ADISPLAY_ID_DEFAULT); |
| 3871 | |
| 3872 | mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3873 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3874 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 3875 | |
| 3876 | PointF touchedPoint = {10, 10}; |
| 3877 | |
| 3878 | NotifyMotionArgs motionArgs = |
| 3879 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3880 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 3881 | mDispatcher->notifyMotion(&motionArgs); |
| 3882 | |
| 3883 | mNoInputWindow->assertNoEvents(); |
| 3884 | mBottomWindow->assertNoEvents(); |
| 3885 | } |
| 3886 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3887 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 3888 | protected: |
| 3889 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 3890 | sp<FakeWindowHandle> mWindow; |
| 3891 | sp<FakeWindowHandle> mMirror; |
| 3892 | |
| 3893 | virtual void SetUp() override { |
| 3894 | InputDispatcherTest::SetUp(); |
| 3895 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 3896 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 3897 | mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT, |
| 3898 | mWindow->getToken()); |
| 3899 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 3900 | mWindow->setFocusable(true); |
| 3901 | mMirror->setFocusable(true); |
| 3902 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 3903 | } |
| 3904 | }; |
| 3905 | |
| 3906 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 3907 | // Request focus on a mirrored window |
| 3908 | setFocusedWindow(mMirror); |
| 3909 | |
| 3910 | // window gets focused |
| 3911 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3912 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3913 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3914 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 3915 | } |
| 3916 | |
| 3917 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 3918 | // focusable. |
| 3919 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 3920 | setFocusedWindow(mMirror); |
| 3921 | |
| 3922 | // window gets focused |
| 3923 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3924 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3925 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3926 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3927 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 3928 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3929 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 3930 | |
| 3931 | mMirror->setFocusable(false); |
| 3932 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 3933 | |
| 3934 | // window loses focus since one of the windows associated with the token in not focusable |
| 3935 | mWindow->consumeFocusEvent(false); |
| 3936 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3937 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3938 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3939 | mWindow->assertNoEvents(); |
| 3940 | } |
| 3941 | |
| 3942 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 3943 | // invisible. |
| 3944 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 3945 | setFocusedWindow(mMirror); |
| 3946 | |
| 3947 | // window gets focused |
| 3948 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3949 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3950 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3951 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3952 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 3953 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3954 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 3955 | |
| 3956 | mMirror->setVisible(false); |
| 3957 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 3958 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3959 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3960 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3961 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3962 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 3963 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3964 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 3965 | |
| 3966 | mWindow->setVisible(false); |
| 3967 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 3968 | |
| 3969 | // window loses focus only after all windows associated with the token become invisible. |
| 3970 | mWindow->consumeFocusEvent(false); |
| 3971 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3972 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3973 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3974 | mWindow->assertNoEvents(); |
| 3975 | } |
| 3976 | |
| 3977 | // A focused & mirrored window remains focused until both windows are removed. |
| 3978 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 3979 | setFocusedWindow(mMirror); |
| 3980 | |
| 3981 | // window gets focused |
| 3982 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3983 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3984 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3985 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3986 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 3987 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3988 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 3989 | |
| 3990 | // single window is removed but the window token remains focused |
| 3991 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 3992 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3993 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3994 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3995 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3996 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 3997 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3998 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 3999 | |
| 4000 | // Both windows are removed |
| 4001 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 4002 | mWindow->consumeFocusEvent(false); |
| 4003 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4004 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 4005 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4006 | mWindow->assertNoEvents(); |
| 4007 | } |
| 4008 | |
| 4009 | // Focus request can be pending until one window becomes visible. |
| 4010 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 4011 | // Request focus on an invisible mirror. |
| 4012 | mWindow->setVisible(false); |
| 4013 | mMirror->setVisible(false); |
| 4014 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 4015 | setFocusedWindow(mMirror); |
| 4016 | |
| 4017 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4018 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4019 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4020 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4021 | |
| 4022 | mMirror->setVisible(true); |
| 4023 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 4024 | |
| 4025 | // window gets focused |
| 4026 | mWindow->consumeFocusEvent(true); |
| 4027 | // window gets the pending key event |
| 4028 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4029 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4030 | |
| 4031 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 4032 | protected: |
| 4033 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 4034 | sp<FakeWindowHandle> mWindow; |
| 4035 | sp<FakeWindowHandle> mSecondWindow; |
| 4036 | |
| 4037 | void SetUp() override { |
| 4038 | InputDispatcherTest::SetUp(); |
| 4039 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 4040 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 4041 | mWindow->setFocusable(true); |
| 4042 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
| 4043 | mSecondWindow->setFocusable(true); |
| 4044 | |
| 4045 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 4046 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 4047 | |
| 4048 | setFocusedWindow(mWindow); |
| 4049 | mWindow->consumeFocusEvent(true); |
| 4050 | } |
| 4051 | |
| 4052 | void notifyPointerCaptureChanged(bool enabled) { |
| 4053 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled); |
| 4054 | mDispatcher->notifyPointerCaptureChanged(&args); |
| 4055 | } |
| 4056 | |
| 4057 | void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) { |
| 4058 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
| 4059 | mFakePolicy->waitForSetPointerCapture(enabled); |
| 4060 | notifyPointerCaptureChanged(enabled); |
| 4061 | window->consumeCaptureEvent(enabled); |
| 4062 | } |
| 4063 | }; |
| 4064 | |
| 4065 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 4066 | // Ensure that capture cannot be obtained for unfocused windows. |
| 4067 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 4068 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 4069 | mSecondWindow->assertNoEvents(); |
| 4070 | |
| 4071 | // Ensure that capture can be enabled from the focus window. |
| 4072 | requestAndVerifyPointerCapture(mWindow, true); |
| 4073 | |
| 4074 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 4075 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 4076 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 4077 | |
| 4078 | // Ensure that capture can be disabled from the window with capture. |
| 4079 | requestAndVerifyPointerCapture(mWindow, false); |
| 4080 | } |
| 4081 | |
| 4082 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
| 4083 | requestAndVerifyPointerCapture(mWindow, true); |
| 4084 | |
| 4085 | setFocusedWindow(mSecondWindow); |
| 4086 | |
| 4087 | // Ensure that the capture disabled event was sent first. |
| 4088 | mWindow->consumeCaptureEvent(false); |
| 4089 | mWindow->consumeFocusEvent(false); |
| 4090 | mSecondWindow->consumeFocusEvent(true); |
| 4091 | mFakePolicy->waitForSetPointerCapture(false); |
| 4092 | |
| 4093 | // Ensure that additional state changes from InputReader are not sent to the window. |
| 4094 | notifyPointerCaptureChanged(false); |
| 4095 | notifyPointerCaptureChanged(true); |
| 4096 | notifyPointerCaptureChanged(false); |
| 4097 | mWindow->assertNoEvents(); |
| 4098 | mSecondWindow->assertNoEvents(); |
| 4099 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 4100 | } |
| 4101 | |
| 4102 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
| 4103 | requestAndVerifyPointerCapture(mWindow, true); |
| 4104 | |
| 4105 | // InputReader unexpectedly disables and enables pointer capture. |
| 4106 | notifyPointerCaptureChanged(false); |
| 4107 | notifyPointerCaptureChanged(true); |
| 4108 | |
| 4109 | // Ensure that Pointer Capture is disabled. |
| 4110 | mWindow->consumeCaptureEvent(false); |
| 4111 | mWindow->assertNoEvents(); |
| 4112 | } |
| 4113 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 4114 | } // namespace android::inputdispatcher |