| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2010 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 17 | #include "../dispatcher/InputDispatcher.h" | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 18 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 19 | #include <android-base/stringprintf.h> | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 20 | #include <android-base/thread_annotations.h> | 
| Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 21 | #include <binder/Binder.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 22 | #include <gtest/gtest.h> | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 23 | #include <input/Input.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 24 | #include <linux/input.h> | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 25 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 26 | #include <cinttypes> | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 27 | #include <thread> | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 28 | #include <unordered_set> | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 29 | #include <vector> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 30 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 31 | using android::base::StringPrintf; | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 32 | using android::gui::FocusRequest; | 
|  | 33 | using android::gui::TouchOcclusionMode; | 
|  | 34 | using android::gui::WindowInfo; | 
|  | 35 | using android::gui::WindowInfoHandle; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 36 | using android::os::InputEventInjectionResult; | 
|  | 37 | using android::os::InputEventInjectionSync; | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 38 | using namespace android::flag_operators; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 39 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 40 | namespace android::inputdispatcher { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 41 |  | 
|  | 42 | // An arbitrary time value. | 
|  | 43 | static const nsecs_t ARBITRARY_TIME = 1234; | 
|  | 44 |  | 
|  | 45 | // An arbitrary device id. | 
|  | 46 | static const int32_t DEVICE_ID = 1; | 
|  | 47 |  | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 48 | // An arbitrary display id. | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 49 | static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 50 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 51 | // An arbitrary injector pid / uid pair that has permission to inject events. | 
|  | 52 | static const int32_t INJECTOR_PID = 999; | 
|  | 53 | static const int32_t INJECTOR_UID = 1001; | 
|  | 54 |  | 
| Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 55 | // An arbitrary pid of the gesture monitor window | 
|  | 56 | static constexpr int32_t MONITOR_PID = 2001; | 
|  | 57 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 58 | struct PointF { | 
|  | 59 | float x; | 
|  | 60 | float y; | 
|  | 61 | }; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 62 |  | 
| Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 63 | /** | 
|  | 64 | * Return a DOWN key event with KEYCODE_A. | 
|  | 65 | */ | 
|  | 66 | static KeyEvent getTestKeyEvent() { | 
|  | 67 | KeyEvent event; | 
|  | 68 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 69 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 70 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, | 
|  | 71 | ARBITRARY_TIME, ARBITRARY_TIME); | 
| Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 72 | return event; | 
|  | 73 | } | 
|  | 74 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 75 | // --- FakeInputDispatcherPolicy --- | 
|  | 76 |  | 
|  | 77 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { | 
|  | 78 | InputDispatcherConfiguration mConfig; | 
|  | 79 |  | 
|  | 80 | protected: | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 81 | virtual ~FakeInputDispatcherPolicy() {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 82 |  | 
|  | 83 | public: | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 84 | FakeInputDispatcherPolicy() {} | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 85 |  | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 86 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { | 
| Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 87 | assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action, | 
|  | 88 | args.displayId); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 89 | } | 
|  | 90 |  | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 91 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) { | 
| Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 92 | assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action, | 
|  | 93 | args.displayId); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 96 | void assertFilterInputEventWasNotCalled() { | 
|  | 97 | std::scoped_lock lock(mLock); | 
|  | 98 | ASSERT_EQ(nullptr, mFilteredEvent); | 
|  | 99 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 100 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 101 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 102 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 103 | ASSERT_TRUE(mConfigurationChangedTime) | 
|  | 104 | << "Timed out waiting for configuration changed call"; | 
|  | 105 | ASSERT_EQ(*mConfigurationChangedTime, when); | 
|  | 106 | mConfigurationChangedTime = std::nullopt; | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 110 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 111 | ASSERT_TRUE(mLastNotifySwitch); | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 112 | // We do not check id because it is not exposed to the policy | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 113 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); | 
|  | 114 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); | 
|  | 115 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); | 
|  | 116 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); | 
|  | 117 | mLastNotifySwitch = std::nullopt; | 
|  | 118 | } | 
|  | 119 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 120 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 121 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 122 | ASSERT_EQ(touchedToken, mOnPointerDownToken); | 
|  | 123 | mOnPointerDownToken.clear(); | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | void assertOnPointerDownWasNotCalled() { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 127 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 128 | ASSERT_TRUE(mOnPointerDownToken == nullptr) | 
|  | 129 | << "Expected onPointerDownOutsideFocus to not have been called"; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 130 | } | 
|  | 131 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 132 | // This function must be called soon after the expected ANR timer starts, | 
|  | 133 | // because we are also checking how much time has passed. | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 134 | void assertNotifyNoFocusedWindowAnrWasCalled( | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 135 | std::chrono::nanoseconds timeout, | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 136 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { | 
|  | 137 | std::shared_ptr<InputApplicationHandle> application; | 
|  | 138 | { // acquire lock | 
|  | 139 | std::unique_lock lock(mLock); | 
|  | 140 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 141 | ASSERT_NO_FATAL_FAILURE( | 
|  | 142 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); | 
|  | 143 | } // release lock | 
|  | 144 | ASSERT_EQ(expectedApplication, application); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 145 | } | 
|  | 146 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 147 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, | 
|  | 148 | const sp<IBinder>& expectedConnectionToken) { | 
|  | 149 | sp<IBinder> connectionToken = getUnresponsiveWindowToken(timeout); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 150 | ASSERT_EQ(expectedConnectionToken, connectionToken); | 
|  | 151 | } | 
|  | 152 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 153 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) { | 
|  | 154 | sp<IBinder> connectionToken = getResponsiveWindowToken(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 155 | ASSERT_EQ(expectedConnectionToken, connectionToken); | 
|  | 156 | } | 
|  | 157 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 158 | void assertNotifyMonitorUnresponsiveWasCalled(std::chrono::nanoseconds timeout) { | 
|  | 159 | int32_t pid = getUnresponsiveMonitorPid(timeout); | 
|  | 160 | ASSERT_EQ(MONITOR_PID, pid); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 161 | } | 
|  | 162 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 163 | void assertNotifyMonitorResponsiveWasCalled() { | 
|  | 164 | int32_t pid = getResponsiveMonitorPid(); | 
|  | 165 | ASSERT_EQ(MONITOR_PID, pid); | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 169 | std::unique_lock lock(mLock); | 
|  | 170 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 171 | return getAnrTokenLockedInterruptible(timeout, mAnrWindowTokens, lock); | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | sp<IBinder> getResponsiveWindowToken() { | 
|  | 175 | std::unique_lock lock(mLock); | 
|  | 176 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 177 | return getAnrTokenLockedInterruptible(0s, mResponsiveWindowTokens, lock); | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | int32_t getUnresponsiveMonitorPid(std::chrono::nanoseconds timeout) { | 
|  | 181 | std::unique_lock lock(mLock); | 
|  | 182 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 183 | return getAnrTokenLockedInterruptible(timeout, mAnrMonitorPids, lock); | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | int32_t getResponsiveMonitorPid() { | 
|  | 187 | std::unique_lock lock(mLock); | 
|  | 188 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 189 | return getAnrTokenLockedInterruptible(0s, mResponsiveMonitorPids, lock); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 190 | } | 
|  | 191 |  | 
|  | 192 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait | 
|  | 193 | // for a specific container to become non-empty. When the container is non-empty, return the | 
|  | 194 | // first entry from the container and erase it. | 
|  | 195 | template <class T> | 
|  | 196 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, | 
|  | 197 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { | 
|  | 198 | const std::chrono::time_point start = std::chrono::steady_clock::now(); | 
|  | 199 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 200 |  | 
|  | 201 | // If there is an ANR, Dispatcher won't be idle because there are still events | 
|  | 202 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle | 
|  | 203 | // before checking if ANR was called. | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 204 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need | 
|  | 205 | // to provide it some time to act. 100ms seems reasonable. | 
|  | 206 | mNotifyAnr.wait_for(lock, timeToWait, | 
|  | 207 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 208 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 209 | if (storage.empty()) { | 
|  | 210 | ADD_FAILURE() << "Did not receive the ANR callback"; | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 211 | return {}; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 212 | } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 213 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because | 
|  | 214 | // the dispatcher started counting before this function was called | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 215 | if (std::chrono::abs(timeout - waited) > 100ms) { | 
|  | 216 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " | 
|  | 217 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() | 
|  | 218 | << "ms, but waited " | 
|  | 219 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() | 
|  | 220 | << "ms instead"; | 
|  | 221 | } | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 222 | T token = storage.front(); | 
|  | 223 | storage.pop(); | 
|  | 224 | return token; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 225 | } | 
|  | 226 |  | 
|  | 227 | void assertNotifyAnrWasNotCalled() { | 
|  | 228 | std::scoped_lock lock(mLock); | 
|  | 229 | ASSERT_TRUE(mAnrApplications.empty()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 230 | ASSERT_TRUE(mAnrWindowTokens.empty()); | 
|  | 231 | ASSERT_TRUE(mAnrMonitorPids.empty()); | 
|  | 232 | ASSERT_TRUE(mResponsiveWindowTokens.empty()) | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 233 | << "ANR was not called, but please also consume the 'connection is responsive' " | 
|  | 234 | "signal"; | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 235 | ASSERT_TRUE(mResponsiveMonitorPids.empty()) | 
|  | 236 | << "Monitor ANR was not called, but please also consume the 'monitor is responsive'" | 
|  | 237 | " signal"; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 238 | } | 
|  | 239 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 240 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { | 
|  | 241 | mConfig.keyRepeatTimeout = timeout; | 
|  | 242 | mConfig.keyRepeatDelay = delay; | 
|  | 243 | } | 
|  | 244 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 245 | void waitForSetPointerCapture(bool enabled) { | 
|  | 246 | std::unique_lock lock(mLock); | 
|  | 247 | base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 248 |  | 
|  | 249 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, | 
|  | 250 | [this, enabled]() REQUIRES(mLock) { | 
|  | 251 | return mPointerCaptureEnabled && | 
|  | 252 | *mPointerCaptureEnabled == | 
|  | 253 | enabled; | 
|  | 254 | })) { | 
|  | 255 | FAIL() << "Timed out waiting for setPointerCapture(" << enabled << ") to be called."; | 
|  | 256 | } | 
|  | 257 | mPointerCaptureEnabled.reset(); | 
|  | 258 | } | 
|  | 259 |  | 
|  | 260 | void assertSetPointerCaptureNotCalled() { | 
|  | 261 | std::unique_lock lock(mLock); | 
|  | 262 | base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 263 |  | 
|  | 264 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { | 
|  | 265 | FAIL() << "Expected setPointerCapture(enabled) to not be called, but was called. " | 
|  | 266 | "enabled = " | 
|  | 267 | << *mPointerCaptureEnabled; | 
|  | 268 | } | 
|  | 269 | mPointerCaptureEnabled.reset(); | 
|  | 270 | } | 
|  | 271 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 272 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { | 
|  | 273 | std::scoped_lock lock(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 274 | ASSERT_TRUE(mNotifyDropWindowWasCalled); | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 275 | ASSERT_EQ(targetToken, mDropTargetWindowToken); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 276 | mNotifyDropWindowWasCalled = false; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 277 | } | 
|  | 278 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 279 | private: | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 280 | std::mutex mLock; | 
|  | 281 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); | 
|  | 282 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); | 
|  | 283 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); | 
|  | 284 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 285 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 286 | std::condition_variable mPointerCaptureChangedCondition; | 
|  | 287 | std::optional<bool> mPointerCaptureEnabled GUARDED_BY(mLock); | 
|  | 288 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 289 | // ANR handling | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 290 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 291 | std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock); | 
|  | 292 | std::queue<sp<IBinder>> mResponsiveWindowTokens GUARDED_BY(mLock); | 
|  | 293 | std::queue<int32_t> mAnrMonitorPids GUARDED_BY(mLock); | 
|  | 294 | std::queue<int32_t> mResponsiveMonitorPids GUARDED_BY(mLock); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 295 | std::condition_variable mNotifyAnr; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 296 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 297 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 298 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 299 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 300 | void notifyConfigurationChanged(nsecs_t when) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 301 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 302 | mConfigurationChangedTime = when; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 303 | } | 
|  | 304 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 305 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, const std::string&) override { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 306 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 307 | mAnrWindowTokens.push(connectionToken); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 308 | mNotifyAnr.notify_all(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 309 | } | 
|  | 310 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 311 | void notifyMonitorUnresponsive(int32_t pid, const std::string&) override { | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 312 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 313 | mAnrMonitorPids.push(pid); | 
|  | 314 | mNotifyAnr.notify_all(); | 
|  | 315 | } | 
|  | 316 |  | 
|  | 317 | void notifyWindowResponsive(const sp<IBinder>& connectionToken) override { | 
|  | 318 | std::scoped_lock lock(mLock); | 
|  | 319 | mResponsiveWindowTokens.push(connectionToken); | 
|  | 320 | mNotifyAnr.notify_all(); | 
|  | 321 | } | 
|  | 322 |  | 
|  | 323 | void notifyMonitorResponsive(int32_t pid) override { | 
|  | 324 | std::scoped_lock lock(mLock); | 
|  | 325 | mResponsiveMonitorPids.push(pid); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 326 | mNotifyAnr.notify_all(); | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | void notifyNoFocusedWindowAnr( | 
|  | 330 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { | 
|  | 331 | std::scoped_lock lock(mLock); | 
|  | 332 | mAnrApplications.push(applicationHandle); | 
|  | 333 | mNotifyAnr.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 334 | } | 
|  | 335 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 336 | void notifyInputChannelBroken(const sp<IBinder>&) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 337 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 338 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} | 
| Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 339 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 340 | void notifyUntrustedTouch(const std::string& obscuringPackage) override {} | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 341 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, | 
|  | 342 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, | 
|  | 343 | const std::vector<float>& values) override {} | 
|  | 344 |  | 
|  | 345 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, | 
|  | 346 | InputDeviceSensorAccuracy accuracy) override {} | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 347 |  | 
| Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 348 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} | 
|  | 349 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 350 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 351 | *outConfig = mConfig; | 
|  | 352 | } | 
|  | 353 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 354 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 355 | std::scoped_lock lock(mLock); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 356 | switch (inputEvent->getType()) { | 
|  | 357 | case AINPUT_EVENT_TYPE_KEY: { | 
|  | 358 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 359 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 360 | break; | 
|  | 361 | } | 
|  | 362 |  | 
|  | 363 | case AINPUT_EVENT_TYPE_MOTION: { | 
|  | 364 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 365 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 366 | break; | 
|  | 367 | } | 
|  | 368 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 369 | return true; | 
|  | 370 | } | 
|  | 371 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 372 | void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 373 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 374 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 375 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 376 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 377 | return 0; | 
|  | 378 | } | 
|  | 379 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 380 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 381 | return false; | 
|  | 382 | } | 
|  | 383 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 384 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, | 
|  | 385 | uint32_t policyFlags) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 386 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 387 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is | 
|  | 388 | * essentially a passthrough for notifySwitch. | 
|  | 389 | */ | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 390 | mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 391 | } | 
|  | 392 |  | 
| Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 393 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 394 |  | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 395 | bool checkInjectEventsPermissionNonReentrant(int32_t pid, int32_t uid) override { | 
|  | 396 | return pid == INJECTOR_PID && uid == INJECTOR_UID; | 
|  | 397 | } | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 398 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 399 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 400 | std::scoped_lock lock(mLock); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 401 | mOnPointerDownToken = newToken; | 
|  | 402 | } | 
|  | 403 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 404 | void setPointerCapture(bool enabled) override { | 
|  | 405 | std::scoped_lock lock(mLock); | 
|  | 406 | mPointerCaptureEnabled = {enabled}; | 
|  | 407 | mPointerCaptureChangedCondition.notify_all(); | 
|  | 408 | } | 
|  | 409 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 410 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { | 
|  | 411 | std::scoped_lock lock(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 412 | mNotifyDropWindowWasCalled = true; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 413 | mDropTargetWindowToken = token; | 
|  | 414 | } | 
|  | 415 |  | 
| Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 416 | void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action, | 
|  | 417 | int32_t displayId) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 418 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 419 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; | 
|  | 420 | ASSERT_EQ(mFilteredEvent->getType(), type); | 
|  | 421 |  | 
|  | 422 | if (type == AINPUT_EVENT_TYPE_KEY) { | 
|  | 423 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent); | 
|  | 424 | EXPECT_EQ(keyEvent.getEventTime(), eventTime); | 
|  | 425 | EXPECT_EQ(keyEvent.getAction(), action); | 
|  | 426 | EXPECT_EQ(keyEvent.getDisplayId(), displayId); | 
|  | 427 | } else if (type == AINPUT_EVENT_TYPE_MOTION) { | 
|  | 428 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent); | 
|  | 429 | EXPECT_EQ(motionEvent.getEventTime(), eventTime); | 
|  | 430 | EXPECT_EQ(motionEvent.getAction(), action); | 
|  | 431 | EXPECT_EQ(motionEvent.getDisplayId(), displayId); | 
|  | 432 | } else { | 
|  | 433 | FAIL() << "Unknown type: " << type; | 
|  | 434 | } | 
|  | 435 |  | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 436 | mFilteredEvent = nullptr; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 437 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 438 | }; | 
|  | 439 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 440 | // --- InputDispatcherTest --- | 
|  | 441 |  | 
|  | 442 | class InputDispatcherTest : public testing::Test { | 
|  | 443 | protected: | 
|  | 444 | sp<FakeInputDispatcherPolicy> mFakePolicy; | 
|  | 445 | sp<InputDispatcher> mDispatcher; | 
|  | 446 |  | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 447 | void SetUp() override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 448 | mFakePolicy = new FakeInputDispatcherPolicy(); | 
|  | 449 | mDispatcher = new InputDispatcher(mFakePolicy); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 450 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 451 | // Start InputDispatcher thread | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 452 | ASSERT_EQ(OK, mDispatcher->start()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 453 | } | 
|  | 454 |  | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 455 | void TearDown() override { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 456 | ASSERT_EQ(OK, mDispatcher->stop()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 457 | mFakePolicy.clear(); | 
|  | 458 | mDispatcher.clear(); | 
|  | 459 | } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 460 |  | 
|  | 461 | /** | 
|  | 462 | * Used for debugging when writing the test | 
|  | 463 | */ | 
|  | 464 | void dumpDispatcherState() { | 
|  | 465 | std::string dump; | 
|  | 466 | mDispatcher->dump(dump); | 
|  | 467 | std::stringstream ss(dump); | 
|  | 468 | std::string to; | 
|  | 469 |  | 
|  | 470 | while (std::getline(ss, to, '\n')) { | 
|  | 471 | ALOGE("%s", to.c_str()); | 
|  | 472 | } | 
|  | 473 | } | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 474 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 475 | void setFocusedWindow(const sp<WindowInfoHandle>& window, | 
|  | 476 | const sp<WindowInfoHandle>& focusedWindow = nullptr) { | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 477 | FocusRequest request; | 
|  | 478 | request.token = window->getToken(); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 479 | request.windowName = window->getName(); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 480 | if (focusedWindow) { | 
|  | 481 | request.focusedToken = focusedWindow->getToken(); | 
|  | 482 | } | 
|  | 483 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 484 | request.displayId = window->getInfo()->displayId; | 
|  | 485 | mDispatcher->setFocusedWindow(request); | 
|  | 486 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 487 | }; | 
|  | 488 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 489 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { | 
|  | 490 | KeyEvent event; | 
|  | 491 |  | 
|  | 492 | // Rejects undefined key actions. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 493 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 494 | INVALID_HMAC, | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 495 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, | 
|  | 496 | ARBITRARY_TIME); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 497 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 498 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 499 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 500 | << "Should reject key events with undefined action."; | 
|  | 501 |  | 
|  | 502 | // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 503 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 504 | INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 505 | ARBITRARY_TIME, ARBITRARY_TIME); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 506 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 507 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 508 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 509 | << "Should reject key events with ACTION_MULTIPLE."; | 
|  | 510 | } | 
|  | 511 |  | 
|  | 512 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { | 
|  | 513 | MotionEvent event; | 
|  | 514 | PointerProperties pointerProperties[MAX_POINTERS + 1]; | 
|  | 515 | PointerCoords pointerCoords[MAX_POINTERS + 1]; | 
|  | 516 | for (int i = 0; i <= MAX_POINTERS; i++) { | 
|  | 517 | pointerProperties[i].clear(); | 
|  | 518 | pointerProperties[i].id = i; | 
|  | 519 | pointerCoords[i].clear(); | 
|  | 520 | } | 
|  | 521 |  | 
| Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 522 | // Some constants commonly used below | 
|  | 523 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; | 
|  | 524 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; | 
|  | 525 | constexpr int32_t metaState = AMETA_NONE; | 
|  | 526 | constexpr MotionClassification classification = MotionClassification::NONE; | 
|  | 527 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 528 | ui::Transform identityTransform; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 529 | // Rejects undefined motion actions. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 530 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 531 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, | 
|  | 532 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 533 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0, | 
|  | 534 | INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 535 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 536 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 537 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 538 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 539 | << "Should reject motion events with undefined action."; | 
|  | 540 |  | 
|  | 541 | // Rejects pointer down with invalid index. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 542 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 543 | AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 544 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 545 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 546 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 547 | ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, | 
|  | 548 | ARBITRARY_TIME, ARBITRARY_TIME, | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 549 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 550 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 551 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 552 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 553 | << "Should reject motion events with pointer down index too large."; | 
|  | 554 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 555 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 556 | AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 557 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 558 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 559 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 560 | ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, | 
|  | 561 | ARBITRARY_TIME, ARBITRARY_TIME, | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 562 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 563 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 564 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 565 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 566 | << "Should reject motion events with pointer down index too small."; | 
|  | 567 |  | 
|  | 568 | // Rejects pointer up with invalid index. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 569 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 570 | AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 571 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 572 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 573 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 574 | ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, | 
|  | 575 | ARBITRARY_TIME, ARBITRARY_TIME, | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 576 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 577 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 578 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 579 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 580 | << "Should reject motion events with pointer up index too large."; | 
|  | 581 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 582 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 583 | AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 584 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 585 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 586 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 587 | ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, | 
|  | 588 | ARBITRARY_TIME, ARBITRARY_TIME, | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 589 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 590 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 591 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 592 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 593 | << "Should reject motion events with pointer up index too small."; | 
|  | 594 |  | 
|  | 595 | // Rejects motion events with invalid number of pointers. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 596 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 597 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 598 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 599 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0, | 
|  | 600 | INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 601 | /*pointerCount*/ 0, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 602 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 603 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 604 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 605 | << "Should reject motion events with 0 pointers."; | 
|  | 606 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 607 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 608 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 609 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 610 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0, | 
|  | 611 | INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 612 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 613 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 614 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 615 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 616 | << "Should reject motion events with more than MAX_POINTERS pointers."; | 
|  | 617 |  | 
|  | 618 | // Rejects motion events with invalid pointer ids. | 
|  | 619 | pointerProperties[0].id = -1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 620 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 621 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 622 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 623 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0, | 
|  | 624 | INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 625 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 626 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 627 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 628 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 629 | << "Should reject motion events with pointer ids less than 0."; | 
|  | 630 |  | 
|  | 631 | pointerProperties[0].id = MAX_POINTER_ID + 1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 632 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 633 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 634 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 635 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0, | 
|  | 636 | INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 637 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 638 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 639 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 640 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 641 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; | 
|  | 642 |  | 
|  | 643 | // Rejects motion events with duplicate pointer ids. | 
|  | 644 | pointerProperties[0].id = 1; | 
|  | 645 | pointerProperties[1].id = 1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 646 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 647 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 648 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 649 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0, | 
|  | 650 | INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 651 | /*pointerCount*/ 2, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 652 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 653 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 654 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 655 | << "Should reject motion events with duplicate pointer ids."; | 
|  | 656 | } | 
|  | 657 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 658 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ | 
|  | 659 |  | 
|  | 660 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { | 
|  | 661 | constexpr nsecs_t eventTime = 20; | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 662 | NotifyConfigurationChangedArgs args(10 /*id*/, eventTime); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 663 | mDispatcher->notifyConfigurationChanged(&args); | 
|  | 664 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 665 |  | 
|  | 666 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); | 
|  | 667 | } | 
|  | 668 |  | 
|  | 669 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 670 | NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/, | 
|  | 671 | 2 /*switchMask*/); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 672 | mDispatcher->notifySwitch(&args); | 
|  | 673 |  | 
|  | 674 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener | 
|  | 675 | args.policyFlags |= POLICY_FLAG_TRUSTED; | 
|  | 676 | mFakePolicy->assertNotifySwitchWasCalled(args); | 
|  | 677 | } | 
|  | 678 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 679 | // --- InputDispatcherTest SetInputWindowTest --- | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 680 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 681 | static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 682 |  | 
|  | 683 | class FakeApplicationHandle : public InputApplicationHandle { | 
|  | 684 | public: | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 685 | FakeApplicationHandle() { | 
|  | 686 | mInfo.name = "Fake Application"; | 
|  | 687 | mInfo.token = new BBinder(); | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 688 | mInfo.dispatchingTimeoutMillis = | 
|  | 689 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 690 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 691 | virtual ~FakeApplicationHandle() {} | 
|  | 692 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 693 | virtual bool updateInfo() override { return true; } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 694 |  | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 695 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { | 
|  | 696 | mInfo.dispatchingTimeoutMillis = timeout.count(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 697 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 698 | }; | 
|  | 699 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 700 | class FakeInputReceiver { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 701 | public: | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 702 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 703 | : mName(name) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 704 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 705 | } | 
|  | 706 |  | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 707 | InputEvent* consume() { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 708 | InputEvent* event; | 
|  | 709 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); | 
|  | 710 | if (!consumeSeq) { | 
|  | 711 | return nullptr; | 
|  | 712 | } | 
|  | 713 | finishEvent(*consumeSeq); | 
|  | 714 | return event; | 
|  | 715 | } | 
|  | 716 |  | 
|  | 717 | /** | 
|  | 718 | * Receive an event without acknowledging it. | 
|  | 719 | * Return the sequence number that could later be used to send finished signal. | 
|  | 720 | */ | 
|  | 721 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 722 | uint32_t consumeSeq; | 
|  | 723 | InputEvent* event; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 724 |  | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 725 | std::chrono::time_point start = std::chrono::steady_clock::now(); | 
|  | 726 | status_t status = WOULD_BLOCK; | 
|  | 727 | while (status == WOULD_BLOCK) { | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 728 | status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 729 | &event); | 
|  | 730 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; | 
|  | 731 | if (elapsed > 100ms) { | 
|  | 732 | break; | 
|  | 733 | } | 
|  | 734 | } | 
|  | 735 |  | 
|  | 736 | if (status == WOULD_BLOCK) { | 
|  | 737 | // Just means there's no event available. | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 738 | return std::nullopt; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 739 | } | 
|  | 740 |  | 
|  | 741 | if (status != OK) { | 
|  | 742 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 743 | return std::nullopt; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 744 | } | 
|  | 745 | if (event == nullptr) { | 
|  | 746 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 747 | return std::nullopt; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 748 | } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 749 | if (outEvent != nullptr) { | 
|  | 750 | *outEvent = event; | 
|  | 751 | } | 
|  | 752 | return consumeSeq; | 
|  | 753 | } | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 754 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 755 | /** | 
|  | 756 | * To be used together with "receiveEvent" to complete the consumption of an event. | 
|  | 757 | */ | 
|  | 758 | void finishEvent(uint32_t consumeSeq) { | 
|  | 759 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); | 
|  | 760 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 761 | } | 
|  | 762 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 763 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { | 
|  | 764 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); | 
|  | 765 | ASSERT_EQ(OK, status); | 
|  | 766 | } | 
|  | 767 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 768 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, | 
|  | 769 | std::optional<int32_t> expectedDisplayId, | 
|  | 770 | std::optional<int32_t> expectedFlags) { | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 771 | InputEvent* event = consume(); | 
|  | 772 |  | 
|  | 773 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 774 | << ": consumer should have returned non-NULL event."; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 775 | ASSERT_EQ(expectedEventType, event->getType()) | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 776 | << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType) | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 777 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 778 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 779 | if (expectedDisplayId.has_value()) { | 
|  | 780 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); | 
|  | 781 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 782 |  | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 783 | switch (expectedEventType) { | 
|  | 784 | case AINPUT_EVENT_TYPE_KEY: { | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 785 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); | 
|  | 786 | EXPECT_EQ(expectedAction, keyEvent.getAction()); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 787 | if (expectedFlags.has_value()) { | 
|  | 788 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); | 
|  | 789 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 790 | break; | 
|  | 791 | } | 
|  | 792 | case AINPUT_EVENT_TYPE_MOTION: { | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 793 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); | 
|  | 794 | EXPECT_EQ(expectedAction, motionEvent.getAction()); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 795 | if (expectedFlags.has_value()) { | 
|  | 796 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); | 
|  | 797 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 798 | break; | 
|  | 799 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 800 | case AINPUT_EVENT_TYPE_FOCUS: { | 
|  | 801 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; | 
|  | 802 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 803 | case AINPUT_EVENT_TYPE_CAPTURE: { | 
|  | 804 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; | 
|  | 805 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 806 | case AINPUT_EVENT_TYPE_DRAG: { | 
|  | 807 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; | 
|  | 808 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 809 | default: { | 
|  | 810 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; | 
|  | 811 | } | 
|  | 812 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 813 | } | 
|  | 814 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 815 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { | 
|  | 816 | InputEvent* event = consume(); | 
|  | 817 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 818 | << ": consumer should have returned non-NULL event."; | 
|  | 819 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType()) | 
|  | 820 | << "Got " << inputEventTypeToString(event->getType()) | 
|  | 821 | << " event instead of FOCUS event"; | 
|  | 822 |  | 
|  | 823 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 824 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 825 |  | 
|  | 826 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); | 
|  | 827 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); | 
|  | 828 | EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode()); | 
|  | 829 | } | 
|  | 830 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 831 | void consumeCaptureEvent(bool hasCapture) { | 
|  | 832 | const InputEvent* event = consume(); | 
|  | 833 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 834 | << ": consumer should have returned non-NULL event."; | 
|  | 835 | ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType()) | 
|  | 836 | << "Got " << inputEventTypeToString(event->getType()) | 
|  | 837 | << " event instead of CAPTURE event"; | 
|  | 838 |  | 
|  | 839 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 840 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 841 |  | 
|  | 842 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); | 
|  | 843 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); | 
|  | 844 | } | 
|  | 845 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 846 | void consumeDragEvent(bool isExiting, float x, float y) { | 
|  | 847 | const InputEvent* event = consume(); | 
|  | 848 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 849 | << ": consumer should have returned non-NULL event."; | 
|  | 850 | ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType()) | 
|  | 851 | << "Got " << inputEventTypeToString(event->getType()) | 
|  | 852 | << " event instead of DRAG event"; | 
|  | 853 |  | 
|  | 854 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 855 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 856 |  | 
|  | 857 | const auto& dragEvent = static_cast<const DragEvent&>(*event); | 
|  | 858 | EXPECT_EQ(isExiting, dragEvent.isExiting()); | 
|  | 859 | EXPECT_EQ(x, dragEvent.getX()); | 
|  | 860 | EXPECT_EQ(y, dragEvent.getY()); | 
|  | 861 | } | 
|  | 862 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 863 | void assertNoEvents() { | 
|  | 864 | InputEvent* event = consume(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 865 | if (event == nullptr) { | 
|  | 866 | return; | 
|  | 867 | } | 
|  | 868 | if (event->getType() == AINPUT_EVENT_TYPE_KEY) { | 
|  | 869 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); | 
|  | 870 | ADD_FAILURE() << "Received key event " | 
|  | 871 | << KeyEvent::actionToString(keyEvent.getAction()); | 
|  | 872 | } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) { | 
|  | 873 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 874 | ADD_FAILURE() << "Received motion event " | 
|  | 875 | << MotionEvent::actionToString(motionEvent.getAction()); | 
|  | 876 | } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) { | 
|  | 877 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); | 
|  | 878 | ADD_FAILURE() << "Received focus event, hasFocus = " | 
|  | 879 | << (focusEvent.getHasFocus() ? "true" : "false"); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 880 | } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) { | 
|  | 881 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); | 
|  | 882 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " | 
|  | 883 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 884 | } | 
|  | 885 | FAIL() << mName.c_str() | 
|  | 886 | << ": should not have received any events, so consume() should return NULL"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 887 | } | 
|  | 888 |  | 
|  | 889 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } | 
|  | 890 |  | 
|  | 891 | protected: | 
|  | 892 | std::unique_ptr<InputConsumer> mConsumer; | 
|  | 893 | PreallocatedInputEventFactory mEventFactory; | 
|  | 894 |  | 
|  | 895 | std::string mName; | 
|  | 896 | }; | 
|  | 897 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 898 | class FakeWindowHandle : public WindowInfoHandle { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 899 | public: | 
|  | 900 | static const int32_t WIDTH = 600; | 
|  | 901 | static const int32_t HEIGHT = 800; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 902 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 903 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 904 | const sp<InputDispatcher>& dispatcher, const std::string name, | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 905 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 906 | : mName(name) { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 907 | if (token == std::nullopt) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 908 | base::Result<std::unique_ptr<InputChannel>> channel = | 
|  | 909 | dispatcher->createInputChannel(name); | 
|  | 910 | token = (*channel)->getConnectionToken(); | 
|  | 911 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 912 | } | 
|  | 913 |  | 
|  | 914 | inputApplicationHandle->updateInfo(); | 
|  | 915 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); | 
|  | 916 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 917 | mInfo.token = *token; | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 918 | mInfo.id = sId++; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 919 | mInfo.name = name; | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 920 | mInfo.type = WindowInfo::Type::APPLICATION; | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 921 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 922 | mInfo.alpha = 1.0; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 923 | mInfo.frameLeft = 0; | 
|  | 924 | mInfo.frameTop = 0; | 
|  | 925 | mInfo.frameRight = WIDTH; | 
|  | 926 | mInfo.frameBottom = HEIGHT; | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 927 | mInfo.transform.set(0, 0); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 928 | mInfo.globalScaleFactor = 1.0; | 
|  | 929 | mInfo.touchableRegion.clear(); | 
|  | 930 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); | 
|  | 931 | mInfo.visible = true; | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 932 | mInfo.focusable = false; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 933 | mInfo.hasWallpaper = false; | 
|  | 934 | mInfo.paused = false; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 935 | mInfo.ownerPid = INJECTOR_PID; | 
|  | 936 | mInfo.ownerUid = INJECTOR_UID; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 937 | mInfo.displayId = displayId; | 
|  | 938 | } | 
|  | 939 |  | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 940 | void setFocusable(bool focusable) { mInfo.focusable = focusable; } | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 941 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 942 | void setVisible(bool visible) { mInfo.visible = visible; } | 
|  | 943 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 944 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 945 | mInfo.dispatchingTimeout = timeout; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 946 | } | 
|  | 947 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 948 | void setPaused(bool paused) { mInfo.paused = paused; } | 
|  | 949 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 950 | void setAlpha(float alpha) { mInfo.alpha = alpha; } | 
|  | 951 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 952 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 953 |  | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 954 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } | 
|  | 955 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 956 | void setFrame(const Rect& frame) { | 
|  | 957 | mInfo.frameLeft = frame.left; | 
|  | 958 | mInfo.frameTop = frame.top; | 
|  | 959 | mInfo.frameRight = frame.right; | 
|  | 960 | mInfo.frameBottom = frame.bottom; | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 961 | mInfo.transform.set(-frame.left, -frame.top); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 962 | mInfo.touchableRegion.clear(); | 
|  | 963 | mInfo.addTouchableRegion(frame); | 
|  | 964 | } | 
|  | 965 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 966 | void addFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags |= flags; } | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 967 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 968 | void setFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags = flags; } | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 969 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 970 | void setInputFeatures(WindowInfo::Feature features) { mInfo.inputFeatures = features; } | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 971 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 972 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { | 
|  | 973 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); | 
|  | 974 | } | 
|  | 975 |  | 
|  | 976 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 977 |  | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 978 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } | 
|  | 979 |  | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 980 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 981 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, | 
|  | 982 | expectedFlags); | 
|  | 983 | } | 
|  | 984 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 985 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 986 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); | 
|  | 987 | } | 
|  | 988 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 989 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 990 | int32_t expectedFlags = 0) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 991 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId, | 
|  | 992 | expectedFlags); | 
|  | 993 | } | 
|  | 994 |  | 
|  | 995 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 996 | int32_t expectedFlags = 0) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 997 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId, | 
|  | 998 | expectedFlags); | 
|  | 999 | } | 
|  | 1000 |  | 
|  | 1001 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1002 | int32_t expectedFlags = 0) { | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1003 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); | 
|  | 1004 | } | 
|  | 1005 |  | 
|  | 1006 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, | 
|  | 1007 | std::optional<int32_t> expectedFlags = std::nullopt) { | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1008 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, | 
|  | 1009 | expectedFlags); | 
|  | 1010 | } | 
|  | 1011 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1012 | void consumeMotionPointerDown(int32_t pointerIdx, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1013 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
|  | 1014 | int32_t expectedFlags = 0) { | 
|  | 1015 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1016 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1017 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); | 
|  | 1018 | } | 
|  | 1019 |  | 
|  | 1020 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1021 | int32_t expectedFlags = 0) { | 
|  | 1022 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1023 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1024 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); | 
|  | 1025 | } | 
|  | 1026 |  | 
|  | 1027 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1028 | int32_t expectedFlags = 0) { | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1029 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, | 
|  | 1030 | expectedFlags); | 
|  | 1031 | } | 
|  | 1032 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1033 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
|  | 1034 | int32_t expectedFlags = 0) { | 
|  | 1035 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, | 
|  | 1036 | expectedFlags); | 
|  | 1037 | } | 
|  | 1038 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1039 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { | 
|  | 1040 | ASSERT_NE(mInputReceiver, nullptr) | 
|  | 1041 | << "Cannot consume events from a window with no receiver"; | 
|  | 1042 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); | 
|  | 1043 | } | 
|  | 1044 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1045 | void consumeCaptureEvent(bool hasCapture) { | 
|  | 1046 | ASSERT_NE(mInputReceiver, nullptr) | 
|  | 1047 | << "Cannot consume events from a window with no receiver"; | 
|  | 1048 | mInputReceiver->consumeCaptureEvent(hasCapture); | 
|  | 1049 | } | 
|  | 1050 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1051 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, | 
|  | 1052 | std::optional<int32_t> expectedDisplayId, | 
|  | 1053 | std::optional<int32_t> expectedFlags) { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1054 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; | 
|  | 1055 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, | 
|  | 1056 | expectedFlags); | 
|  | 1057 | } | 
|  | 1058 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1059 | void consumeDragEvent(bool isExiting, float x, float y) { | 
|  | 1060 | mInputReceiver->consumeDragEvent(isExiting, x, y); | 
|  | 1061 | } | 
|  | 1062 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1063 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1064 | if (mInputReceiver == nullptr) { | 
|  | 1065 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; | 
|  | 1066 | return std::nullopt; | 
|  | 1067 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1068 | return mInputReceiver->receiveEvent(outEvent); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1069 | } | 
|  | 1070 |  | 
|  | 1071 | void finishEvent(uint32_t sequenceNum) { | 
|  | 1072 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; | 
|  | 1073 | mInputReceiver->finishEvent(sequenceNum); | 
|  | 1074 | } | 
|  | 1075 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1076 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { | 
|  | 1077 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; | 
|  | 1078 | mInputReceiver->sendTimeline(inputEventId, timeline); | 
|  | 1079 | } | 
|  | 1080 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1081 | InputEvent* consume() { | 
|  | 1082 | if (mInputReceiver == nullptr) { | 
|  | 1083 | return nullptr; | 
|  | 1084 | } | 
|  | 1085 | return mInputReceiver->consume(); | 
|  | 1086 | } | 
|  | 1087 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1088 | MotionEvent* consumeMotion() { | 
|  | 1089 | InputEvent* event = consume(); | 
|  | 1090 | if (event == nullptr) { | 
|  | 1091 | ADD_FAILURE() << "Consume failed : no event"; | 
|  | 1092 | return nullptr; | 
|  | 1093 | } | 
|  | 1094 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { | 
|  | 1095 | ADD_FAILURE() << "Instead of motion event, got " | 
|  | 1096 | << inputEventTypeToString(event->getType()); | 
|  | 1097 | return nullptr; | 
|  | 1098 | } | 
|  | 1099 | return static_cast<MotionEvent*>(event); | 
|  | 1100 | } | 
|  | 1101 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1102 | void assertNoEvents() { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1103 | if (mInputReceiver == nullptr && | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1104 | mInfo.inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL)) { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1105 | return; // Can't receive events if the window does not have input channel | 
|  | 1106 | } | 
|  | 1107 | ASSERT_NE(nullptr, mInputReceiver) | 
|  | 1108 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1109 | mInputReceiver->assertNoEvents(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1110 | } | 
|  | 1111 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1112 | sp<IBinder> getToken() { return mInfo.token; } | 
|  | 1113 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1114 | const std::string& getName() { return mName; } | 
|  | 1115 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1116 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { | 
|  | 1117 | mInfo.ownerPid = ownerPid; | 
|  | 1118 | mInfo.ownerUid = ownerUid; | 
|  | 1119 | } | 
|  | 1120 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1121 | private: | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1122 | const std::string mName; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1123 | std::unique_ptr<FakeInputReceiver> mInputReceiver; | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1124 | 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] | 1125 | }; | 
|  | 1126 |  | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1127 | std::atomic<int32_t> FakeWindowHandle::sId{1}; | 
|  | 1128 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1129 | static InputEventInjectionResult injectKey( | 
|  | 1130 | const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, | 
|  | 1131 | int32_t displayId = ADISPLAY_ID_NONE, | 
|  | 1132 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1133 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
|  | 1134 | bool allowKeyRepeat = true) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1135 | KeyEvent event; | 
|  | 1136 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1137 |  | 
|  | 1138 | // Define a valid key down event. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1139 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1140 | INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE, | 
|  | 1141 | repeatCount, currentTime, currentTime); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1142 |  | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1143 | int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; | 
|  | 1144 | if (!allowKeyRepeat) { | 
|  | 1145 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; | 
|  | 1146 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1147 | // Inject event until dispatch out. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1148 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1149 | injectionTimeout, policyFlags); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1150 | } | 
|  | 1151 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1152 | static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher, | 
|  | 1153 | int32_t displayId = ADISPLAY_ID_NONE) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1154 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId); | 
|  | 1155 | } | 
|  | 1156 |  | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1157 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without | 
|  | 1158 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it | 
|  | 1159 | // has to be woken up to process the repeating key. | 
|  | 1160 | static InputEventInjectionResult injectKeyDownNoRepeat(const sp<InputDispatcher>& dispatcher, | 
|  | 1161 | int32_t displayId = ADISPLAY_ID_NONE) { | 
|  | 1162 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId, | 
|  | 1163 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, | 
|  | 1164 | /* allowKeyRepeat */ false); | 
|  | 1165 | } | 
|  | 1166 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1167 | static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher, | 
|  | 1168 | int32_t displayId = ADISPLAY_ID_NONE) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1169 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId); | 
|  | 1170 | } | 
|  | 1171 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1172 | class PointerBuilder { | 
|  | 1173 | public: | 
|  | 1174 | PointerBuilder(int32_t id, int32_t toolType) { | 
|  | 1175 | mProperties.clear(); | 
|  | 1176 | mProperties.id = id; | 
|  | 1177 | mProperties.toolType = toolType; | 
|  | 1178 | mCoords.clear(); | 
|  | 1179 | } | 
|  | 1180 |  | 
|  | 1181 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } | 
|  | 1182 |  | 
|  | 1183 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } | 
|  | 1184 |  | 
|  | 1185 | PointerBuilder& axis(int32_t axis, float value) { | 
|  | 1186 | mCoords.setAxisValue(axis, value); | 
|  | 1187 | return *this; | 
|  | 1188 | } | 
|  | 1189 |  | 
|  | 1190 | PointerProperties buildProperties() const { return mProperties; } | 
|  | 1191 |  | 
|  | 1192 | PointerCoords buildCoords() const { return mCoords; } | 
|  | 1193 |  | 
|  | 1194 | private: | 
|  | 1195 | PointerProperties mProperties; | 
|  | 1196 | PointerCoords mCoords; | 
|  | 1197 | }; | 
|  | 1198 |  | 
|  | 1199 | class MotionEventBuilder { | 
|  | 1200 | public: | 
|  | 1201 | MotionEventBuilder(int32_t action, int32_t source) { | 
|  | 1202 | mAction = action; | 
|  | 1203 | mSource = source; | 
|  | 1204 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1205 | } | 
|  | 1206 |  | 
|  | 1207 | MotionEventBuilder& eventTime(nsecs_t eventTime) { | 
|  | 1208 | mEventTime = eventTime; | 
|  | 1209 | return *this; | 
|  | 1210 | } | 
|  | 1211 |  | 
|  | 1212 | MotionEventBuilder& displayId(int32_t displayId) { | 
|  | 1213 | mDisplayId = displayId; | 
|  | 1214 | return *this; | 
|  | 1215 | } | 
|  | 1216 |  | 
|  | 1217 | MotionEventBuilder& actionButton(int32_t actionButton) { | 
|  | 1218 | mActionButton = actionButton; | 
|  | 1219 | return *this; | 
|  | 1220 | } | 
|  | 1221 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1222 | MotionEventBuilder& buttonState(int32_t buttonState) { | 
|  | 1223 | mButtonState = buttonState; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1224 | return *this; | 
|  | 1225 | } | 
|  | 1226 |  | 
|  | 1227 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { | 
|  | 1228 | mRawXCursorPosition = rawXCursorPosition; | 
|  | 1229 | return *this; | 
|  | 1230 | } | 
|  | 1231 |  | 
|  | 1232 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { | 
|  | 1233 | mRawYCursorPosition = rawYCursorPosition; | 
|  | 1234 | return *this; | 
|  | 1235 | } | 
|  | 1236 |  | 
|  | 1237 | MotionEventBuilder& pointer(PointerBuilder pointer) { | 
|  | 1238 | mPointers.push_back(pointer); | 
|  | 1239 | return *this; | 
|  | 1240 | } | 
|  | 1241 |  | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1242 | MotionEventBuilder& addFlag(uint32_t flags) { | 
|  | 1243 | mFlags |= flags; | 
|  | 1244 | return *this; | 
|  | 1245 | } | 
|  | 1246 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1247 | MotionEvent build() { | 
|  | 1248 | std::vector<PointerProperties> pointerProperties; | 
|  | 1249 | std::vector<PointerCoords> pointerCoords; | 
|  | 1250 | for (const PointerBuilder& pointer : mPointers) { | 
|  | 1251 | pointerProperties.push_back(pointer.buildProperties()); | 
|  | 1252 | pointerCoords.push_back(pointer.buildCoords()); | 
|  | 1253 | } | 
|  | 1254 |  | 
|  | 1255 | // Set mouse cursor position for the most common cases to avoid boilerplate. | 
|  | 1256 | if (mSource == AINPUT_SOURCE_MOUSE && | 
|  | 1257 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && | 
|  | 1258 | mPointers.size() == 1) { | 
|  | 1259 | mRawXCursorPosition = pointerCoords[0].getX(); | 
|  | 1260 | mRawYCursorPosition = pointerCoords[0].getY(); | 
|  | 1261 | } | 
|  | 1262 |  | 
|  | 1263 | MotionEvent event; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1264 | ui::Transform identityTransform; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1265 | event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC, | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1266 | mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1267 | mButtonState, MotionClassification::NONE, identityTransform, | 
|  | 1268 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 1269 | mRawYCursorPosition, mDisplayOrientation, mDisplayWidth, mDisplayHeight, | 
|  | 1270 | mEventTime, mEventTime, mPointers.size(), pointerProperties.data(), | 
|  | 1271 | pointerCoords.data()); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1272 |  | 
|  | 1273 | return event; | 
|  | 1274 | } | 
|  | 1275 |  | 
|  | 1276 | private: | 
|  | 1277 | int32_t mAction; | 
|  | 1278 | int32_t mSource; | 
|  | 1279 | nsecs_t mEventTime; | 
|  | 1280 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; | 
|  | 1281 | int32_t mActionButton{0}; | 
|  | 1282 | int32_t mButtonState{0}; | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1283 | int32_t mFlags{0}; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1284 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; | 
|  | 1285 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 1286 | uint32_t mDisplayOrientation{ui::Transform::ROT_0}; | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1287 | int32_t mDisplayWidth{INVALID_DISPLAY_SIZE}; | 
|  | 1288 | int32_t mDisplayHeight{INVALID_DISPLAY_SIZE}; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1289 |  | 
|  | 1290 | std::vector<PointerBuilder> mPointers; | 
|  | 1291 | }; | 
|  | 1292 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1293 | static InputEventInjectionResult injectMotionEvent( | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1294 | const sp<InputDispatcher>& dispatcher, const MotionEvent& event, | 
|  | 1295 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1296 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) { | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1297 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode, | 
|  | 1298 | injectionTimeout, | 
|  | 1299 | POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); | 
|  | 1300 | } | 
|  | 1301 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1302 | static InputEventInjectionResult injectMotionEvent( | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1303 | const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId, | 
|  | 1304 | const PointF& position, | 
|  | 1305 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1306 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
|  | 1307 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1308 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1309 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) { | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1310 | MotionEvent event = MotionEventBuilder(action, source) | 
|  | 1311 | .displayId(displayId) | 
|  | 1312 | .eventTime(eventTime) | 
|  | 1313 | .rawXCursorPosition(cursorPosition.x) | 
|  | 1314 | .rawYCursorPosition(cursorPosition.y) | 
|  | 1315 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 1316 | .x(position.x) | 
|  | 1317 | .y(position.y)) | 
|  | 1318 | .build(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1319 |  | 
|  | 1320 | // Inject event until dispatch out. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1321 | return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1322 | } | 
|  | 1323 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1324 | static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher, | 
|  | 1325 | int32_t source, int32_t displayId, | 
|  | 1326 | const PointF& location = {100, 200}) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1327 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1328 | } | 
|  | 1329 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1330 | static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher, | 
|  | 1331 | int32_t source, int32_t displayId, | 
|  | 1332 | const PointF& location = {100, 200}) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1333 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1334 | } | 
|  | 1335 |  | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1336 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { | 
|  | 1337 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1338 | // Define a valid key event. | 
| Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1339 | NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, | 
|  | 1340 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, | 
|  | 1341 | KEY_A, AMETA_NONE, currentTime); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1342 |  | 
|  | 1343 | return args; | 
|  | 1344 | } | 
|  | 1345 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1346 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, | 
|  | 1347 | const std::vector<PointF>& points) { | 
|  | 1348 | size_t pointerCount = points.size(); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1349 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { | 
|  | 1350 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; | 
|  | 1351 | } | 
|  | 1352 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1353 | PointerProperties pointerProperties[pointerCount]; | 
|  | 1354 | PointerCoords pointerCoords[pointerCount]; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1355 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1356 | for (size_t i = 0; i < pointerCount; i++) { | 
|  | 1357 | pointerProperties[i].clear(); | 
|  | 1358 | pointerProperties[i].id = i; | 
|  | 1359 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1360 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1361 | pointerCoords[i].clear(); | 
|  | 1362 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); | 
|  | 1363 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); | 
|  | 1364 | } | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1365 |  | 
|  | 1366 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1367 | // Define a valid motion event. | 
| Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1368 | NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1369 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, | 
|  | 1370 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1371 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, | 
|  | 1372 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1373 | AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 1374 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1375 |  | 
|  | 1376 | return args; | 
|  | 1377 | } | 
|  | 1378 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1379 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { | 
|  | 1380 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); | 
|  | 1381 | } | 
|  | 1382 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1383 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(bool enabled) { | 
|  | 1384 | return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), enabled); | 
|  | 1385 | } | 
|  | 1386 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1387 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1388 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1389 | sp<FakeWindowHandle> window = | 
|  | 1390 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1391 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1392 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1393 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1394 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 1395 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1396 |  | 
|  | 1397 | // Window should receive motion event. | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1398 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1399 | } | 
|  | 1400 |  | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1401 | /** | 
|  | 1402 | * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues. | 
|  | 1403 | * To ensure that window receives only events that were directly inside of it, add | 
|  | 1404 | * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input | 
|  | 1405 | * when finding touched windows. | 
|  | 1406 | * This test serves as a sanity check for the next test, where setInputWindows is | 
|  | 1407 | * called twice. | 
|  | 1408 | */ | 
|  | 1409 | TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1410 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1411 | sp<FakeWindowHandle> window = | 
|  | 1412 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 1413 | window->setFrame(Rect(0, 0, 100, 100)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1414 | window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1415 |  | 
|  | 1416 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1417 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1418 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1419 | {50, 50})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1420 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1421 |  | 
|  | 1422 | // Window should receive motion event. | 
|  | 1423 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1424 | } | 
|  | 1425 |  | 
|  | 1426 | /** | 
|  | 1427 | * Calling setInputWindows twice, with the same info, should not cause any issues. | 
|  | 1428 | * To ensure that window receives only events that were directly inside of it, add | 
|  | 1429 | * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input | 
|  | 1430 | * when finding touched windows. | 
|  | 1431 | */ | 
|  | 1432 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1433 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1434 | sp<FakeWindowHandle> window = | 
|  | 1435 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 1436 | window->setFrame(Rect(0, 0, 100, 100)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1437 | window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1438 |  | 
|  | 1439 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 1440 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1441 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1442 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1443 | {50, 50})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1444 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1445 |  | 
|  | 1446 | // Window should receive motion event. | 
|  | 1447 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1448 | } | 
|  | 1449 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1450 | // The foreground window should receive the first touch down event. | 
|  | 1451 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1452 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1453 | sp<FakeWindowHandle> windowTop = | 
|  | 1454 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 1455 | sp<FakeWindowHandle> windowSecond = | 
|  | 1456 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1457 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1458 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1459 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1460 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 1461 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1462 |  | 
|  | 1463 | // 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] | 1464 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1465 | windowSecond->assertNoEvents(); | 
|  | 1466 | } | 
|  | 1467 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1468 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1469 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1470 | sp<FakeWindowHandle> windowLeft = | 
|  | 1471 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
|  | 1472 | windowLeft->setFrame(Rect(0, 0, 600, 800)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1473 | windowLeft->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1474 | sp<FakeWindowHandle> windowRight = | 
|  | 1475 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
|  | 1476 | windowRight->setFrame(Rect(600, 0, 1200, 800)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1477 | windowRight->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1478 |  | 
|  | 1479 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 1480 |  | 
|  | 1481 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); | 
|  | 1482 |  | 
|  | 1483 | // 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] | 1484 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1485 | injectMotionEvent(mDispatcher, | 
|  | 1486 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1487 | AINPUT_SOURCE_MOUSE) | 
|  | 1488 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1489 | .x(900) | 
|  | 1490 | .y(400)) | 
|  | 1491 | .build())); | 
|  | 1492 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1493 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1494 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1495 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1496 |  | 
|  | 1497 | // Move cursor into left window | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1498 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1499 | injectMotionEvent(mDispatcher, | 
|  | 1500 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1501 | AINPUT_SOURCE_MOUSE) | 
|  | 1502 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1503 | .x(300) | 
|  | 1504 | .y(400)) | 
|  | 1505 | .build())); | 
|  | 1506 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 1507 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1508 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1509 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1510 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1511 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1512 |  | 
|  | 1513 | // Inject a series of mouse events for a mouse click | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1514 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1515 | injectMotionEvent(mDispatcher, | 
|  | 1516 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 1517 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1518 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1519 | .x(300) | 
|  | 1520 | .y(400)) | 
|  | 1521 | .build())); | 
|  | 1522 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1523 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1524 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1525 | injectMotionEvent(mDispatcher, | 
|  | 1526 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 1527 | AINPUT_SOURCE_MOUSE) | 
|  | 1528 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1529 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1530 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1531 | .x(300) | 
|  | 1532 | .y(400)) | 
|  | 1533 | .build())); | 
|  | 1534 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 1535 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1536 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1537 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1538 | injectMotionEvent(mDispatcher, | 
|  | 1539 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 1540 | AINPUT_SOURCE_MOUSE) | 
|  | 1541 | .buttonState(0) | 
|  | 1542 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1543 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1544 | .x(300) | 
|  | 1545 | .y(400)) | 
|  | 1546 | .build())); | 
|  | 1547 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 1548 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1549 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1550 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1551 | injectMotionEvent(mDispatcher, | 
|  | 1552 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) | 
|  | 1553 | .buttonState(0) | 
|  | 1554 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1555 | .x(300) | 
|  | 1556 | .y(400)) | 
|  | 1557 | .build())); | 
|  | 1558 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 1559 |  | 
|  | 1560 | // Move mouse cursor back to right window | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1561 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1562 | injectMotionEvent(mDispatcher, | 
|  | 1563 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1564 | AINPUT_SOURCE_MOUSE) | 
|  | 1565 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1566 | .x(900) | 
|  | 1567 | .y(400)) | 
|  | 1568 | .build())); | 
|  | 1569 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 1570 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1571 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1572 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1573 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1574 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1575 | } | 
|  | 1576 |  | 
|  | 1577 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected | 
|  | 1578 | // directly in this test. | 
|  | 1579 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1580 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1581 | sp<FakeWindowHandle> window = | 
|  | 1582 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 1583 | window->setFrame(Rect(0, 0, 1200, 800)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1584 | window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1585 |  | 
|  | 1586 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 1587 |  | 
|  | 1588 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 1589 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1590 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1591 | injectMotionEvent(mDispatcher, | 
|  | 1592 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1593 | AINPUT_SOURCE_MOUSE) | 
|  | 1594 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1595 | .x(300) | 
|  | 1596 | .y(400)) | 
|  | 1597 | .build())); | 
|  | 1598 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1599 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1600 |  | 
|  | 1601 | // Inject a series of mouse events for a mouse click | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1602 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1603 | injectMotionEvent(mDispatcher, | 
|  | 1604 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 1605 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1606 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1607 | .x(300) | 
|  | 1608 | .y(400)) | 
|  | 1609 | .build())); | 
|  | 1610 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1611 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1612 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1613 | injectMotionEvent(mDispatcher, | 
|  | 1614 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 1615 | AINPUT_SOURCE_MOUSE) | 
|  | 1616 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1617 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1618 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1619 | .x(300) | 
|  | 1620 | .y(400)) | 
|  | 1621 | .build())); | 
|  | 1622 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 1623 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1624 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1625 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1626 | injectMotionEvent(mDispatcher, | 
|  | 1627 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 1628 | AINPUT_SOURCE_MOUSE) | 
|  | 1629 | .buttonState(0) | 
|  | 1630 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1631 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1632 | .x(300) | 
|  | 1633 | .y(400)) | 
|  | 1634 | .build())); | 
|  | 1635 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 1636 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1637 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1638 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1639 | injectMotionEvent(mDispatcher, | 
|  | 1640 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) | 
|  | 1641 | .buttonState(0) | 
|  | 1642 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1643 | .x(300) | 
|  | 1644 | .y(400)) | 
|  | 1645 | .build())); | 
|  | 1646 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 1647 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1648 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1649 | injectMotionEvent(mDispatcher, | 
|  | 1650 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 1651 | AINPUT_SOURCE_MOUSE) | 
|  | 1652 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1653 | .x(300) | 
|  | 1654 | .y(400)) | 
|  | 1655 | .build())); | 
|  | 1656 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 1657 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1658 | } | 
|  | 1659 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1660 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1661 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1662 |  | 
|  | 1663 | sp<FakeWindowHandle> windowLeft = | 
|  | 1664 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
|  | 1665 | windowLeft->setFrame(Rect(0, 0, 600, 800)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1666 | windowLeft->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1667 | sp<FakeWindowHandle> windowRight = | 
|  | 1668 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
|  | 1669 | windowRight->setFrame(Rect(600, 0, 1200, 800)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1670 | windowRight->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1671 |  | 
|  | 1672 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 1673 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1674 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1675 |  | 
|  | 1676 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of | 
|  | 1677 | // left window. This event should be dispatched to the left window. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1678 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1679 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1680 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1681 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1682 | windowRight->assertNoEvents(); | 
|  | 1683 | } | 
|  | 1684 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1685 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1686 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1687 | sp<FakeWindowHandle> window = | 
|  | 1688 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1689 | window->setFocusable(true); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1690 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1691 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1692 | setFocusedWindow(window); | 
|  | 1693 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1694 | window->consumeFocusEvent(true); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1695 |  | 
|  | 1696 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 1697 | mDispatcher->notifyKey(&keyArgs); | 
|  | 1698 |  | 
|  | 1699 | // Window should receive key down event. | 
|  | 1700 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 1701 |  | 
|  | 1702 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED | 
|  | 1703 | // on the app side. | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1704 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1705 | mDispatcher->notifyDeviceReset(&args); | 
|  | 1706 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, | 
|  | 1707 | AKEY_EVENT_FLAG_CANCELED); | 
|  | 1708 | } | 
|  | 1709 |  | 
|  | 1710 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1711 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1712 | sp<FakeWindowHandle> window = | 
|  | 1713 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 1714 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1715 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1716 |  | 
|  | 1717 | NotifyMotionArgs motionArgs = | 
|  | 1718 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1719 | ADISPLAY_ID_DEFAULT); | 
|  | 1720 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 1721 |  | 
|  | 1722 | // Window should receive motion down event. | 
|  | 1723 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1724 |  | 
|  | 1725 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL | 
|  | 1726 | // on the app side. | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1727 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1728 | mDispatcher->notifyDeviceReset(&args); | 
|  | 1729 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, | 
|  | 1730 | 0 /*expectedFlags*/); | 
|  | 1731 | } | 
|  | 1732 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1733 | using TransferFunction = | 
|  | 1734 | std::function<bool(sp<InputDispatcher> dispatcher, sp<IBinder>, sp<IBinder>)>; | 
|  | 1735 |  | 
|  | 1736 | class TransferTouchFixture : public InputDispatcherTest, | 
|  | 1737 | public ::testing::WithParamInterface<TransferFunction> {}; | 
|  | 1738 |  | 
|  | 1739 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1740 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1741 |  | 
|  | 1742 | // Create a couple of windows | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1743 | sp<FakeWindowHandle> firstWindow = | 
|  | 1744 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
|  | 1745 | sp<FakeWindowHandle> secondWindow = | 
|  | 1746 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1747 |  | 
|  | 1748 | // Add the windows to the dispatcher | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1749 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1750 |  | 
|  | 1751 | // Send down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1752 | NotifyMotionArgs downMotionArgs = | 
|  | 1753 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1754 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1755 | mDispatcher->notifyMotion(&downMotionArgs); | 
|  | 1756 | // Only the first window should get the down event | 
|  | 1757 | firstWindow->consumeMotionDown(); | 
|  | 1758 | secondWindow->assertNoEvents(); | 
|  | 1759 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1760 | // Transfer touch to the second window | 
|  | 1761 | TransferFunction f = GetParam(); | 
|  | 1762 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); | 
|  | 1763 | ASSERT_TRUE(success); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1764 | // The first window gets cancel and the second gets down | 
|  | 1765 | firstWindow->consumeMotionCancel(); | 
|  | 1766 | secondWindow->consumeMotionDown(); | 
|  | 1767 |  | 
|  | 1768 | // Send up event to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1769 | NotifyMotionArgs upMotionArgs = | 
|  | 1770 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1771 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1772 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 1773 | // The first  window gets no events and the second gets up | 
|  | 1774 | firstWindow->assertNoEvents(); | 
|  | 1775 | secondWindow->consumeMotionUp(); | 
|  | 1776 | } | 
|  | 1777 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1778 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1779 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1780 |  | 
|  | 1781 | PointF touchPoint = {10, 10}; | 
|  | 1782 |  | 
|  | 1783 | // Create a couple of windows | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1784 | sp<FakeWindowHandle> firstWindow = | 
|  | 1785 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
|  | 1786 | sp<FakeWindowHandle> secondWindow = | 
|  | 1787 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1788 |  | 
|  | 1789 | // Add the windows to the dispatcher | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1790 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1791 |  | 
|  | 1792 | // Send down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1793 | NotifyMotionArgs downMotionArgs = | 
|  | 1794 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1795 | ADISPLAY_ID_DEFAULT, {touchPoint}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1796 | mDispatcher->notifyMotion(&downMotionArgs); | 
|  | 1797 | // Only the first window should get the down event | 
|  | 1798 | firstWindow->consumeMotionDown(); | 
|  | 1799 | secondWindow->assertNoEvents(); | 
|  | 1800 |  | 
|  | 1801 | // Send pointer down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1802 | NotifyMotionArgs pointerDownMotionArgs = | 
|  | 1803 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1804 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1805 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1806 | {touchPoint, touchPoint}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1807 | mDispatcher->notifyMotion(&pointerDownMotionArgs); | 
|  | 1808 | // Only the first window should get the pointer down event | 
|  | 1809 | firstWindow->consumeMotionPointerDown(1); | 
|  | 1810 | secondWindow->assertNoEvents(); | 
|  | 1811 |  | 
|  | 1812 | // Transfer touch focus to the second window | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1813 | TransferFunction f = GetParam(); | 
|  | 1814 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); | 
|  | 1815 | ASSERT_TRUE(success); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1816 | // The first window gets cancel and the second gets down and pointer down | 
|  | 1817 | firstWindow->consumeMotionCancel(); | 
|  | 1818 | secondWindow->consumeMotionDown(); | 
|  | 1819 | secondWindow->consumeMotionPointerDown(1); | 
|  | 1820 |  | 
|  | 1821 | // Send pointer up to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1822 | NotifyMotionArgs pointerUpMotionArgs = | 
|  | 1823 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1824 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1825 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1826 | {touchPoint, touchPoint}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1827 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 1828 | // The first window gets nothing and the second gets pointer up | 
|  | 1829 | firstWindow->assertNoEvents(); | 
|  | 1830 | secondWindow->consumeMotionPointerUp(1); | 
|  | 1831 |  | 
|  | 1832 | // Send up event to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1833 | NotifyMotionArgs upMotionArgs = | 
|  | 1834 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1835 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1836 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 1837 | // The first window gets nothing and the second gets up | 
|  | 1838 | firstWindow->assertNoEvents(); | 
|  | 1839 | secondWindow->consumeMotionUp(); | 
|  | 1840 | } | 
|  | 1841 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1842 | // For the cases of single pointer touch and two pointers non-split touch, the api's | 
|  | 1843 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ | 
|  | 1844 | // for the case where there are multiple pointers split across several windows. | 
|  | 1845 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, | 
|  | 1846 | ::testing::Values( | 
|  | 1847 | [&](sp<InputDispatcher> dispatcher, sp<IBinder> /*ignored*/, | 
|  | 1848 | sp<IBinder> destChannelToken) { | 
|  | 1849 | return dispatcher->transferTouch(destChannelToken); | 
|  | 1850 | }, | 
|  | 1851 | [&](sp<InputDispatcher> dispatcher, sp<IBinder> from, | 
|  | 1852 | sp<IBinder> to) { | 
|  | 1853 | return dispatcher->transferTouchFocus(from, to, | 
|  | 1854 | false /*isDragAndDrop*/); | 
|  | 1855 | })); | 
|  | 1856 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1857 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1858 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1859 |  | 
|  | 1860 | // Create a non touch modal window that supports split touch | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1861 | sp<FakeWindowHandle> firstWindow = | 
|  | 1862 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1863 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1864 | firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1865 |  | 
|  | 1866 | // Create a non touch modal window that supports split touch | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1867 | sp<FakeWindowHandle> secondWindow = | 
|  | 1868 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1869 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1870 | secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1871 |  | 
|  | 1872 | // Add the windows to the dispatcher | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1873 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1874 |  | 
|  | 1875 | PointF pointInFirst = {300, 200}; | 
|  | 1876 | PointF pointInSecond = {300, 600}; | 
|  | 1877 |  | 
|  | 1878 | // Send down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1879 | NotifyMotionArgs firstDownMotionArgs = | 
|  | 1880 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1881 | ADISPLAY_ID_DEFAULT, {pointInFirst}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1882 | mDispatcher->notifyMotion(&firstDownMotionArgs); | 
|  | 1883 | // Only the first window should get the down event | 
|  | 1884 | firstWindow->consumeMotionDown(); | 
|  | 1885 | secondWindow->assertNoEvents(); | 
|  | 1886 |  | 
|  | 1887 | // Send down to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1888 | NotifyMotionArgs secondDownMotionArgs = | 
|  | 1889 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1890 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1891 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1892 | {pointInFirst, pointInSecond}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1893 | mDispatcher->notifyMotion(&secondDownMotionArgs); | 
|  | 1894 | // The first window gets a move and the second a down | 
|  | 1895 | firstWindow->consumeMotionMove(); | 
|  | 1896 | secondWindow->consumeMotionDown(); | 
|  | 1897 |  | 
|  | 1898 | // Transfer touch focus to the second window | 
|  | 1899 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); | 
|  | 1900 | // The first window gets cancel and the new gets pointer down (it already saw down) | 
|  | 1901 | firstWindow->consumeMotionCancel(); | 
|  | 1902 | secondWindow->consumeMotionPointerDown(1); | 
|  | 1903 |  | 
|  | 1904 | // Send pointer up to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1905 | NotifyMotionArgs pointerUpMotionArgs = | 
|  | 1906 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1907 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1908 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1909 | {pointInFirst, pointInSecond}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1910 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 1911 | // The first window gets nothing and the second gets pointer up | 
|  | 1912 | firstWindow->assertNoEvents(); | 
|  | 1913 | secondWindow->consumeMotionPointerUp(1); | 
|  | 1914 |  | 
|  | 1915 | // Send up event to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1916 | NotifyMotionArgs upMotionArgs = | 
|  | 1917 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1918 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1919 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 1920 | // The first window gets nothing and the second gets up | 
|  | 1921 | firstWindow->assertNoEvents(); | 
|  | 1922 | secondWindow->consumeMotionUp(); | 
|  | 1923 | } | 
|  | 1924 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1925 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. | 
|  | 1926 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving | 
|  | 1927 | // touch is not supported, so the touch should continue on those windows and the transferred-to | 
|  | 1928 | // window should get nothing. | 
|  | 1929 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { | 
|  | 1930 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 1931 |  | 
|  | 1932 | // Create a non touch modal window that supports split touch | 
|  | 1933 | sp<FakeWindowHandle> firstWindow = | 
|  | 1934 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
|  | 1935 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1936 | firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1937 |  | 
|  | 1938 | // Create a non touch modal window that supports split touch | 
|  | 1939 | sp<FakeWindowHandle> secondWindow = | 
|  | 1940 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
|  | 1941 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1942 | secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1943 |  | 
|  | 1944 | // Add the windows to the dispatcher | 
|  | 1945 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
|  | 1946 |  | 
|  | 1947 | PointF pointInFirst = {300, 200}; | 
|  | 1948 | PointF pointInSecond = {300, 600}; | 
|  | 1949 |  | 
|  | 1950 | // Send down to the first window | 
|  | 1951 | NotifyMotionArgs firstDownMotionArgs = | 
|  | 1952 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1953 | ADISPLAY_ID_DEFAULT, {pointInFirst}); | 
|  | 1954 | mDispatcher->notifyMotion(&firstDownMotionArgs); | 
|  | 1955 | // Only the first window should get the down event | 
|  | 1956 | firstWindow->consumeMotionDown(); | 
|  | 1957 | secondWindow->assertNoEvents(); | 
|  | 1958 |  | 
|  | 1959 | // Send down to the second window | 
|  | 1960 | NotifyMotionArgs secondDownMotionArgs = | 
|  | 1961 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1962 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1963 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1964 | {pointInFirst, pointInSecond}); | 
|  | 1965 | mDispatcher->notifyMotion(&secondDownMotionArgs); | 
|  | 1966 | // The first window gets a move and the second a down | 
|  | 1967 | firstWindow->consumeMotionMove(); | 
|  | 1968 | secondWindow->consumeMotionDown(); | 
|  | 1969 |  | 
|  | 1970 | // Transfer touch focus to the second window | 
|  | 1971 | const bool transferred = mDispatcher->transferTouch(secondWindow->getToken()); | 
|  | 1972 | // The 'transferTouch' call should not succeed, because there are 2 touched windows | 
|  | 1973 | ASSERT_FALSE(transferred); | 
|  | 1974 | firstWindow->assertNoEvents(); | 
|  | 1975 | secondWindow->assertNoEvents(); | 
|  | 1976 |  | 
|  | 1977 | // The rest of the dispatch should proceed as normal | 
|  | 1978 | // Send pointer up to the second window | 
|  | 1979 | NotifyMotionArgs pointerUpMotionArgs = | 
|  | 1980 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1981 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1982 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1983 | {pointInFirst, pointInSecond}); | 
|  | 1984 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 1985 | // The first window gets MOVE and the second gets pointer up | 
|  | 1986 | firstWindow->consumeMotionMove(); | 
|  | 1987 | secondWindow->consumeMotionUp(); | 
|  | 1988 |  | 
|  | 1989 | // Send up event to the first window | 
|  | 1990 | NotifyMotionArgs upMotionArgs = | 
|  | 1991 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1992 | ADISPLAY_ID_DEFAULT); | 
|  | 1993 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 1994 | // The first window gets nothing and the second gets up | 
|  | 1995 | firstWindow->consumeMotionUp(); | 
|  | 1996 | secondWindow->assertNoEvents(); | 
|  | 1997 | } | 
|  | 1998 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1999 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2000 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2001 | sp<FakeWindowHandle> window = | 
|  | 2002 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2003 |  | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2004 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2005 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2006 | setFocusedWindow(window); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2007 |  | 
|  | 2008 | window->consumeFocusEvent(true); | 
|  | 2009 |  | 
|  | 2010 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 2011 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2012 |  | 
|  | 2013 | // Window should receive key down event. | 
|  | 2014 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 2015 | } | 
|  | 2016 |  | 
|  | 2017 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2018 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2019 | sp<FakeWindowHandle> window = | 
|  | 2020 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2021 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2022 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2023 |  | 
|  | 2024 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 2025 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2026 | mDispatcher->waitForIdle(); | 
|  | 2027 |  | 
|  | 2028 | window->assertNoEvents(); | 
|  | 2029 | } | 
|  | 2030 |  | 
|  | 2031 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys | 
|  | 2032 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2033 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2034 | sp<FakeWindowHandle> window = | 
|  | 2035 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2036 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2037 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2038 |  | 
|  | 2039 | // Send key | 
|  | 2040 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 2041 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2042 | // Send motion | 
|  | 2043 | NotifyMotionArgs motionArgs = | 
|  | 2044 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2045 | ADISPLAY_ID_DEFAULT); | 
|  | 2046 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2047 |  | 
|  | 2048 | // Window should receive only the motion event | 
|  | 2049 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2050 | window->assertNoEvents(); // Key event or focus event will not be received | 
|  | 2051 | } | 
|  | 2052 |  | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2053 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { | 
|  | 2054 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2055 |  | 
|  | 2056 | // Create first non touch modal window that supports split touch | 
|  | 2057 | sp<FakeWindowHandle> firstWindow = | 
|  | 2058 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
|  | 2059 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2060 | firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2061 |  | 
|  | 2062 | // Create second non touch modal window that supports split touch | 
|  | 2063 | sp<FakeWindowHandle> secondWindow = | 
|  | 2064 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
|  | 2065 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2066 | secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2067 |  | 
|  | 2068 | // Add the windows to the dispatcher | 
|  | 2069 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
|  | 2070 |  | 
|  | 2071 | PointF pointInFirst = {300, 200}; | 
|  | 2072 | PointF pointInSecond = {300, 600}; | 
|  | 2073 |  | 
|  | 2074 | // Send down to the first window | 
|  | 2075 | NotifyMotionArgs firstDownMotionArgs = | 
|  | 2076 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2077 | ADISPLAY_ID_DEFAULT, {pointInFirst}); | 
|  | 2078 | mDispatcher->notifyMotion(&firstDownMotionArgs); | 
|  | 2079 | // Only the first window should get the down event | 
|  | 2080 | firstWindow->consumeMotionDown(); | 
|  | 2081 | secondWindow->assertNoEvents(); | 
|  | 2082 |  | 
|  | 2083 | // Send down to the second window | 
|  | 2084 | NotifyMotionArgs secondDownMotionArgs = | 
|  | 2085 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 2086 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 2087 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 2088 | {pointInFirst, pointInSecond}); | 
|  | 2089 | mDispatcher->notifyMotion(&secondDownMotionArgs); | 
|  | 2090 | // The first window gets a move and the second a down | 
|  | 2091 | firstWindow->consumeMotionMove(); | 
|  | 2092 | secondWindow->consumeMotionDown(); | 
|  | 2093 |  | 
|  | 2094 | // Send pointer cancel to the second window | 
|  | 2095 | NotifyMotionArgs pointerUpMotionArgs = | 
|  | 2096 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 2097 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 2098 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 2099 | {pointInFirst, pointInSecond}); | 
|  | 2100 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; | 
|  | 2101 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 2102 | // The first window gets move and the second gets cancel. | 
|  | 2103 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); | 
|  | 2104 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); | 
|  | 2105 |  | 
|  | 2106 | // Send up event. | 
|  | 2107 | NotifyMotionArgs upMotionArgs = | 
|  | 2108 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2109 | ADISPLAY_ID_DEFAULT); | 
|  | 2110 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 2111 | // The first window gets up and the second gets nothing. | 
|  | 2112 | firstWindow->consumeMotionUp(); | 
|  | 2113 | secondWindow->assertNoEvents(); | 
|  | 2114 | } | 
|  | 2115 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 2116 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { | 
|  | 2117 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2118 |  | 
|  | 2119 | sp<FakeWindowHandle> window = | 
|  | 2120 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 2121 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2122 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; | 
|  | 2123 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; | 
|  | 2124 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; | 
|  | 2125 |  | 
|  | 2126 | window->sendTimeline(1 /*inputEventId*/, graphicsTimeline); | 
|  | 2127 | window->assertNoEvents(); | 
|  | 2128 | mDispatcher->waitForIdle(); | 
|  | 2129 | } | 
|  | 2130 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2131 | class FakeMonitorReceiver { | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2132 | public: | 
|  | 2133 | FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name, | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2134 | int32_t displayId, bool isGestureMonitor = false) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 2135 | base::Result<std::unique_ptr<InputChannel>> channel = | 
| Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 2136 | dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 2137 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2138 | } | 
|  | 2139 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2140 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } | 
|  | 2141 |  | 
|  | 2142 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 2143 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, | 
|  | 2144 | expectedDisplayId, expectedFlags); | 
|  | 2145 | } | 
|  | 2146 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2147 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } | 
|  | 2148 |  | 
|  | 2149 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } | 
|  | 2150 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2151 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 2152 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, | 
|  | 2153 | expectedDisplayId, expectedFlags); | 
|  | 2154 | } | 
|  | 2155 |  | 
|  | 2156 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 2157 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, | 
|  | 2158 | expectedDisplayId, expectedFlags); | 
|  | 2159 | } | 
|  | 2160 |  | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2161 | MotionEvent* consumeMotion() { | 
|  | 2162 | InputEvent* event = mInputReceiver->consume(); | 
|  | 2163 | if (!event) { | 
|  | 2164 | ADD_FAILURE() << "No event was produced"; | 
|  | 2165 | return nullptr; | 
|  | 2166 | } | 
|  | 2167 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { | 
|  | 2168 | ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion"; | 
|  | 2169 | return nullptr; | 
|  | 2170 | } | 
|  | 2171 | return static_cast<MotionEvent*>(event); | 
|  | 2172 | } | 
|  | 2173 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2174 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } | 
|  | 2175 |  | 
|  | 2176 | private: | 
|  | 2177 | std::unique_ptr<FakeInputReceiver> mInputReceiver; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2178 | }; | 
|  | 2179 |  | 
|  | 2180 | // Tests for gesture monitors | 
|  | 2181 | TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2182 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2183 | sp<FakeWindowHandle> window = | 
|  | 2184 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2185 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2186 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2187 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2188 | true /*isGestureMonitor*/); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2189 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2190 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2191 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2192 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2193 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2194 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2195 | } | 
|  | 2196 |  | 
|  | 2197 | TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2198 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2199 | sp<FakeWindowHandle> window = | 
|  | 2200 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2201 |  | 
|  | 2202 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2203 | window->setFocusable(true); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2204 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2205 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2206 | setFocusedWindow(window); | 
|  | 2207 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2208 | window->consumeFocusEvent(true); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2209 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2210 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2211 | true /*isGestureMonitor*/); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2212 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2213 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
|  | 2214 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2215 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2216 | monitor.assertNoEvents(); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2217 | } | 
|  | 2218 |  | 
|  | 2219 | TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2220 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2221 | sp<FakeWindowHandle> window = | 
|  | 2222 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2223 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2224 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2225 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2226 | true /*isGestureMonitor*/); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2227 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2228 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2229 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2230 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2231 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2232 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2233 |  | 
|  | 2234 | window->releaseChannel(); | 
|  | 2235 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2236 | mDispatcher->pilferPointers(monitor.getToken()); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2237 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2238 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2239 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2240 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2241 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2242 | } | 
|  | 2243 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2244 | TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) { | 
|  | 2245 | FakeMonitorReceiver monitor = | 
|  | 2246 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, | 
|  | 2247 | true /*isGestureMonitor*/); | 
|  | 2248 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2249 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2250 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); | 
|  | 2251 | std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); | 
|  | 2252 | ASSERT_TRUE(consumeSeq); | 
|  | 2253 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 2254 | mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2255 | monitor.finishEvent(*consumeSeq); | 
|  | 2256 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 2257 | mFakePolicy->assertNotifyMonitorResponsiveWasCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2258 | } | 
|  | 2259 |  | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2260 | // Tests for gesture monitors | 
|  | 2261 | TEST_F(InputDispatcherTest, GestureMonitor_NoWindowTransform) { | 
|  | 2262 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2263 | sp<FakeWindowHandle> window = | 
|  | 2264 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2265 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2266 | window->setWindowOffset(20, 40); | 
|  | 2267 | window->setWindowTransform(0, 1, -1, 0); | 
|  | 2268 |  | 
|  | 2269 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2270 | true /*isGestureMonitor*/); | 
|  | 2271 |  | 
|  | 2272 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2273 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 2274 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 2275 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2276 | MotionEvent* event = monitor.consumeMotion(); | 
|  | 2277 | // Even though window has transform, gesture monitor must not. | 
|  | 2278 | ASSERT_EQ(ui::Transform(), event->getTransform()); | 
|  | 2279 | } | 
|  | 2280 |  | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2281 | TEST_F(InputDispatcherTest, TestMoveEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2282 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2283 | sp<FakeWindowHandle> window = | 
|  | 2284 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2285 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2286 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2287 |  | 
|  | 2288 | NotifyMotionArgs motionArgs = | 
|  | 2289 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2290 | ADISPLAY_ID_DEFAULT); | 
|  | 2291 |  | 
|  | 2292 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2293 | // Window should receive motion down event. | 
|  | 2294 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2295 |  | 
|  | 2296 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2297 | motionArgs.id += 1; | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2298 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 2299 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, | 
|  | 2300 | motionArgs.pointerCoords[0].getX() - 10); | 
|  | 2301 |  | 
|  | 2302 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2303 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, | 
|  | 2304 | 0 /*expectedFlags*/); | 
|  | 2305 | } | 
|  | 2306 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2307 | /** | 
|  | 2308 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to | 
|  | 2309 | * the device default right away. In the test scenario, we check both the default value, | 
|  | 2310 | * and the action of enabling / disabling. | 
|  | 2311 | */ | 
|  | 2312 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2313 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2314 | sp<FakeWindowHandle> window = | 
|  | 2315 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 2316 |  | 
|  | 2317 | // Set focused application. | 
|  | 2318 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2319 | window->setFocusable(true); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2320 |  | 
|  | 2321 | SCOPED_TRACE("Check default value of touch mode"); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2322 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2323 | setFocusedWindow(window); | 
|  | 2324 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2325 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 2326 |  | 
|  | 2327 | SCOPED_TRACE("Remove the window to trigger focus loss"); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2328 | window->setFocusable(false); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2329 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2330 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); | 
|  | 2331 |  | 
|  | 2332 | SCOPED_TRACE("Disable touch mode"); | 
|  | 2333 | mDispatcher->setInTouchMode(false); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2334 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2335 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2336 | setFocusedWindow(window); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2337 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); | 
|  | 2338 |  | 
|  | 2339 | SCOPED_TRACE("Remove the window to trigger focus loss"); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2340 | window->setFocusable(false); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2341 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2342 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); | 
|  | 2343 |  | 
|  | 2344 | SCOPED_TRACE("Enable touch mode again"); | 
|  | 2345 | mDispatcher->setInTouchMode(true); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2346 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2347 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2348 | setFocusedWindow(window); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2349 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 2350 |  | 
|  | 2351 | window->assertNoEvents(); | 
|  | 2352 | } | 
|  | 2353 |  | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2354 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2355 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2356 | sp<FakeWindowHandle> window = | 
|  | 2357 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 2358 |  | 
|  | 2359 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2360 | window->setFocusable(true); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2361 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2362 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2363 | setFocusedWindow(window); | 
|  | 2364 |  | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2365 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 2366 |  | 
|  | 2367 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); | 
|  | 2368 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2369 |  | 
|  | 2370 | InputEvent* event = window->consume(); | 
|  | 2371 | ASSERT_NE(event, nullptr); | 
|  | 2372 |  | 
|  | 2373 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); | 
|  | 2374 | ASSERT_NE(verified, nullptr); | 
|  | 2375 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); | 
|  | 2376 |  | 
|  | 2377 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); | 
|  | 2378 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); | 
|  | 2379 | ASSERT_EQ(keyArgs.source, verified->source); | 
|  | 2380 | ASSERT_EQ(keyArgs.displayId, verified->displayId); | 
|  | 2381 |  | 
|  | 2382 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); | 
|  | 2383 |  | 
|  | 2384 | ASSERT_EQ(keyArgs.action, verifiedKey.action); | 
|  | 2385 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2386 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); | 
|  | 2387 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); | 
|  | 2388 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); | 
|  | 2389 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); | 
|  | 2390 | ASSERT_EQ(0, verifiedKey.repeatCount); | 
|  | 2391 | } | 
|  | 2392 |  | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2393 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2394 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2395 | sp<FakeWindowHandle> window = | 
|  | 2396 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 2397 |  | 
|  | 2398 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2399 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2400 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2401 |  | 
|  | 2402 | NotifyMotionArgs motionArgs = | 
|  | 2403 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2404 | ADISPLAY_ID_DEFAULT); | 
|  | 2405 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2406 |  | 
|  | 2407 | InputEvent* event = window->consume(); | 
|  | 2408 | ASSERT_NE(event, nullptr); | 
|  | 2409 |  | 
|  | 2410 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); | 
|  | 2411 | ASSERT_NE(verified, nullptr); | 
|  | 2412 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); | 
|  | 2413 |  | 
|  | 2414 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); | 
|  | 2415 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); | 
|  | 2416 | EXPECT_EQ(motionArgs.source, verified->source); | 
|  | 2417 | EXPECT_EQ(motionArgs.displayId, verified->displayId); | 
|  | 2418 |  | 
|  | 2419 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); | 
|  | 2420 |  | 
|  | 2421 | EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX); | 
|  | 2422 | EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY); | 
|  | 2423 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); | 
|  | 2424 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); | 
|  | 2425 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); | 
|  | 2426 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); | 
|  | 2427 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); | 
|  | 2428 | } | 
|  | 2429 |  | 
| Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 2430 | TEST_F(InputDispatcherTest, NonPointerMotionEvent_JoystickAndTouchpadNotTransformed) { | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2431 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2432 | sp<FakeWindowHandle> window = | 
|  | 2433 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 2434 | const std::string name = window->getName(); | 
|  | 2435 |  | 
|  | 2436 | // Window gets transformed by offset values. | 
|  | 2437 | window->setWindowOffset(500.0f, 500.0f); | 
|  | 2438 |  | 
|  | 2439 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2440 | window->setFocusable(true); | 
|  | 2441 |  | 
|  | 2442 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2443 |  | 
|  | 2444 | // First, we set focused window so that focusedWindowHandle is not null. | 
|  | 2445 | setFocusedWindow(window); | 
|  | 2446 |  | 
|  | 2447 | // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked. | 
|  | 2448 | window->consumeFocusEvent(true); | 
|  | 2449 |  | 
| Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 2450 | constexpr const std::array nonTransformedSources = {std::pair(AINPUT_SOURCE_TOUCHPAD, | 
|  | 2451 | AMOTION_EVENT_ACTION_DOWN), | 
|  | 2452 | std::pair(AINPUT_SOURCE_JOYSTICK, | 
|  | 2453 | AMOTION_EVENT_ACTION_MOVE)}; | 
|  | 2454 | for (const auto& [source, action] : nonTransformedSources) { | 
|  | 2455 | const NotifyMotionArgs motionArgs = generateMotionArgs(action, source, ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2456 | mDispatcher->notifyMotion(&motionArgs); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2457 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 2458 | MotionEvent* event = window->consumeMotion(); | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2459 | ASSERT_NE(event, nullptr); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2460 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 2461 | const MotionEvent& motionEvent = *event; | 
| Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 2462 | EXPECT_EQ(action, motionEvent.getAction()); | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2463 | EXPECT_EQ(motionArgs.pointerCount, motionEvent.getPointerCount()); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2464 |  | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2465 | float expectedX = motionArgs.pointerCoords[0].getX(); | 
|  | 2466 | float expectedY = motionArgs.pointerCoords[0].getY(); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2467 |  | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2468 | // Ensure the axis values from the final motion event are not transformed. | 
|  | 2469 | EXPECT_EQ(expectedX, motionEvent.getX(0)) | 
|  | 2470 | << "expected " << expectedX << " for x coord of " << name.c_str() << ", got " | 
|  | 2471 | << motionEvent.getX(0); | 
|  | 2472 | EXPECT_EQ(expectedY, motionEvent.getY(0)) | 
|  | 2473 | << "expected " << expectedY << " for y coord of " << name.c_str() << ", got " | 
|  | 2474 | << motionEvent.getY(0); | 
|  | 2475 | // Ensure the raw and transformed axis values for the motion event are the same. | 
|  | 2476 | EXPECT_EQ(motionEvent.getRawX(0), motionEvent.getX(0)) | 
|  | 2477 | << "expected raw and transformed X-axis values to be equal"; | 
|  | 2478 | EXPECT_EQ(motionEvent.getRawY(0), motionEvent.getY(0)) | 
|  | 2479 | << "expected raw and transformed Y-axis values to be equal"; | 
|  | 2480 | } | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2481 | } | 
|  | 2482 |  | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2483 | /** | 
|  | 2484 | * Ensure that separate calls to sign the same data are generating the same key. | 
|  | 2485 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance | 
|  | 2486 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky | 
|  | 2487 | * tests. | 
|  | 2488 | */ | 
|  | 2489 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { | 
|  | 2490 | KeyEvent event = getTestKeyEvent(); | 
|  | 2491 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); | 
|  | 2492 |  | 
|  | 2493 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); | 
|  | 2494 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); | 
|  | 2495 | ASSERT_EQ(hmac1, hmac2); | 
|  | 2496 | } | 
|  | 2497 |  | 
|  | 2498 | /** | 
|  | 2499 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. | 
|  | 2500 | */ | 
|  | 2501 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { | 
|  | 2502 | KeyEvent event = getTestKeyEvent(); | 
|  | 2503 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); | 
|  | 2504 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); | 
|  | 2505 |  | 
|  | 2506 | verifiedEvent.deviceId += 1; | 
|  | 2507 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2508 |  | 
|  | 2509 | verifiedEvent.source += 1; | 
|  | 2510 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2511 |  | 
|  | 2512 | verifiedEvent.eventTimeNanos += 1; | 
|  | 2513 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2514 |  | 
|  | 2515 | verifiedEvent.displayId += 1; | 
|  | 2516 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2517 |  | 
|  | 2518 | verifiedEvent.action += 1; | 
|  | 2519 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2520 |  | 
|  | 2521 | verifiedEvent.downTimeNanos += 1; | 
|  | 2522 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2523 |  | 
|  | 2524 | verifiedEvent.flags += 1; | 
|  | 2525 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2526 |  | 
|  | 2527 | verifiedEvent.keyCode += 1; | 
|  | 2528 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2529 |  | 
|  | 2530 | verifiedEvent.scanCode += 1; | 
|  | 2531 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2532 |  | 
|  | 2533 | verifiedEvent.metaState += 1; | 
|  | 2534 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2535 |  | 
|  | 2536 | verifiedEvent.repeatCount += 1; | 
|  | 2537 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2538 | } | 
|  | 2539 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2540 | TEST_F(InputDispatcherTest, SetFocusedWindow) { | 
|  | 2541 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2542 | sp<FakeWindowHandle> windowTop = | 
|  | 2543 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 2544 | sp<FakeWindowHandle> windowSecond = | 
|  | 2545 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 2546 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2547 |  | 
|  | 2548 | // Top window is also focusable but is not granted focus. | 
|  | 2549 | windowTop->setFocusable(true); | 
|  | 2550 | windowSecond->setFocusable(true); | 
|  | 2551 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
|  | 2552 | setFocusedWindow(windowSecond); | 
|  | 2553 |  | 
|  | 2554 | windowSecond->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2555 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 2556 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2557 |  | 
|  | 2558 | // Focused window should receive event. | 
|  | 2559 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 2560 | windowTop->assertNoEvents(); | 
|  | 2561 | } | 
|  | 2562 |  | 
|  | 2563 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { | 
|  | 2564 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2565 | sp<FakeWindowHandle> window = | 
|  | 2566 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 2567 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2568 |  | 
|  | 2569 | window->setFocusable(true); | 
|  | 2570 | // Release channel for window is no longer valid. | 
|  | 2571 | window->releaseChannel(); | 
|  | 2572 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2573 | setFocusedWindow(window); | 
|  | 2574 |  | 
|  | 2575 | // Test inject a key down, should timeout. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2576 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 2577 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2578 |  | 
|  | 2579 | // window channel is invalid, so it should not receive any input event. | 
|  | 2580 | window->assertNoEvents(); | 
|  | 2581 | } | 
|  | 2582 |  | 
|  | 2583 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { | 
|  | 2584 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2585 | sp<FakeWindowHandle> window = | 
|  | 2586 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 2587 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2588 |  | 
|  | 2589 | // Window is not focusable. | 
|  | 2590 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2591 | setFocusedWindow(window); | 
|  | 2592 |  | 
|  | 2593 | // Test inject a key down, should timeout. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2594 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 2595 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2596 |  | 
|  | 2597 | // window is invalid, so it should not receive any input event. | 
|  | 2598 | window->assertNoEvents(); | 
|  | 2599 | } | 
|  | 2600 |  | 
|  | 2601 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { | 
|  | 2602 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2603 | sp<FakeWindowHandle> windowTop = | 
|  | 2604 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 2605 | sp<FakeWindowHandle> windowSecond = | 
|  | 2606 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 2607 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2608 |  | 
|  | 2609 | windowTop->setFocusable(true); | 
|  | 2610 | windowSecond->setFocusable(true); | 
|  | 2611 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
|  | 2612 | setFocusedWindow(windowTop); | 
|  | 2613 | windowTop->consumeFocusEvent(true); | 
|  | 2614 |  | 
|  | 2615 | setFocusedWindow(windowSecond, windowTop); | 
|  | 2616 | windowSecond->consumeFocusEvent(true); | 
|  | 2617 | windowTop->consumeFocusEvent(false); | 
|  | 2618 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2619 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 2620 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2621 |  | 
|  | 2622 | // Focused window should receive event. | 
|  | 2623 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 2624 | } | 
|  | 2625 |  | 
|  | 2626 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { | 
|  | 2627 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2628 | sp<FakeWindowHandle> windowTop = | 
|  | 2629 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 2630 | sp<FakeWindowHandle> windowSecond = | 
|  | 2631 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 2632 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2633 |  | 
|  | 2634 | windowTop->setFocusable(true); | 
|  | 2635 | windowSecond->setFocusable(true); | 
|  | 2636 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
|  | 2637 | setFocusedWindow(windowSecond, windowTop); | 
|  | 2638 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2639 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 2640 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2641 |  | 
|  | 2642 | // Event should be dropped. | 
|  | 2643 | windowTop->assertNoEvents(); | 
|  | 2644 | windowSecond->assertNoEvents(); | 
|  | 2645 | } | 
|  | 2646 |  | 
|  | 2647 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { | 
|  | 2648 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2649 | sp<FakeWindowHandle> window = | 
|  | 2650 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 2651 | sp<FakeWindowHandle> previousFocusedWindow = | 
|  | 2652 | new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow", | 
|  | 2653 | ADISPLAY_ID_DEFAULT); | 
|  | 2654 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2655 |  | 
|  | 2656 | window->setFocusable(true); | 
|  | 2657 | previousFocusedWindow->setFocusable(true); | 
|  | 2658 | window->setVisible(false); | 
|  | 2659 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); | 
|  | 2660 | setFocusedWindow(previousFocusedWindow); | 
|  | 2661 | previousFocusedWindow->consumeFocusEvent(true); | 
|  | 2662 |  | 
|  | 2663 | // Requesting focus on invisible window takes focus from currently focused window. | 
|  | 2664 | setFocusedWindow(window); | 
|  | 2665 | previousFocusedWindow->consumeFocusEvent(false); | 
|  | 2666 |  | 
|  | 2667 | // Injected key goes to pending queue. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2668 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2669 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2670 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2671 |  | 
|  | 2672 | // Window does not get focus event or key down. | 
|  | 2673 | window->assertNoEvents(); | 
|  | 2674 |  | 
|  | 2675 | // Window becomes visible. | 
|  | 2676 | window->setVisible(true); | 
|  | 2677 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2678 |  | 
|  | 2679 | // Window receives focus event. | 
|  | 2680 | window->consumeFocusEvent(true); | 
|  | 2681 | // Focused window receives key down. | 
|  | 2682 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 2683 | } | 
|  | 2684 |  | 
| Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 2685 | TEST_F(InputDispatcherTest, DisplayRemoved) { | 
|  | 2686 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2687 | sp<FakeWindowHandle> window = | 
|  | 2688 | new FakeWindowHandle(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); | 
|  | 2689 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2690 |  | 
|  | 2691 | // window is granted focus. | 
|  | 2692 | window->setFocusable(true); | 
|  | 2693 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2694 | setFocusedWindow(window); | 
|  | 2695 | window->consumeFocusEvent(true); | 
|  | 2696 |  | 
|  | 2697 | // When a display is removed window loses focus. | 
|  | 2698 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); | 
|  | 2699 | window->consumeFocusEvent(false); | 
|  | 2700 | } | 
|  | 2701 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2702 | /** | 
|  | 2703 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, | 
|  | 2704 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top | 
|  | 2705 | * of the 'slipperyEnterWindow'. | 
|  | 2706 | * | 
|  | 2707 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such | 
|  | 2708 | * a way so that the touched location is no longer covered by the top window. | 
|  | 2709 | * | 
|  | 2710 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now | 
|  | 2711 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had | 
|  | 2712 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive | 
|  | 2713 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting | 
|  | 2714 | * with ACTION_DOWN). | 
|  | 2715 | * Thus, the touch has been transferred from the top window into the bottom window, because the top | 
|  | 2716 | * window moved itself away from the touched location and had Flag::SLIPPERY. | 
|  | 2717 | * | 
|  | 2718 | * Even though the top window moved away from the touched location, it is still obscuring the bottom | 
|  | 2719 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ | 
|  | 2720 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. | 
|  | 2721 | * | 
|  | 2722 | * In this test, we ensure that the event received by the bottom window has | 
|  | 2723 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. | 
|  | 2724 | */ | 
|  | 2725 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { | 
|  | 2726 | constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1; | 
|  | 2727 | constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1; | 
|  | 2728 |  | 
|  | 2729 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2730 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2731 |  | 
|  | 2732 | sp<FakeWindowHandle> slipperyExitWindow = | 
|  | 2733 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2734 | slipperyExitWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SLIPPERY); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2735 | // Make sure this one overlaps the bottom window | 
|  | 2736 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); | 
|  | 2737 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom | 
|  | 2738 | // one. Windows with the same owner are not considered to be occluding each other. | 
|  | 2739 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); | 
|  | 2740 |  | 
|  | 2741 | sp<FakeWindowHandle> slipperyEnterWindow = | 
|  | 2742 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 2743 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 2744 |  | 
|  | 2745 | mDispatcher->setInputWindows( | 
|  | 2746 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); | 
|  | 2747 |  | 
|  | 2748 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions | 
|  | 2749 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2750 | ADISPLAY_ID_DEFAULT, {{50, 50}}); | 
|  | 2751 | mDispatcher->notifyMotion(&args); | 
|  | 2752 | slipperyExitWindow->consumeMotionDown(); | 
|  | 2753 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); | 
|  | 2754 | mDispatcher->setInputWindows( | 
|  | 2755 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); | 
|  | 2756 |  | 
|  | 2757 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2758 | ADISPLAY_ID_DEFAULT, {{51, 51}}); | 
|  | 2759 | mDispatcher->notifyMotion(&args); | 
|  | 2760 |  | 
|  | 2761 | slipperyExitWindow->consumeMotionCancel(); | 
|  | 2762 |  | 
|  | 2763 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, | 
|  | 2764 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); | 
|  | 2765 | } | 
|  | 2766 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2767 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { | 
|  | 2768 | protected: | 
|  | 2769 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms | 
|  | 2770 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000;   // 40 ms | 
|  | 2771 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2772 | std::shared_ptr<FakeApplicationHandle> mApp; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2773 | sp<FakeWindowHandle> mWindow; | 
|  | 2774 |  | 
|  | 2775 | virtual void SetUp() override { | 
|  | 2776 | mFakePolicy = new FakeInputDispatcherPolicy(); | 
|  | 2777 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); | 
|  | 2778 | mDispatcher = new InputDispatcher(mFakePolicy); | 
|  | 2779 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); | 
|  | 2780 | ASSERT_EQ(OK, mDispatcher->start()); | 
|  | 2781 |  | 
|  | 2782 | setUpWindow(); | 
|  | 2783 | } | 
|  | 2784 |  | 
|  | 2785 | void setUpWindow() { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2786 | mApp = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2787 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2788 |  | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2789 | mWindow->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2790 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2791 | setFocusedWindow(mWindow); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2792 | mWindow->consumeFocusEvent(true); | 
|  | 2793 | } | 
|  | 2794 |  | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2795 | void sendAndConsumeKeyDown(int32_t deviceId) { | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2796 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2797 | keyArgs.deviceId = deviceId; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2798 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event | 
|  | 2799 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2800 |  | 
|  | 2801 | // Window should receive key down event. | 
|  | 2802 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 2803 | } | 
|  | 2804 |  | 
|  | 2805 | void expectKeyRepeatOnce(int32_t repeatCount) { | 
|  | 2806 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); | 
|  | 2807 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 2808 | ASSERT_NE(nullptr, repeatEvent); | 
|  | 2809 |  | 
|  | 2810 | uint32_t eventType = repeatEvent->getType(); | 
|  | 2811 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); | 
|  | 2812 |  | 
|  | 2813 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); | 
|  | 2814 | uint32_t eventAction = repeatKeyEvent->getAction(); | 
|  | 2815 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); | 
|  | 2816 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); | 
|  | 2817 | } | 
|  | 2818 |  | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2819 | void sendAndConsumeKeyUp(int32_t deviceId) { | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2820 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2821 | keyArgs.deviceId = deviceId; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2822 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event | 
|  | 2823 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2824 |  | 
|  | 2825 | // Window should receive key down event. | 
|  | 2826 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, | 
|  | 2827 | 0 /*expectedFlags*/); | 
|  | 2828 | } | 
|  | 2829 | }; | 
|  | 2830 |  | 
|  | 2831 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2832 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 2833 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 2834 | expectKeyRepeatOnce(repeatCount); | 
|  | 2835 | } | 
|  | 2836 | } | 
|  | 2837 |  | 
|  | 2838 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { | 
|  | 2839 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 2840 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 2841 | expectKeyRepeatOnce(repeatCount); | 
|  | 2842 | } | 
|  | 2843 | sendAndConsumeKeyDown(2 /* deviceId */); | 
|  | 2844 | /* repeatCount will start from 1 for deviceId 2 */ | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2845 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 2846 | expectKeyRepeatOnce(repeatCount); | 
|  | 2847 | } | 
|  | 2848 | } | 
|  | 2849 |  | 
|  | 2850 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2851 | sendAndConsumeKeyDown(1 /* deviceId */); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2852 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2853 | sendAndConsumeKeyUp(1 /* deviceId */); | 
|  | 2854 | mWindow->assertNoEvents(); | 
|  | 2855 | } | 
|  | 2856 |  | 
|  | 2857 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { | 
|  | 2858 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 2859 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 2860 | sendAndConsumeKeyDown(2 /* deviceId */); | 
|  | 2861 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 2862 | // Stale key up from device 1. | 
|  | 2863 | sendAndConsumeKeyUp(1 /* deviceId */); | 
|  | 2864 | // Device 2 is still down, keep repeating | 
|  | 2865 | expectKeyRepeatOnce(2 /*repeatCount*/); | 
|  | 2866 | expectKeyRepeatOnce(3 /*repeatCount*/); | 
|  | 2867 | // Device 2 key up | 
|  | 2868 | sendAndConsumeKeyUp(2 /* deviceId */); | 
|  | 2869 | mWindow->assertNoEvents(); | 
|  | 2870 | } | 
|  | 2871 |  | 
|  | 2872 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { | 
|  | 2873 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 2874 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 2875 | sendAndConsumeKeyDown(2 /* deviceId */); | 
|  | 2876 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 2877 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. | 
|  | 2878 | sendAndConsumeKeyUp(2 /* deviceId */); | 
|  | 2879 | // Device 1 still holds key down, but the repeating was already stopped | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2880 | mWindow->assertNoEvents(); | 
|  | 2881 | } | 
|  | 2882 |  | 
|  | 2883 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2884 | sendAndConsumeKeyDown(1 /* deviceId */); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2885 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 2886 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 2887 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; | 
|  | 2888 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, | 
|  | 2889 | IdGenerator::getSource(repeatEvent->getId())); | 
|  | 2890 | } | 
|  | 2891 | } | 
|  | 2892 |  | 
|  | 2893 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2894 | sendAndConsumeKeyDown(1 /* deviceId */); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2895 |  | 
|  | 2896 | std::unordered_set<int32_t> idSet; | 
|  | 2897 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 2898 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 2899 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; | 
|  | 2900 | int32_t id = repeatEvent->getId(); | 
|  | 2901 | EXPECT_EQ(idSet.end(), idSet.find(id)); | 
|  | 2902 | idSet.insert(id); | 
|  | 2903 | } | 
|  | 2904 | } | 
|  | 2905 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2906 | /* Test InputDispatcher for MultiDisplay */ | 
|  | 2907 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { | 
|  | 2908 | public: | 
|  | 2909 | static constexpr int32_t SECOND_DISPLAY_ID = 1; | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2910 | virtual void SetUp() override { | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2911 | InputDispatcherTest::SetUp(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2912 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2913 | application1 = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2914 | windowInPrimary = | 
|  | 2915 | new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2916 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2917 | // Set focus window for primary display, but focused display would be second one. | 
|  | 2918 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2919 | windowInPrimary->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2920 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2921 | setFocusedWindow(windowInPrimary); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2922 | windowInPrimary->consumeFocusEvent(true); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2923 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2924 | application2 = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2925 | windowInSecondary = | 
|  | 2926 | new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2927 | // Set focus to second display window. | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2928 | // Set focus display to second one. | 
|  | 2929 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); | 
|  | 2930 | // Set focus window for second display. | 
|  | 2931 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2932 | windowInSecondary->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2933 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2934 | setFocusedWindow(windowInSecondary); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2935 | windowInSecondary->consumeFocusEvent(true); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2936 | } | 
|  | 2937 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2938 | virtual void TearDown() override { | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2939 | InputDispatcherTest::TearDown(); | 
|  | 2940 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2941 | application1.reset(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2942 | windowInPrimary.clear(); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2943 | application2.reset(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2944 | windowInSecondary.clear(); | 
|  | 2945 | } | 
|  | 2946 |  | 
|  | 2947 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2948 | std::shared_ptr<FakeApplicationHandle> application1; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2949 | sp<FakeWindowHandle> windowInPrimary; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2950 | std::shared_ptr<FakeApplicationHandle> application2; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2951 | sp<FakeWindowHandle> windowInSecondary; | 
|  | 2952 | }; | 
|  | 2953 |  | 
|  | 2954 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { | 
|  | 2955 | // Test touch down on primary display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2956 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2957 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 2958 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2959 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2960 | windowInSecondary->assertNoEvents(); | 
|  | 2961 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2962 | // Test touch down on second display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2963 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2964 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) | 
|  | 2965 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2966 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2967 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2968 | } | 
|  | 2969 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2970 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2971 | // Test inject a key down with display id specified. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 2972 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2973 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2974 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2975 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2976 | windowInSecondary->assertNoEvents(); | 
|  | 2977 |  | 
|  | 2978 | // Test inject a key down without display id specified. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 2979 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2980 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2981 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2982 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2983 |  | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2984 | // Remove all windows in secondary display. | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2985 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2986 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2987 | // Old focus should receive a cancel event. | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2988 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, | 
|  | 2989 | AKEY_EVENT_FLAG_CANCELED); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2990 |  | 
|  | 2991 | // Test inject a key down, should timeout because of no target window. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 2992 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2993 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2994 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2995 | windowInSecondary->consumeFocusEvent(false); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2996 | windowInSecondary->assertNoEvents(); | 
|  | 2997 | } | 
|  | 2998 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2999 | // Test per-display input monitors for motion event. | 
|  | 3000 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3001 | FakeMonitorReceiver monitorInPrimary = | 
|  | 3002 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
|  | 3003 | FakeMonitorReceiver monitorInSecondary = | 
|  | 3004 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3005 |  | 
|  | 3006 | // Test touch down on primary display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3007 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3008 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 3009 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3010 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3011 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3012 | windowInSecondary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3013 | monitorInSecondary.assertNoEvents(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3014 |  | 
|  | 3015 | // Test touch down on second display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3016 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3017 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) | 
|  | 3018 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3019 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3020 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3021 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3022 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3023 |  | 
|  | 3024 | // Test inject a non-pointer motion event. | 
|  | 3025 | // If specific a display, it will dispatch to the focused window of particular display, | 
|  | 3026 | // or it will dispatch to the focused window of focused display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3027 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3028 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) | 
|  | 3029 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3030 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3031 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3032 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3033 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3034 | } | 
|  | 3035 |  | 
|  | 3036 | // Test per-display input monitors for key event. | 
|  | 3037 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3038 | // Input monitor per display. | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3039 | FakeMonitorReceiver monitorInPrimary = | 
|  | 3040 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
|  | 3041 | FakeMonitorReceiver monitorInSecondary = | 
|  | 3042 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3043 |  | 
|  | 3044 | // Test inject a key down. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3045 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 3046 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3047 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3048 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3049 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3050 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3051 | } | 
|  | 3052 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3053 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { | 
|  | 3054 | sp<FakeWindowHandle> secondWindowInPrimary = | 
|  | 3055 | new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); | 
|  | 3056 | secondWindowInPrimary->setFocusable(true); | 
|  | 3057 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); | 
|  | 3058 | setFocusedWindow(secondWindowInPrimary); | 
|  | 3059 | windowInPrimary->consumeFocusEvent(false); | 
|  | 3060 | secondWindowInPrimary->consumeFocusEvent(true); | 
|  | 3061 |  | 
|  | 3062 | // Test inject a key down. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3063 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
|  | 3064 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3065 | windowInPrimary->assertNoEvents(); | 
|  | 3066 | windowInSecondary->assertNoEvents(); | 
|  | 3067 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 3068 | } | 
|  | 3069 |  | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3070 | class InputFilterTest : public InputDispatcherTest { | 
|  | 3071 | protected: | 
|  | 3072 | static constexpr int32_t SECOND_DISPLAY_ID = 1; | 
|  | 3073 |  | 
|  | 3074 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) { | 
|  | 3075 | NotifyMotionArgs motionArgs; | 
|  | 3076 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3077 | motionArgs = | 
|  | 3078 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3079 | mDispatcher->notifyMotion(&motionArgs); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3080 | motionArgs = | 
|  | 3081 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3082 | mDispatcher->notifyMotion(&motionArgs); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3083 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3084 | if (expectToBeFiltered) { | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 3085 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3086 | } else { | 
|  | 3087 | mFakePolicy->assertFilterInputEventWasNotCalled(); | 
|  | 3088 | } | 
|  | 3089 | } | 
|  | 3090 |  | 
|  | 3091 | void testNotifyKey(bool expectToBeFiltered) { | 
|  | 3092 | NotifyKeyArgs keyArgs; | 
|  | 3093 |  | 
|  | 3094 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); | 
|  | 3095 | mDispatcher->notifyKey(&keyArgs); | 
|  | 3096 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); | 
|  | 3097 | mDispatcher->notifyKey(&keyArgs); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3098 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3099 |  | 
|  | 3100 | if (expectToBeFiltered) { | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 3101 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3102 | } else { | 
|  | 3103 | mFakePolicy->assertFilterInputEventWasNotCalled(); | 
|  | 3104 | } | 
|  | 3105 | } | 
|  | 3106 | }; | 
|  | 3107 |  | 
|  | 3108 | // Test InputFilter for MotionEvent | 
|  | 3109 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { | 
|  | 3110 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. | 
|  | 3111 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); | 
|  | 3112 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); | 
|  | 3113 |  | 
|  | 3114 | // Enable InputFilter | 
|  | 3115 | mDispatcher->setInputFilterEnabled(true); | 
|  | 3116 | // Test touch on both primary and second display, and check if both events are filtered. | 
|  | 3117 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); | 
|  | 3118 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); | 
|  | 3119 |  | 
|  | 3120 | // Disable InputFilter | 
|  | 3121 | mDispatcher->setInputFilterEnabled(false); | 
|  | 3122 | // Test touch on both primary and second display, and check if both events aren't filtered. | 
|  | 3123 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); | 
|  | 3124 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); | 
|  | 3125 | } | 
|  | 3126 |  | 
|  | 3127 | // Test InputFilter for KeyEvent | 
|  | 3128 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { | 
|  | 3129 | // Since the InputFilter is disabled by default, check if key event aren't filtered. | 
|  | 3130 | testNotifyKey(/*expectToBeFiltered*/ false); | 
|  | 3131 |  | 
|  | 3132 | // Enable InputFilter | 
|  | 3133 | mDispatcher->setInputFilterEnabled(true); | 
|  | 3134 | // Send a key event, and check if it is filtered. | 
|  | 3135 | testNotifyKey(/*expectToBeFiltered*/ true); | 
|  | 3136 |  | 
|  | 3137 | // Disable InputFilter | 
|  | 3138 | mDispatcher->setInputFilterEnabled(false); | 
|  | 3139 | // Send a key event, and check if it isn't filtered. | 
|  | 3140 | testNotifyKey(/*expectToBeFiltered*/ false); | 
|  | 3141 | } | 
|  | 3142 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3143 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { | 
|  | 3144 | protected: | 
|  | 3145 | virtual void SetUp() override { | 
|  | 3146 | InputDispatcherTest::SetUp(); | 
|  | 3147 |  | 
|  | 3148 | /** | 
|  | 3149 | * We don't need to enable input filter to test the injected event policy, but we enabled it | 
|  | 3150 | * here to make the tests more realistic, since this policy only matters when inputfilter is | 
|  | 3151 | * on. | 
|  | 3152 | */ | 
|  | 3153 | mDispatcher->setInputFilterEnabled(true); | 
|  | 3154 |  | 
|  | 3155 | std::shared_ptr<InputApplicationHandle> application = | 
|  | 3156 | std::make_shared<FakeApplicationHandle>(); | 
|  | 3157 | mWindow = | 
|  | 3158 | new FakeWindowHandle(application, mDispatcher, "Test Window", ADISPLAY_ID_DEFAULT); | 
|  | 3159 |  | 
|  | 3160 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3161 | mWindow->setFocusable(true); | 
|  | 3162 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3163 | setFocusedWindow(mWindow); | 
|  | 3164 | mWindow->consumeFocusEvent(true); | 
|  | 3165 | } | 
|  | 3166 |  | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3167 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, | 
|  | 3168 | int32_t flags) { | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3169 | KeyEvent event; | 
|  | 3170 |  | 
|  | 3171 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 3172 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, | 
|  | 3173 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, | 
|  | 3174 | KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime); | 
|  | 3175 | const int32_t additionalPolicyFlags = | 
|  | 3176 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; | 
|  | 3177 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3178 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
|  | 3179 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, | 
|  | 3180 | policyFlags | additionalPolicyFlags)); | 
|  | 3181 |  | 
|  | 3182 | InputEvent* received = mWindow->consume(); | 
|  | 3183 | ASSERT_NE(nullptr, received); | 
|  | 3184 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3185 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY); | 
|  | 3186 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); | 
|  | 3187 | ASSERT_EQ(flags, keyEvent.getFlags()); | 
|  | 3188 | } | 
|  | 3189 |  | 
|  | 3190 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, | 
|  | 3191 | int32_t flags) { | 
|  | 3192 | MotionEvent event; | 
|  | 3193 | PointerProperties pointerProperties[1]; | 
|  | 3194 | PointerCoords pointerCoords[1]; | 
|  | 3195 | pointerProperties[0].clear(); | 
|  | 3196 | pointerProperties[0].id = 0; | 
|  | 3197 | pointerCoords[0].clear(); | 
|  | 3198 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); | 
|  | 3199 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); | 
|  | 3200 |  | 
|  | 3201 | ui::Transform identityTransform; | 
|  | 3202 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 3203 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3204 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, | 
|  | 3205 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, | 
|  | 3206 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 3207 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0, | 
|  | 3208 | 0 /*INVALID_DISPLAY_SIZE*/, 0 /*INVALID_DISPLAY_SIZE*/, eventTime, | 
|  | 3209 | eventTime, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3210 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
|  | 3211 |  | 
|  | 3212 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; | 
|  | 3213 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3214 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
|  | 3215 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, | 
|  | 3216 | policyFlags | additionalPolicyFlags)); | 
|  | 3217 |  | 
|  | 3218 | InputEvent* received = mWindow->consume(); | 
|  | 3219 | ASSERT_NE(nullptr, received); | 
|  | 3220 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); | 
|  | 3221 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION); | 
|  | 3222 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); | 
|  | 3223 | ASSERT_EQ(flags, motionEvent.getFlags()); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3224 | } | 
|  | 3225 |  | 
|  | 3226 | private: | 
|  | 3227 | sp<FakeWindowHandle> mWindow; | 
|  | 3228 | }; | 
|  | 3229 |  | 
|  | 3230 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3231 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input | 
|  | 3232 | // filter. Without it, the event will no different from a regularly injected event, and the | 
|  | 3233 | // injected device id will be overwritten. | 
|  | 3234 | testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, | 
|  | 3235 | 0 /*flags*/); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3236 | } | 
|  | 3237 |  | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3238 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3239 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3240 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, | 
|  | 3241 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); | 
|  | 3242 | } | 
|  | 3243 |  | 
|  | 3244 | TEST_F(InputFilterInjectionPolicyTest, | 
|  | 3245 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { | 
|  | 3246 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, | 
|  | 3247 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, | 
|  | 3248 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3249 | } | 
|  | 3250 |  | 
|  | 3251 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { | 
|  | 3252 | testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3253 | VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3254 | } | 
|  | 3255 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3256 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3257 | virtual void SetUp() override { | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3258 | InputDispatcherTest::SetUp(); | 
|  | 3259 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3260 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 3261 | std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3262 | mUnfocusedWindow = | 
|  | 3263 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3264 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); | 
|  | 3265 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this | 
|  | 3266 | // window. | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3267 | mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3268 |  | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3269 | mFocusedWindow = | 
|  | 3270 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 3271 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3272 | mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3273 |  | 
|  | 3274 | // Set focused application. | 
|  | 3275 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3276 | mFocusedWindow->setFocusable(true); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3277 |  | 
|  | 3278 | // Expect one focus window exist in display. | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3279 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3280 | setFocusedWindow(mFocusedWindow); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3281 | mFocusedWindow->consumeFocusEvent(true); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3282 | } | 
|  | 3283 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3284 | virtual void TearDown() override { | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3285 | InputDispatcherTest::TearDown(); | 
|  | 3286 |  | 
|  | 3287 | mUnfocusedWindow.clear(); | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3288 | mFocusedWindow.clear(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3289 | } | 
|  | 3290 |  | 
|  | 3291 | protected: | 
|  | 3292 | sp<FakeWindowHandle> mUnfocusedWindow; | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3293 | sp<FakeWindowHandle> mFocusedWindow; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3294 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3295 | }; | 
|  | 3296 |  | 
|  | 3297 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action | 
|  | 3298 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received | 
|  | 3299 | // the onPointerDownOutsideFocus callback. | 
|  | 3300 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3301 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3302 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3303 | {20, 20})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3304 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3305 | mUnfocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3306 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3307 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3308 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); | 
|  | 3309 | } | 
|  | 3310 |  | 
|  | 3311 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action | 
|  | 3312 | // DOWN on the window that doesn't have focus. Ensure no window received the | 
|  | 3313 | // onPointerDownOutsideFocus callback. | 
|  | 3314 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3315 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3316 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3317 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3318 | mFocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3319 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3320 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3321 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3322 | } | 
|  | 3323 |  | 
|  | 3324 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't | 
|  | 3325 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. | 
|  | 3326 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3327 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3328 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3329 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3330 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3331 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3332 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3333 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3334 | } | 
|  | 3335 |  | 
|  | 3336 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action | 
|  | 3337 | // DOWN on the window that already has focus. Ensure no window received the | 
|  | 3338 | // onPointerDownOutsideFocus callback. | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3339 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3340 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3341 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3342 | FOCUSED_WINDOW_TOUCH_POINT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3343 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3344 | mFocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3345 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3346 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3347 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3348 | } | 
|  | 3349 |  | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3350 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag | 
|  | 3351 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. | 
|  | 3352 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { | 
|  | 3353 | const MotionEvent event = | 
|  | 3354 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 3355 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 3356 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20)) | 
|  | 3357 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) | 
|  | 3358 | .build(); | 
|  | 3359 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) | 
|  | 3360 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 3361 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); | 
|  | 3362 |  | 
|  | 3363 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3364 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
|  | 3365 | // Ensure that the unfocused window did not receive any FOCUS events. | 
|  | 3366 | mUnfocusedWindow->assertNoEvents(); | 
|  | 3367 | } | 
|  | 3368 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3369 | // These tests ensures we can send touch events to a single client when there are multiple input | 
|  | 3370 | // windows that point to the same client token. | 
|  | 3371 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { | 
|  | 3372 | virtual void SetUp() override { | 
|  | 3373 | InputDispatcherTest::SetUp(); | 
|  | 3374 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3375 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 3376 | std::make_shared<FakeApplicationHandle>(); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3377 | mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1", | 
|  | 3378 | ADISPLAY_ID_DEFAULT); | 
|  | 3379 | // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window. | 
|  | 3380 | // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows. | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3381 | mWindow1->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3382 | mWindow1->setFrame(Rect(0, 0, 100, 100)); | 
|  | 3383 |  | 
|  | 3384 | mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2", | 
|  | 3385 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3386 | mWindow2->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3387 | mWindow2->setFrame(Rect(100, 100, 200, 200)); | 
|  | 3388 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3389 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3390 | } | 
|  | 3391 |  | 
|  | 3392 | protected: | 
|  | 3393 | sp<FakeWindowHandle> mWindow1; | 
|  | 3394 | sp<FakeWindowHandle> mWindow2; | 
|  | 3395 |  | 
|  | 3396 | // Helper function to convert the point from screen coordinates into the window's space | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3397 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3398 | vec2 vals = windowInfo->transform.transform(point.x, point.y); | 
|  | 3399 | return {vals.x, vals.y}; | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3400 | } | 
|  | 3401 |  | 
|  | 3402 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, | 
|  | 3403 | const std::vector<PointF>& points) { | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3404 | const std::string name = window->getName(); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3405 | InputEvent* event = window->consume(); | 
|  | 3406 |  | 
|  | 3407 | ASSERT_NE(nullptr, event) << name.c_str() | 
|  | 3408 | << ": consumer should have returned non-NULL event."; | 
|  | 3409 |  | 
|  | 3410 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) | 
|  | 3411 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) | 
|  | 3412 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; | 
|  | 3413 |  | 
|  | 3414 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); | 
|  | 3415 | EXPECT_EQ(expectedAction, motionEvent.getAction()); | 
|  | 3416 |  | 
|  | 3417 | for (size_t i = 0; i < points.size(); i++) { | 
|  | 3418 | float expectedX = points[i].x; | 
|  | 3419 | float expectedY = points[i].y; | 
|  | 3420 |  | 
|  | 3421 | EXPECT_EQ(expectedX, motionEvent.getX(i)) | 
|  | 3422 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() | 
|  | 3423 | << ", got " << motionEvent.getX(i); | 
|  | 3424 | EXPECT_EQ(expectedY, motionEvent.getY(i)) | 
|  | 3425 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() | 
|  | 3426 | << ", got " << motionEvent.getY(i); | 
|  | 3427 | } | 
|  | 3428 | } | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3429 |  | 
|  | 3430 | void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints, | 
|  | 3431 | std::vector<PointF> expectedPoints) { | 
|  | 3432 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3433 | ADISPLAY_ID_DEFAULT, touchedPoints); | 
|  | 3434 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 3435 |  | 
|  | 3436 | // Always consume from window1 since it's the window that has the InputReceiver | 
|  | 3437 | consumeMotionEvent(mWindow1, action, expectedPoints); | 
|  | 3438 | } | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3439 | }; | 
|  | 3440 |  | 
|  | 3441 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { | 
|  | 3442 | // Touch Window 1 | 
|  | 3443 | PointF touchedPoint = {10, 10}; | 
|  | 3444 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3445 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3446 |  | 
|  | 3447 | // Release touch on Window 1 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3448 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3449 |  | 
|  | 3450 | // Touch Window 2 | 
|  | 3451 | touchedPoint = {150, 150}; | 
|  | 3452 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3453 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3454 | } | 
|  | 3455 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3456 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { | 
|  | 3457 | // Set scale value for window2 | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3458 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 3459 |  | 
|  | 3460 | // Touch Window 1 | 
|  | 3461 | PointF touchedPoint = {10, 10}; | 
|  | 3462 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3463 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3464 | // Release touch on Window 1 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3465 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3466 |  | 
|  | 3467 | // Touch Window 2 | 
|  | 3468 | touchedPoint = {150, 150}; | 
|  | 3469 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3470 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
|  | 3471 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3472 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3473 | // Update the transform so rotation is set | 
|  | 3474 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 3475 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
|  | 3476 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3477 | } | 
|  | 3478 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3479 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3480 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 3481 |  | 
|  | 3482 | // Touch Window 1 | 
|  | 3483 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 3484 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3485 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3486 |  | 
|  | 3487 | // Touch Window 2 | 
|  | 3488 | int32_t actionPointerDown = | 
|  | 3489 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3490 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 3491 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
|  | 3492 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3493 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3494 | // Release Window 2 | 
|  | 3495 | int32_t actionPointerUp = | 
|  | 3496 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
|  | 3497 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); | 
|  | 3498 | expectedPoints.pop_back(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3499 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3500 | // Update the transform so rotation is set for Window 2 | 
|  | 3501 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 3502 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
|  | 3503 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3504 | } | 
|  | 3505 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3506 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3507 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 3508 |  | 
|  | 3509 | // Touch Window 1 | 
|  | 3510 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 3511 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3512 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3513 |  | 
|  | 3514 | // Touch Window 2 | 
|  | 3515 | int32_t actionPointerDown = | 
|  | 3516 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3517 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 3518 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3519 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3520 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3521 |  | 
|  | 3522 | // Move both windows | 
|  | 3523 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 3524 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 3525 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 3526 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3527 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3528 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3529 | // Release Window 2 | 
|  | 3530 | int32_t actionPointerUp = | 
|  | 3531 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
|  | 3532 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); | 
|  | 3533 | expectedPoints.pop_back(); | 
|  | 3534 |  | 
|  | 3535 | // Touch Window 2 | 
|  | 3536 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 3537 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
|  | 3538 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
|  | 3539 |  | 
|  | 3540 | // Move both windows | 
|  | 3541 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 3542 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 3543 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 3544 |  | 
|  | 3545 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3546 | } | 
|  | 3547 |  | 
|  | 3548 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { | 
|  | 3549 | mWindow1->setWindowScale(0.5f, 0.5f); | 
|  | 3550 |  | 
|  | 3551 | // Touch Window 1 | 
|  | 3552 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 3553 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3554 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3555 |  | 
|  | 3556 | // Touch Window 2 | 
|  | 3557 | int32_t actionPointerDown = | 
|  | 3558 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3559 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 3560 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3561 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3562 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3563 |  | 
|  | 3564 | // Move both windows | 
|  | 3565 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 3566 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 3567 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 3568 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3569 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3570 | } | 
|  | 3571 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3572 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { | 
|  | 3573 | virtual void SetUp() override { | 
|  | 3574 | InputDispatcherTest::SetUp(); | 
|  | 3575 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3576 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3577 | mApplication->setDispatchingTimeout(20ms); | 
|  | 3578 | mWindow = | 
|  | 3579 | new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 3580 | mWindow->setFrame(Rect(0, 0, 30, 30)); | 
| Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 3581 | mWindow->setDispatchingTimeout(30ms); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3582 | mWindow->setFocusable(true); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3583 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this | 
|  | 3584 | // window. | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3585 | mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3586 |  | 
|  | 3587 | // Set focused application. | 
|  | 3588 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); | 
|  | 3589 |  | 
|  | 3590 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3591 | setFocusedWindow(mWindow); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3592 | mWindow->consumeFocusEvent(true); | 
|  | 3593 | } | 
|  | 3594 |  | 
|  | 3595 | virtual void TearDown() override { | 
|  | 3596 | InputDispatcherTest::TearDown(); | 
|  | 3597 | mWindow.clear(); | 
|  | 3598 | } | 
|  | 3599 |  | 
|  | 3600 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3601 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3602 | sp<FakeWindowHandle> mWindow; | 
|  | 3603 | static constexpr PointF WINDOW_LOCATION = {20, 20}; | 
|  | 3604 |  | 
|  | 3605 | void tapOnWindow() { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3606 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3607 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3608 | WINDOW_LOCATION)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3609 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3610 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3611 | WINDOW_LOCATION)); | 
|  | 3612 | } | 
|  | 3613 | }; | 
|  | 3614 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3615 | // Send a tap and respond, which should not cause an ANR. | 
|  | 3616 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { | 
|  | 3617 | tapOnWindow(); | 
|  | 3618 | mWindow->consumeMotionDown(); | 
|  | 3619 | mWindow->consumeMotionUp(); | 
|  | 3620 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3621 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3622 | } | 
|  | 3623 |  | 
|  | 3624 | // Send a regular key and respond, which should not cause an ANR. | 
|  | 3625 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3626 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3627 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 3628 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3629 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3630 | } | 
|  | 3631 |  | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3632 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { | 
|  | 3633 | mWindow->setFocusable(false); | 
|  | 3634 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3635 | mWindow->consumeFocusEvent(false); | 
|  | 3636 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3637 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3638 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3639 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, | 
|  | 3640 | false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3641 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3642 | // Key will not go to window because we have no focused window. | 
|  | 3643 | // The 'no focused window' ANR timer should start instead. | 
|  | 3644 |  | 
|  | 3645 | // Now, the focused application goes away. | 
|  | 3646 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); | 
|  | 3647 | // The key should get dropped and there should be no ANR. | 
|  | 3648 |  | 
|  | 3649 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3650 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3651 | } | 
|  | 3652 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3653 | // 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] | 3654 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. | 
|  | 3655 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3656 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3657 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3658 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3659 | WINDOW_LOCATION)); | 
|  | 3660 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3661 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN | 
|  | 3662 | ASSERT_TRUE(sequenceNum); | 
|  | 3663 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3664 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3665 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3666 | mWindow->finishEvent(*sequenceNum); | 
|  | 3667 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, | 
|  | 3668 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3669 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3670 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3671 | } | 
|  | 3672 |  | 
|  | 3673 | // Send a key to the app and have the app not respond right away. | 
|  | 3674 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { | 
|  | 3675 | // Inject a key, and don't respond - expect that ANR is called. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3676 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3677 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); | 
|  | 3678 | ASSERT_TRUE(sequenceNum); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3679 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3680 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3681 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3682 | } | 
|  | 3683 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3684 | // We have a focused application, but no focused window | 
|  | 3685 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3686 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3687 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3688 | mWindow->consumeFocusEvent(false); | 
|  | 3689 |  | 
|  | 3690 | // taps on the window work as normal | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3691 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3692 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3693 | WINDOW_LOCATION)); | 
|  | 3694 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); | 
|  | 3695 | mDispatcher->waitForIdle(); | 
|  | 3696 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3697 |  | 
|  | 3698 | // Once a focused event arrives, we get an ANR for this application | 
|  | 3699 | // We specify the injection timeout to be smaller than the application timeout, to ensure that | 
|  | 3700 | // injection times out (instead of failing). | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3701 | const InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3702 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3703 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3704 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3705 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3706 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3707 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3708 | } | 
|  | 3709 |  | 
|  | 3710 | // We have a focused application, but no focused window | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3711 | // Make sure that we don't notify policy twice about the same ANR. | 
|  | 3712 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3713 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3714 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3715 | mWindow->consumeFocusEvent(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3716 |  | 
|  | 3717 | // Once a focused event arrives, we get an ANR for this application | 
|  | 3718 | // We specify the injection timeout to be smaller than the application timeout, to ensure that | 
|  | 3719 | // injection times out (instead of failing). | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3720 | const InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3721 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3722 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3723 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3724 | const std::chrono::duration appTimeout = | 
|  | 3725 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3726 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3727 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3728 | std::this_thread::sleep_for(appTimeout); | 
|  | 3729 | // ANR should not be raised again. It is up to policy to do that if it desires. | 
|  | 3730 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3731 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3732 | // If we now get a focused window, the ANR should stop, but the policy handles that via | 
|  | 3733 | // '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] | 3734 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3735 | } | 
|  | 3736 |  | 
|  | 3737 | // We have a focused application, but no focused window | 
|  | 3738 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3739 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3740 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3741 | mWindow->consumeFocusEvent(false); | 
|  | 3742 |  | 
|  | 3743 | // Once a focused event arrives, we get an ANR for this application | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3744 | const InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3745 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3746 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); | 
|  | 3747 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3748 |  | 
|  | 3749 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3750 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3751 |  | 
|  | 3752 | // Future focused events get dropped right away | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3753 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3754 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3755 | mWindow->assertNoEvents(); | 
|  | 3756 | } | 
|  | 3757 |  | 
|  | 3758 | /** | 
|  | 3759 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the | 
|  | 3760 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. | 
|  | 3761 | * If we process 1 of the events, but ANR on the second event with the same timestamp, | 
|  | 3762 | * the ANR mechanism should still work. | 
|  | 3763 | * | 
|  | 3764 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the | 
|  | 3765 | * DOWN event, while not responding on the second one. | 
|  | 3766 | */ | 
|  | 3767 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { | 
|  | 3768 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 3769 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3770 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, | 
|  | 3771 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 3772 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3773 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3774 |  | 
|  | 3775 | // Now send ACTION_UP, with identical timestamp | 
|  | 3776 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3777 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, | 
|  | 3778 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 3779 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3780 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3781 |  | 
|  | 3782 | // We have now sent down and up. Let's consume first event and then ANR on the second. | 
|  | 3783 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3784 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3785 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3786 | } | 
|  | 3787 |  | 
|  | 3788 | // If an app is not responding to a key event, gesture monitors should continue to receive | 
|  | 3789 | // new motion events | 
|  | 3790 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) { | 
|  | 3791 | FakeMonitorReceiver monitor = | 
|  | 3792 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, | 
|  | 3793 | true /*isGestureMonitor*/); | 
|  | 3794 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3795 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3796 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3797 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3798 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3799 |  | 
|  | 3800 | // Stuck on the ACTION_UP | 
|  | 3801 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3802 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3803 |  | 
|  | 3804 | // New tap will go to the gesture monitor, but not to the window | 
|  | 3805 | tapOnWindow(); | 
|  | 3806 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3807 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 3808 |  | 
|  | 3809 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion | 
|  | 3810 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3811 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3812 | mWindow->assertNoEvents(); | 
|  | 3813 | monitor.assertNoEvents(); | 
|  | 3814 | } | 
|  | 3815 |  | 
|  | 3816 | // If an app is not responding to a motion event, gesture monitors should continue to receive | 
|  | 3817 | // new motion events | 
|  | 3818 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) { | 
|  | 3819 | FakeMonitorReceiver monitor = | 
|  | 3820 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, | 
|  | 3821 | true /*isGestureMonitor*/); | 
|  | 3822 |  | 
|  | 3823 | tapOnWindow(); | 
|  | 3824 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3825 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 3826 |  | 
|  | 3827 | mWindow->consumeMotionDown(); | 
|  | 3828 | // Stuck on the ACTION_UP | 
|  | 3829 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3830 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3831 |  | 
|  | 3832 | // New tap will go to the gesture monitor, but not to the window | 
|  | 3833 | tapOnWindow(); | 
|  | 3834 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3835 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 3836 |  | 
|  | 3837 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion | 
|  | 3838 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3839 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3840 | mWindow->assertNoEvents(); | 
|  | 3841 | monitor.assertNoEvents(); | 
|  | 3842 | } | 
|  | 3843 |  | 
|  | 3844 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to | 
|  | 3845 | // process events, you don't get an anr. When the window later becomes unresponsive again, you | 
|  | 3846 | // get an ANR again. | 
|  | 3847 | // 1. tap -> block on ACTION_UP -> receive ANR | 
|  | 3848 | // 2. consume all pending events (= queue becomes healthy again) | 
|  | 3849 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time | 
|  | 3850 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { | 
|  | 3851 | tapOnWindow(); | 
|  | 3852 |  | 
|  | 3853 | mWindow->consumeMotionDown(); | 
|  | 3854 | // Block on ACTION_UP | 
|  | 3855 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3856 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3857 | mWindow->consumeMotionUp(); // Now the connection should be healthy again | 
|  | 3858 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3859 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3860 | mWindow->assertNoEvents(); | 
|  | 3861 |  | 
|  | 3862 | tapOnWindow(); | 
|  | 3863 | mWindow->consumeMotionDown(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3864 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3865 | mWindow->consumeMotionUp(); | 
|  | 3866 |  | 
|  | 3867 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3868 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3869 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3870 | mWindow->assertNoEvents(); | 
|  | 3871 | } | 
|  | 3872 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3873 | // If a connection remains unresponsive for a while, make sure policy is only notified once about | 
|  | 3874 | // it. | 
|  | 3875 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3876 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3877 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3878 | WINDOW_LOCATION)); | 
|  | 3879 |  | 
|  | 3880 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3881 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3882 | std::this_thread::sleep_for(windowTimeout); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3883 | // 'notifyConnectionUnresponsive' should only be called once per connection | 
|  | 3884 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3885 | // 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] | 3886 | mWindow->consumeMotionDown(); | 
|  | 3887 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, | 
|  | 3888 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 3889 | mWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3890 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3891 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3892 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3893 | } | 
|  | 3894 |  | 
|  | 3895 | /** | 
|  | 3896 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 3897 | * not to to the focused window until the motion is processed. | 
|  | 3898 | * | 
|  | 3899 | * Warning!!! | 
|  | 3900 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT | 
|  | 3901 | * and the injection timeout that we specify when injecting the key. | 
|  | 3902 | * We must have the injection timeout (10ms) be smaller than | 
|  | 3903 | *  KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). | 
|  | 3904 | * | 
|  | 3905 | * If that value changes, this test should also change. | 
|  | 3906 | */ | 
|  | 3907 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { | 
|  | 3908 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering | 
|  | 3909 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3910 |  | 
|  | 3911 | tapOnWindow(); | 
|  | 3912 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); | 
|  | 3913 | ASSERT_TRUE(downSequenceNum); | 
|  | 3914 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); | 
|  | 3915 | ASSERT_TRUE(upSequenceNum); | 
|  | 3916 | // Don't finish the events yet, and send a key | 
|  | 3917 | // Injection will "succeed" because we will eventually give up and send the key to the focused | 
|  | 3918 | // window even if motions are still being processed. But because the injection timeout is short, | 
|  | 3919 | // we will receive INJECTION_TIMED_OUT as the result. | 
|  | 3920 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3921 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3922 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3923 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); | 
|  | 3924 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3925 | // Key will not be sent to the window, yet, because the window is still processing events | 
|  | 3926 | // and the key remains pending, waiting for the touch events to be processed | 
|  | 3927 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); | 
|  | 3928 | ASSERT_FALSE(keySequenceNum); | 
|  | 3929 |  | 
|  | 3930 | std::this_thread::sleep_for(500ms); | 
|  | 3931 | // if we wait long enough though, dispatcher will give up, and still send the key | 
|  | 3932 | // to the focused window, even though we have not yet finished the motion event | 
|  | 3933 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 3934 | mWindow->finishEvent(*downSequenceNum); | 
|  | 3935 | mWindow->finishEvent(*upSequenceNum); | 
|  | 3936 | } | 
|  | 3937 |  | 
|  | 3938 | /** | 
|  | 3939 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 3940 | * not go to the focused window until the motion is processed. | 
|  | 3941 | * If then a new motion comes in, then the pending key event should be going to the currently | 
|  | 3942 | * focused window right away. | 
|  | 3943 | */ | 
|  | 3944 | TEST_F(InputDispatcherSingleWindowAnr, | 
|  | 3945 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { | 
|  | 3946 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering | 
|  | 3947 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3948 |  | 
|  | 3949 | tapOnWindow(); | 
|  | 3950 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); | 
|  | 3951 | ASSERT_TRUE(downSequenceNum); | 
|  | 3952 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); | 
|  | 3953 | ASSERT_TRUE(upSequenceNum); | 
|  | 3954 | // Don't finish the events yet, and send a key | 
|  | 3955 | // Injection is async, so it will succeed | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3956 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3957 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3958 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3959 | // At this point, key is still pending, and should not be sent to the application yet. | 
|  | 3960 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); | 
|  | 3961 | ASSERT_FALSE(keySequenceNum); | 
|  | 3962 |  | 
|  | 3963 | // Now tap down again. It should cause the pending key to go to the focused window right away. | 
|  | 3964 | tapOnWindow(); | 
|  | 3965 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd | 
|  | 3966 | // the other events yet. We can finish events in any order. | 
|  | 3967 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN | 
|  | 3968 | mWindow->finishEvent(*upSequenceNum);   // first tap's ACTION_UP | 
|  | 3969 | mWindow->consumeMotionDown(); | 
|  | 3970 | mWindow->consumeMotionUp(); | 
|  | 3971 | mWindow->assertNoEvents(); | 
|  | 3972 | } | 
|  | 3973 |  | 
|  | 3974 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { | 
|  | 3975 | virtual void SetUp() override { | 
|  | 3976 | InputDispatcherTest::SetUp(); | 
|  | 3977 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3978 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3979 | mApplication->setDispatchingTimeout(10ms); | 
|  | 3980 | mUnfocusedWindow = | 
|  | 3981 | new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT); | 
|  | 3982 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); | 
|  | 3983 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this | 
|  | 3984 | // window. | 
|  | 3985 | // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3986 | mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 3987 | WindowInfo::Flag::WATCH_OUTSIDE_TOUCH | | 
|  | 3988 | WindowInfo::Flag::SPLIT_TOUCH); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3989 |  | 
|  | 3990 | mFocusedWindow = | 
|  | 3991 | new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 3992 | mFocusedWindow->setDispatchingTimeout(30ms); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3993 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3994 | mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3995 |  | 
|  | 3996 | // Set focused application. | 
|  | 3997 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3998 | mFocusedWindow->setFocusable(true); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3999 |  | 
|  | 4000 | // Expect one focus window exist in display. | 
|  | 4001 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4002 | setFocusedWindow(mFocusedWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4003 | mFocusedWindow->consumeFocusEvent(true); | 
|  | 4004 | } | 
|  | 4005 |  | 
|  | 4006 | virtual void TearDown() override { | 
|  | 4007 | InputDispatcherTest::TearDown(); | 
|  | 4008 |  | 
|  | 4009 | mUnfocusedWindow.clear(); | 
|  | 4010 | mFocusedWindow.clear(); | 
|  | 4011 | } | 
|  | 4012 |  | 
|  | 4013 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4014 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4015 | sp<FakeWindowHandle> mUnfocusedWindow; | 
|  | 4016 | sp<FakeWindowHandle> mFocusedWindow; | 
|  | 4017 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; | 
|  | 4018 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; | 
|  | 4019 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; | 
|  | 4020 |  | 
|  | 4021 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } | 
|  | 4022 |  | 
|  | 4023 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } | 
|  | 4024 |  | 
|  | 4025 | private: | 
|  | 4026 | void tap(const PointF& location) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4027 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4028 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4029 | location)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4030 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4031 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4032 | location)); | 
|  | 4033 | } | 
|  | 4034 | }; | 
|  | 4035 |  | 
|  | 4036 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout | 
|  | 4037 | // should be ANR'd first. | 
|  | 4038 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4039 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4040 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4041 | FOCUSED_WINDOW_LOCATION)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4042 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4043 | mFocusedWindow->consumeMotionDown(); | 
|  | 4044 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
|  | 4045 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 4046 | // We consumed all events, so no ANR | 
|  | 4047 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4048 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4049 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4050 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4051 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4052 | FOCUSED_WINDOW_LOCATION)); | 
|  | 4053 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 4054 | ASSERT_TRUE(unfocusedSequenceNum); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4055 |  | 
|  | 4056 | const std::chrono::duration timeout = | 
|  | 4057 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4058 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4059 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event | 
|  | 4060 | // sequence to make it consistent | 
|  | 4061 | mFocusedWindow->consumeMotionCancel(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4062 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4063 | mFocusedWindow->consumeMotionDown(); | 
|  | 4064 | // This cancel is generated because the connection was unresponsive | 
|  | 4065 | mFocusedWindow->consumeMotionCancel(); | 
|  | 4066 | mFocusedWindow->assertNoEvents(); | 
|  | 4067 | mUnfocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4068 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4069 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4070 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4071 | } | 
|  | 4072 |  | 
|  | 4073 | // If we have 2 windows with identical timeouts that are both unresponsive, | 
|  | 4074 | // it doesn't matter which order they should have ANR. | 
|  | 4075 | // But we should receive ANR for both. | 
|  | 4076 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { | 
|  | 4077 | // Set the timeout for unfocused window to match the focused window | 
|  | 4078 | mUnfocusedWindow->setDispatchingTimeout(10ms); | 
|  | 4079 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
|  | 4080 |  | 
|  | 4081 | tapOnFocusedWindow(); | 
|  | 4082 | // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4083 | sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms); | 
|  | 4084 | sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4085 |  | 
|  | 4086 | // 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] | 4087 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || | 
|  | 4088 | mFocusedWindow->getToken() == anrConnectionToken2); | 
|  | 4089 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || | 
|  | 4090 | mUnfocusedWindow->getToken() == anrConnectionToken2); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4091 |  | 
|  | 4092 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4093 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4094 |  | 
|  | 4095 | mFocusedWindow->consumeMotionDown(); | 
|  | 4096 | mFocusedWindow->consumeMotionUp(); | 
|  | 4097 | mUnfocusedWindow->consumeMotionOutside(); | 
|  | 4098 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4099 | sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken(); | 
|  | 4100 | sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4101 |  | 
|  | 4102 | // Both applications should be marked as responsive, in any order | 
|  | 4103 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || | 
|  | 4104 | mFocusedWindow->getToken() == responsiveToken2); | 
|  | 4105 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || | 
|  | 4106 | mUnfocusedWindow->getToken() == responsiveToken2); | 
|  | 4107 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4108 | } | 
|  | 4109 |  | 
|  | 4110 | // If a window is already not responding, the second tap on the same window should be ignored. | 
|  | 4111 | // We should also log an error to account for the dropped event (not tested here). | 
|  | 4112 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. | 
|  | 4113 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { | 
|  | 4114 | tapOnFocusedWindow(); | 
|  | 4115 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
|  | 4116 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 4117 | // Receive the events, but don't respond | 
|  | 4118 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN | 
|  | 4119 | ASSERT_TRUE(downEventSequenceNum); | 
|  | 4120 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP | 
|  | 4121 | ASSERT_TRUE(upEventSequenceNum); | 
|  | 4122 | const std::chrono::duration timeout = | 
|  | 4123 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4124 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4125 |  | 
|  | 4126 | // Tap once again | 
|  | 4127 | // 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] | 4128 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4129 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4130 | FOCUSED_WINDOW_LOCATION)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4131 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4132 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4133 | FOCUSED_WINDOW_LOCATION)); | 
|  | 4134 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a | 
|  | 4135 | // valid touch target | 
|  | 4136 | mUnfocusedWindow->assertNoEvents(); | 
|  | 4137 |  | 
|  | 4138 | // Consume the first tap | 
|  | 4139 | mFocusedWindow->finishEvent(*downEventSequenceNum); | 
|  | 4140 | mFocusedWindow->finishEvent(*upEventSequenceNum); | 
|  | 4141 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4142 | // The second tap did not go to the focused window | 
|  | 4143 | mFocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4144 | // Since all events are finished, connection should be deemed healthy again | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4145 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4146 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4147 | } | 
|  | 4148 |  | 
|  | 4149 | // If you tap outside of all windows, there will not be ANR | 
|  | 4150 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4151 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4152 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4153 | LOCATION_OUTSIDE_ALL_WINDOWS)); | 
|  | 4154 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4155 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4156 | } | 
|  | 4157 |  | 
|  | 4158 | // Since the focused window is paused, tapping on it should not produce any events | 
|  | 4159 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { | 
|  | 4160 | mFocusedWindow->setPaused(true); | 
|  | 4161 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
|  | 4162 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4163 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4164 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4165 | FOCUSED_WINDOW_LOCATION)); | 
|  | 4166 |  | 
|  | 4167 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); | 
|  | 4168 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4169 | // Should not ANR because the window is paused, and touches shouldn't go to it | 
|  | 4170 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4171 |  | 
|  | 4172 | mFocusedWindow->assertNoEvents(); | 
|  | 4173 | mUnfocusedWindow->assertNoEvents(); | 
|  | 4174 | } | 
|  | 4175 |  | 
|  | 4176 | /** | 
|  | 4177 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 4178 | * not to to the focused window until the motion is processed. | 
|  | 4179 | * If a different window becomes focused at this time, the key should go to that window instead. | 
|  | 4180 | * | 
|  | 4181 | * Warning!!! | 
|  | 4182 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT | 
|  | 4183 | * and the injection timeout that we specify when injecting the key. | 
|  | 4184 | * We must have the injection timeout (10ms) be smaller than | 
|  | 4185 | *  KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). | 
|  | 4186 | * | 
|  | 4187 | * If that value changes, this test should also change. | 
|  | 4188 | */ | 
|  | 4189 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { | 
|  | 4190 | // Set a long ANR timeout to prevent it from triggering | 
|  | 4191 | mFocusedWindow->setDispatchingTimeout(2s); | 
|  | 4192 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 4193 |  | 
|  | 4194 | tapOnUnfocusedWindow(); | 
|  | 4195 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 4196 | ASSERT_TRUE(downSequenceNum); | 
|  | 4197 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 4198 | ASSERT_TRUE(upSequenceNum); | 
|  | 4199 | // Don't finish the events yet, and send a key | 
|  | 4200 | // Injection will succeed because we will eventually give up and send the key to the focused | 
|  | 4201 | // window even if motions are still being processed. | 
|  | 4202 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4203 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4204 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4205 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); | 
|  | 4206 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4207 | // Key will not be sent to the window, yet, because the window is still processing events | 
|  | 4208 | // and the key remains pending, waiting for the touch events to be processed | 
|  | 4209 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); | 
|  | 4210 | ASSERT_FALSE(keySequenceNum); | 
|  | 4211 |  | 
|  | 4212 | // 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] | 4213 | mFocusedWindow->setFocusable(false); | 
|  | 4214 | mUnfocusedWindow->setFocusable(true); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4215 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4216 | setFocusedWindow(mUnfocusedWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4217 |  | 
|  | 4218 | // Focus events should precede the key events | 
|  | 4219 | mUnfocusedWindow->consumeFocusEvent(true); | 
|  | 4220 | mFocusedWindow->consumeFocusEvent(false); | 
|  | 4221 |  | 
|  | 4222 | // Finish the tap events, which should unblock dispatcher | 
|  | 4223 | mUnfocusedWindow->finishEvent(*downSequenceNum); | 
|  | 4224 | mUnfocusedWindow->finishEvent(*upSequenceNum); | 
|  | 4225 |  | 
|  | 4226 | // Now that all queues are cleared and no backlog in the connections, the key event | 
|  | 4227 | // can finally go to the newly focused "mUnfocusedWindow". | 
|  | 4228 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 4229 | mFocusedWindow->assertNoEvents(); | 
|  | 4230 | mUnfocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4231 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4232 | } | 
|  | 4233 |  | 
|  | 4234 | // When the touch stream is split across 2 windows, and one of them does not respond, | 
|  | 4235 | // then ANR should be raised and the touch should be canceled for the unresponsive window. | 
|  | 4236 | // The other window should not be affected by that. | 
|  | 4237 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { | 
|  | 4238 | // Touch Window 1 | 
|  | 4239 | NotifyMotionArgs motionArgs = | 
|  | 4240 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4241 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); | 
|  | 4242 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4243 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
|  | 4244 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 4245 |  | 
|  | 4246 | // Touch Window 2 | 
|  | 4247 | int32_t actionPointerDown = | 
|  | 4248 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
|  | 4249 |  | 
|  | 4250 | motionArgs = | 
|  | 4251 | generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4252 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); | 
|  | 4253 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4254 |  | 
|  | 4255 | const std::chrono::duration timeout = | 
|  | 4256 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4257 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4258 |  | 
|  | 4259 | mUnfocusedWindow->consumeMotionDown(); | 
|  | 4260 | mFocusedWindow->consumeMotionDown(); | 
|  | 4261 | // Focused window may or may not receive ACTION_MOVE | 
|  | 4262 | // But it should definitely receive ACTION_CANCEL due to the ANR | 
|  | 4263 | InputEvent* event; | 
|  | 4264 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); | 
|  | 4265 | ASSERT_TRUE(moveOrCancelSequenceNum); | 
|  | 4266 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); | 
|  | 4267 | ASSERT_NE(nullptr, event); | 
|  | 4268 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); | 
|  | 4269 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 4270 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { | 
|  | 4271 | mFocusedWindow->consumeMotionCancel(); | 
|  | 4272 | } else { | 
|  | 4273 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); | 
|  | 4274 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4275 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4276 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4277 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4278 | mUnfocusedWindow->assertNoEvents(); | 
|  | 4279 | mFocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4280 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4281 | } | 
|  | 4282 |  | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 4283 | /** | 
|  | 4284 | * If we have no focused window, and a key comes in, we start the ANR timer. | 
|  | 4285 | * The focused application should add a focused window before the timer runs out to prevent ANR. | 
|  | 4286 | * | 
|  | 4287 | * If the user touches another application during this time, the key should be dropped. | 
|  | 4288 | * Next, if a new focused window comes in, without toggling the focused application, | 
|  | 4289 | * then no ANR should occur. | 
|  | 4290 | * | 
|  | 4291 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', | 
|  | 4292 | * but in some cases the policy may not update the focused application. | 
|  | 4293 | */ | 
|  | 4294 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { | 
|  | 4295 | std::shared_ptr<FakeApplicationHandle> focusedApplication = | 
|  | 4296 | std::make_shared<FakeApplicationHandle>(); | 
|  | 4297 | focusedApplication->setDispatchingTimeout(60ms); | 
|  | 4298 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); | 
|  | 4299 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. | 
|  | 4300 | mFocusedWindow->setFocusable(false); | 
|  | 4301 |  | 
|  | 4302 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 4303 | mFocusedWindow->consumeFocusEvent(false); | 
|  | 4304 |  | 
|  | 4305 | // Send a key. The ANR timer should start because there is no focused window. | 
|  | 4306 | // 'focusedApplication' will get blamed if this timer completes. | 
|  | 4307 | // 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] | 4308 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 4309 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4310 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, | 
|  | 4311 | false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4312 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 4313 |  | 
|  | 4314 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, | 
|  | 4315 | // then the injected touches won't cause the focused event to get dropped. | 
|  | 4316 | // The dispatcher only checks for whether the queue should be pruned upon queueing. | 
|  | 4317 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would | 
|  | 4318 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. | 
|  | 4319 | // For this test, it means that the key would get delivered to the window once it becomes | 
|  | 4320 | // focused. | 
|  | 4321 | std::this_thread::sleep_for(10ms); | 
|  | 4322 |  | 
|  | 4323 | // Touch unfocused window. This should force the pending key to get dropped. | 
|  | 4324 | NotifyMotionArgs motionArgs = | 
|  | 4325 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4326 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); | 
|  | 4327 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4328 |  | 
|  | 4329 | // We do not consume the motion right away, because that would require dispatcher to first | 
|  | 4330 | // process (== drop) the key event, and by that time, ANR will be raised. | 
|  | 4331 | // Set the focused window first. | 
|  | 4332 | mFocusedWindow->setFocusable(true); | 
|  | 4333 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 4334 | setFocusedWindow(mFocusedWindow); | 
|  | 4335 | mFocusedWindow->consumeFocusEvent(true); | 
|  | 4336 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs | 
|  | 4337 | // to another application. This could be a bug / behaviour in the policy. | 
|  | 4338 |  | 
|  | 4339 | mUnfocusedWindow->consumeMotionDown(); | 
|  | 4340 |  | 
|  | 4341 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4342 | // Should not ANR because we actually have a focused window. It was just added too slowly. | 
|  | 4343 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); | 
|  | 4344 | } | 
|  | 4345 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4346 | // These tests ensure we cannot send touch events to a window that's positioned behind a window | 
|  | 4347 | // that has feature NO_INPUT_CHANNEL. | 
|  | 4348 | // Layout: | 
|  | 4349 | //   Top (closest to user) | 
|  | 4350 | //       mNoInputWindow (above all windows) | 
|  | 4351 | //       mBottomWindow | 
|  | 4352 | //   Bottom (furthest from user) | 
|  | 4353 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { | 
|  | 4354 | virtual void SetUp() override { | 
|  | 4355 | InputDispatcherTest::SetUp(); | 
|  | 4356 |  | 
|  | 4357 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
|  | 4358 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, | 
|  | 4359 | "Window without input channel", ADISPLAY_ID_DEFAULT, | 
|  | 4360 | std::make_optional<sp<IBinder>>(nullptr) /*token*/); | 
|  | 4361 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4362 | mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4363 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 4364 | // It's perfectly valid for this window to not have an associated input channel | 
|  | 4365 |  | 
|  | 4366 | mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window", | 
|  | 4367 | ADISPLAY_ID_DEFAULT); | 
|  | 4368 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 4369 |  | 
|  | 4370 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); | 
|  | 4371 | } | 
|  | 4372 |  | 
|  | 4373 | protected: | 
|  | 4374 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
|  | 4375 | sp<FakeWindowHandle> mNoInputWindow; | 
|  | 4376 | sp<FakeWindowHandle> mBottomWindow; | 
|  | 4377 | }; | 
|  | 4378 |  | 
|  | 4379 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { | 
|  | 4380 | PointF touchedPoint = {10, 10}; | 
|  | 4381 |  | 
|  | 4382 | NotifyMotionArgs motionArgs = | 
|  | 4383 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4384 | ADISPLAY_ID_DEFAULT, {touchedPoint}); | 
|  | 4385 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4386 |  | 
|  | 4387 | mNoInputWindow->assertNoEvents(); | 
|  | 4388 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have | 
|  | 4389 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, | 
|  | 4390 | // and therefore should prevent mBottomWindow from receiving touches | 
|  | 4391 | mBottomWindow->assertNoEvents(); | 
|  | 4392 | } | 
|  | 4393 |  | 
|  | 4394 | /** | 
|  | 4395 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, | 
|  | 4396 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. | 
|  | 4397 | */ | 
|  | 4398 | TEST_F(InputDispatcherMultiWindowOcclusionTests, | 
|  | 4399 | NoInputChannelFeature_DropsTouchesWithValidChannel) { | 
|  | 4400 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, | 
|  | 4401 | "Window with input channel and NO_INPUT_CHANNEL", | 
|  | 4402 | ADISPLAY_ID_DEFAULT); | 
|  | 4403 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4404 | mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4405 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 4406 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); | 
|  | 4407 |  | 
|  | 4408 | PointF touchedPoint = {10, 10}; | 
|  | 4409 |  | 
|  | 4410 | NotifyMotionArgs motionArgs = | 
|  | 4411 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4412 | ADISPLAY_ID_DEFAULT, {touchedPoint}); | 
|  | 4413 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4414 |  | 
|  | 4415 | mNoInputWindow->assertNoEvents(); | 
|  | 4416 | mBottomWindow->assertNoEvents(); | 
|  | 4417 | } | 
|  | 4418 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4419 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { | 
|  | 4420 | protected: | 
|  | 4421 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 4422 | sp<FakeWindowHandle> mWindow; | 
|  | 4423 | sp<FakeWindowHandle> mMirror; | 
|  | 4424 |  | 
|  | 4425 | virtual void SetUp() override { | 
|  | 4426 | InputDispatcherTest::SetUp(); | 
|  | 4427 | mApp = std::make_shared<FakeApplicationHandle>(); | 
|  | 4428 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 4429 | mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT, | 
|  | 4430 | mWindow->getToken()); | 
|  | 4431 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
|  | 4432 | mWindow->setFocusable(true); | 
|  | 4433 | mMirror->setFocusable(true); | 
|  | 4434 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4435 | } | 
|  | 4436 | }; | 
|  | 4437 |  | 
|  | 4438 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { | 
|  | 4439 | // Request focus on a mirrored window | 
|  | 4440 | setFocusedWindow(mMirror); | 
|  | 4441 |  | 
|  | 4442 | // window gets focused | 
|  | 4443 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4444 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4445 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4446 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 4447 | } | 
|  | 4448 |  | 
|  | 4449 | // A focused & mirrored window remains focused only if the window and its mirror are both | 
|  | 4450 | // focusable. | 
|  | 4451 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { | 
|  | 4452 | setFocusedWindow(mMirror); | 
|  | 4453 |  | 
|  | 4454 | // window gets focused | 
|  | 4455 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4456 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4457 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4458 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4459 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4460 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4461 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4462 |  | 
|  | 4463 | mMirror->setFocusable(false); | 
|  | 4464 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4465 |  | 
|  | 4466 | // window loses focus since one of the windows associated with the token in not focusable | 
|  | 4467 | mWindow->consumeFocusEvent(false); | 
|  | 4468 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4469 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 4470 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4471 | mWindow->assertNoEvents(); | 
|  | 4472 | } | 
|  | 4473 |  | 
|  | 4474 | // A focused & mirrored window remains focused until the window and its mirror both become | 
|  | 4475 | // invisible. | 
|  | 4476 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { | 
|  | 4477 | setFocusedWindow(mMirror); | 
|  | 4478 |  | 
|  | 4479 | // window gets focused | 
|  | 4480 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4481 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4482 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4483 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4484 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4485 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4486 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4487 |  | 
|  | 4488 | mMirror->setVisible(false); | 
|  | 4489 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4490 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4491 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4492 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4493 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4494 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4495 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4496 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4497 |  | 
|  | 4498 | mWindow->setVisible(false); | 
|  | 4499 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4500 |  | 
|  | 4501 | // window loses focus only after all windows associated with the token become invisible. | 
|  | 4502 | mWindow->consumeFocusEvent(false); | 
|  | 4503 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4504 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 4505 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4506 | mWindow->assertNoEvents(); | 
|  | 4507 | } | 
|  | 4508 |  | 
|  | 4509 | // A focused & mirrored window remains focused until both windows are removed. | 
|  | 4510 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { | 
|  | 4511 | setFocusedWindow(mMirror); | 
|  | 4512 |  | 
|  | 4513 | // window gets focused | 
|  | 4514 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4515 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4516 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4517 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4518 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4519 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4520 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4521 |  | 
|  | 4522 | // single window is removed but the window token remains focused | 
|  | 4523 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); | 
|  | 4524 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4525 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4526 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4527 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4528 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4529 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4530 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4531 |  | 
|  | 4532 | // Both windows are removed | 
|  | 4533 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); | 
|  | 4534 | mWindow->consumeFocusEvent(false); | 
|  | 4535 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4536 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 4537 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4538 | mWindow->assertNoEvents(); | 
|  | 4539 | } | 
|  | 4540 |  | 
|  | 4541 | // Focus request can be pending until one window becomes visible. | 
|  | 4542 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { | 
|  | 4543 | // Request focus on an invisible mirror. | 
|  | 4544 | mWindow->setVisible(false); | 
|  | 4545 | mMirror->setVisible(false); | 
|  | 4546 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4547 | setFocusedWindow(mMirror); | 
|  | 4548 |  | 
|  | 4549 | // Injected key goes to pending queue. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4550 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4551 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4552 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4553 |  | 
|  | 4554 | mMirror->setVisible(true); | 
|  | 4555 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4556 |  | 
|  | 4557 | // window gets focused | 
|  | 4558 | mWindow->consumeFocusEvent(true); | 
|  | 4559 | // window gets the pending key event | 
|  | 4560 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 4561 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4562 |  | 
|  | 4563 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { | 
|  | 4564 | protected: | 
|  | 4565 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 4566 | sp<FakeWindowHandle> mWindow; | 
|  | 4567 | sp<FakeWindowHandle> mSecondWindow; | 
|  | 4568 |  | 
|  | 4569 | void SetUp() override { | 
|  | 4570 | InputDispatcherTest::SetUp(); | 
|  | 4571 | mApp = std::make_shared<FakeApplicationHandle>(); | 
|  | 4572 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 4573 | mWindow->setFocusable(true); | 
|  | 4574 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); | 
|  | 4575 | mSecondWindow->setFocusable(true); | 
|  | 4576 |  | 
|  | 4577 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
|  | 4578 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); | 
|  | 4579 |  | 
|  | 4580 | setFocusedWindow(mWindow); | 
|  | 4581 | mWindow->consumeFocusEvent(true); | 
|  | 4582 | } | 
|  | 4583 |  | 
|  | 4584 | void notifyPointerCaptureChanged(bool enabled) { | 
|  | 4585 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled); | 
|  | 4586 | mDispatcher->notifyPointerCaptureChanged(&args); | 
|  | 4587 | } | 
|  | 4588 |  | 
|  | 4589 | void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) { | 
|  | 4590 | mDispatcher->requestPointerCapture(window->getToken(), enabled); | 
|  | 4591 | mFakePolicy->waitForSetPointerCapture(enabled); | 
|  | 4592 | notifyPointerCaptureChanged(enabled); | 
|  | 4593 | window->consumeCaptureEvent(enabled); | 
|  | 4594 | } | 
|  | 4595 | }; | 
|  | 4596 |  | 
|  | 4597 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { | 
|  | 4598 | // Ensure that capture cannot be obtained for unfocused windows. | 
|  | 4599 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); | 
|  | 4600 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 4601 | mSecondWindow->assertNoEvents(); | 
|  | 4602 |  | 
|  | 4603 | // Ensure that capture can be enabled from the focus window. | 
|  | 4604 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 4605 |  | 
|  | 4606 | // Ensure that capture cannot be disabled from a window that does not have capture. | 
|  | 4607 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); | 
|  | 4608 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 4609 |  | 
|  | 4610 | // Ensure that capture can be disabled from the window with capture. | 
|  | 4611 | requestAndVerifyPointerCapture(mWindow, false); | 
|  | 4612 | } | 
|  | 4613 |  | 
|  | 4614 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { | 
|  | 4615 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 4616 |  | 
|  | 4617 | setFocusedWindow(mSecondWindow); | 
|  | 4618 |  | 
|  | 4619 | // Ensure that the capture disabled event was sent first. | 
|  | 4620 | mWindow->consumeCaptureEvent(false); | 
|  | 4621 | mWindow->consumeFocusEvent(false); | 
|  | 4622 | mSecondWindow->consumeFocusEvent(true); | 
|  | 4623 | mFakePolicy->waitForSetPointerCapture(false); | 
|  | 4624 |  | 
|  | 4625 | // Ensure that additional state changes from InputReader are not sent to the window. | 
|  | 4626 | notifyPointerCaptureChanged(false); | 
|  | 4627 | notifyPointerCaptureChanged(true); | 
|  | 4628 | notifyPointerCaptureChanged(false); | 
|  | 4629 | mWindow->assertNoEvents(); | 
|  | 4630 | mSecondWindow->assertNoEvents(); | 
|  | 4631 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 4632 | } | 
|  | 4633 |  | 
|  | 4634 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { | 
|  | 4635 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 4636 |  | 
|  | 4637 | // InputReader unexpectedly disables and enables pointer capture. | 
|  | 4638 | notifyPointerCaptureChanged(false); | 
|  | 4639 | notifyPointerCaptureChanged(true); | 
|  | 4640 |  | 
|  | 4641 | // Ensure that Pointer Capture is disabled. | 
| Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 4642 | mFakePolicy->waitForSetPointerCapture(false); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4643 | mWindow->consumeCaptureEvent(false); | 
|  | 4644 | mWindow->assertNoEvents(); | 
|  | 4645 | } | 
|  | 4646 |  | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 4647 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { | 
|  | 4648 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 4649 |  | 
|  | 4650 | // The first window loses focus. | 
|  | 4651 | setFocusedWindow(mSecondWindow); | 
|  | 4652 | mFakePolicy->waitForSetPointerCapture(false); | 
|  | 4653 | mWindow->consumeCaptureEvent(false); | 
|  | 4654 |  | 
|  | 4655 | // Request Pointer Capture from the second window before the notification from InputReader | 
|  | 4656 | // arrives. | 
|  | 4657 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); | 
|  | 4658 | mFakePolicy->waitForSetPointerCapture(true); | 
|  | 4659 |  | 
|  | 4660 | // InputReader notifies Pointer Capture was disabled (because of the focus change). | 
|  | 4661 | notifyPointerCaptureChanged(false); | 
|  | 4662 |  | 
|  | 4663 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). | 
|  | 4664 | notifyPointerCaptureChanged(true); | 
|  | 4665 |  | 
|  | 4666 | mSecondWindow->consumeFocusEvent(true); | 
|  | 4667 | mSecondWindow->consumeCaptureEvent(true); | 
|  | 4668 | } | 
|  | 4669 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4670 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { | 
|  | 4671 | protected: | 
|  | 4672 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4673 |  | 
|  | 4674 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; | 
|  | 4675 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); | 
|  | 4676 |  | 
|  | 4677 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; | 
|  | 4678 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); | 
|  | 4679 |  | 
|  | 4680 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold | 
|  | 4681 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; | 
|  | 4682 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); | 
|  | 4683 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < | 
|  | 4684 | MAXIMUM_OBSCURING_OPACITY); | 
|  | 4685 |  | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4686 | static const int32_t TOUCHED_APP_UID = 10001; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4687 | static const int32_t APP_B_UID = 10002; | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4688 | static const int32_t APP_C_UID = 10003; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4689 |  | 
|  | 4690 | sp<FakeWindowHandle> mTouchWindow; | 
|  | 4691 |  | 
|  | 4692 | virtual void SetUp() override { | 
|  | 4693 | InputDispatcherTest::SetUp(); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4694 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4695 | mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK); | 
|  | 4696 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); | 
|  | 4697 | } | 
|  | 4698 |  | 
|  | 4699 | virtual void TearDown() override { | 
|  | 4700 | InputDispatcherTest::TearDown(); | 
|  | 4701 | mTouchWindow.clear(); | 
|  | 4702 | } | 
|  | 4703 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4704 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, | 
|  | 4705 | float alpha = 1.0f) { | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4706 | sp<FakeWindowHandle> window = getWindow(uid, name); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4707 | window->setFlags(WindowInfo::Flag::NOT_TOUCHABLE); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4708 | window->setTouchOcclusionMode(mode); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4709 | window->setAlpha(alpha); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4710 | return window; | 
|  | 4711 | } | 
|  | 4712 |  | 
|  | 4713 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { | 
|  | 4714 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); | 
|  | 4715 | sp<FakeWindowHandle> window = | 
|  | 4716 | new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); | 
|  | 4717 | // Generate an arbitrary PID based on the UID | 
|  | 4718 | window->setOwnerInfo(1777 + (uid % 10000), uid); | 
|  | 4719 | return window; | 
|  | 4720 | } | 
|  | 4721 |  | 
|  | 4722 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { | 
|  | 4723 | NotifyMotionArgs args = | 
|  | 4724 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4725 | ADISPLAY_ID_DEFAULT, points); | 
|  | 4726 | mDispatcher->notifyMotion(&args); | 
|  | 4727 | } | 
|  | 4728 | }; | 
|  | 4729 |  | 
|  | 4730 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4731 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4732 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4733 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4734 |  | 
|  | 4735 | touch(); | 
|  | 4736 |  | 
|  | 4737 | mTouchWindow->assertNoEvents(); | 
|  | 4738 | } | 
|  | 4739 |  | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 4740 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4741 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { | 
|  | 4742 | const sp<FakeWindowHandle>& w = | 
|  | 4743 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); | 
|  | 4744 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4745 |  | 
|  | 4746 | touch(); | 
|  | 4747 |  | 
|  | 4748 | mTouchWindow->assertNoEvents(); | 
|  | 4749 | } | 
|  | 4750 |  | 
|  | 4751 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 4752 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { | 
|  | 4753 | const sp<FakeWindowHandle>& w = | 
|  | 4754 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 4755 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4756 |  | 
|  | 4757 | touch(); | 
|  | 4758 |  | 
|  | 4759 | w->assertNoEvents(); | 
|  | 4760 | } | 
|  | 4761 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4762 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4763 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); | 
|  | 4764 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4765 |  | 
|  | 4766 | touch(); | 
|  | 4767 |  | 
|  | 4768 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4769 | } | 
|  | 4770 |  | 
|  | 4771 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4772 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4773 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4774 | w->setFrame(Rect(0, 0, 50, 50)); | 
|  | 4775 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4776 |  | 
|  | 4777 | touch({PointF{100, 100}}); | 
|  | 4778 |  | 
|  | 4779 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4780 | } | 
|  | 4781 |  | 
|  | 4782 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4783 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4784 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4785 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4786 |  | 
|  | 4787 | touch(); | 
|  | 4788 |  | 
|  | 4789 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4790 | } | 
|  | 4791 |  | 
|  | 4792 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { | 
|  | 4793 | const sp<FakeWindowHandle>& w = | 
|  | 4794 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
|  | 4795 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4796 |  | 
|  | 4797 | touch(); | 
|  | 4798 |  | 
|  | 4799 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4800 | } | 
|  | 4801 |  | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 4802 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { | 
|  | 4803 | const sp<FakeWindowHandle>& w = | 
|  | 4804 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
|  | 4805 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4806 |  | 
|  | 4807 | touch(); | 
|  | 4808 |  | 
|  | 4809 | w->assertNoEvents(); | 
|  | 4810 | } | 
|  | 4811 |  | 
|  | 4812 | /** | 
|  | 4813 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs | 
|  | 4814 | * inside) while letting them pass-through. Note that even though touch passes through the occluding | 
|  | 4815 | * window, the occluding window will still receive ACTION_OUTSIDE event. | 
|  | 4816 | */ | 
|  | 4817 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 4818 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { | 
|  | 4819 | const sp<FakeWindowHandle>& w = | 
|  | 4820 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4821 | w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH); | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 4822 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4823 |  | 
|  | 4824 | touch(); | 
|  | 4825 |  | 
|  | 4826 | w->consumeMotionOutside(); | 
|  | 4827 | } | 
|  | 4828 |  | 
|  | 4829 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { | 
|  | 4830 | const sp<FakeWindowHandle>& w = | 
|  | 4831 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4832 | w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH); | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 4833 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4834 |  | 
|  | 4835 | touch(); | 
|  | 4836 |  | 
|  | 4837 | InputEvent* event = w->consume(); | 
|  | 4838 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()); | 
|  | 4839 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 4840 | EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX()); | 
|  | 4841 | EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY()); | 
|  | 4842 | } | 
|  | 4843 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4844 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4845 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4846 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4847 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4848 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4849 |  | 
|  | 4850 | touch(); | 
|  | 4851 |  | 
|  | 4852 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4853 | } | 
|  | 4854 |  | 
|  | 4855 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { | 
|  | 4856 | const sp<FakeWindowHandle>& w = | 
|  | 4857 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4858 | MAXIMUM_OBSCURING_OPACITY); | 
|  | 4859 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4860 |  | 
|  | 4861 | touch(); | 
|  | 4862 |  | 
|  | 4863 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4864 | } | 
|  | 4865 |  | 
|  | 4866 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4867 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4868 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4869 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4870 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4871 |  | 
|  | 4872 | touch(); | 
|  | 4873 |  | 
|  | 4874 | mTouchWindow->assertNoEvents(); | 
|  | 4875 | } | 
|  | 4876 |  | 
|  | 4877 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { | 
|  | 4878 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 | 
|  | 4879 | const sp<FakeWindowHandle>& w1 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4880 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, | 
|  | 4881 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4882 | const sp<FakeWindowHandle>& w2 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4883 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, | 
|  | 4884 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4885 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 4886 |  | 
|  | 4887 | touch(); | 
|  | 4888 |  | 
|  | 4889 | mTouchWindow->assertNoEvents(); | 
|  | 4890 | } | 
|  | 4891 |  | 
|  | 4892 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { | 
|  | 4893 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 | 
|  | 4894 | const sp<FakeWindowHandle>& w1 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4895 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, | 
|  | 4896 | OPACITY_FAR_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4897 | const sp<FakeWindowHandle>& w2 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4898 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, | 
|  | 4899 | OPACITY_FAR_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4900 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 4901 |  | 
|  | 4902 | touch(); | 
|  | 4903 |  | 
|  | 4904 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4905 | } | 
|  | 4906 |  | 
|  | 4907 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 4908 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { | 
|  | 4909 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4910 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4911 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4912 | const sp<FakeWindowHandle>& wC = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4913 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 4914 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4915 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
|  | 4916 |  | 
|  | 4917 | touch(); | 
|  | 4918 |  | 
|  | 4919 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4920 | } | 
|  | 4921 |  | 
|  | 4922 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { | 
|  | 4923 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4924 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4925 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4926 | const sp<FakeWindowHandle>& wC = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4927 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 4928 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4929 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4930 |  | 
|  | 4931 | touch(); | 
|  | 4932 |  | 
|  | 4933 | mTouchWindow->assertNoEvents(); | 
|  | 4934 | } | 
|  | 4935 |  | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4936 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 4937 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { | 
|  | 4938 | const sp<FakeWindowHandle>& wA = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4939 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 4940 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4941 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4942 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4943 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4944 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); | 
|  | 4945 |  | 
|  | 4946 | touch(); | 
|  | 4947 |  | 
|  | 4948 | mTouchWindow->assertNoEvents(); | 
|  | 4949 | } | 
|  | 4950 |  | 
|  | 4951 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 4952 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { | 
|  | 4953 | const sp<FakeWindowHandle>& wA = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4954 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 4955 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4956 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4957 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4958 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4959 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); | 
|  | 4960 |  | 
|  | 4961 | touch(); | 
|  | 4962 |  | 
|  | 4963 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4964 | } | 
|  | 4965 |  | 
|  | 4966 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { | 
|  | 4967 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4968 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 4969 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4970 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4971 |  | 
|  | 4972 | touch(); | 
|  | 4973 |  | 
|  | 4974 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4975 | } | 
|  | 4976 |  | 
|  | 4977 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { | 
|  | 4978 | const sp<FakeWindowHandle>& w = | 
|  | 4979 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 4980 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4981 |  | 
|  | 4982 | touch(); | 
|  | 4983 |  | 
|  | 4984 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4985 | } | 
|  | 4986 |  | 
| Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 4987 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 4988 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { | 
|  | 4989 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); | 
|  | 4990 | const sp<FakeWindowHandle>& w = | 
|  | 4991 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); | 
|  | 4992 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4993 |  | 
|  | 4994 | touch(); | 
|  | 4995 |  | 
|  | 4996 | mTouchWindow->assertNoEvents(); | 
|  | 4997 | } | 
|  | 4998 |  | 
|  | 4999 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { | 
|  | 5000 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); | 
|  | 5001 | const sp<FakeWindowHandle>& w = | 
|  | 5002 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); | 
|  | 5003 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5004 |  | 
|  | 5005 | touch(); | 
|  | 5006 |  | 
|  | 5007 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5008 | } | 
|  | 5009 |  | 
|  | 5010 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5011 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { | 
|  | 5012 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); | 
|  | 5013 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5014 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 5015 | OPACITY_ABOVE_THRESHOLD); | 
|  | 5016 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5017 |  | 
|  | 5018 | touch(); | 
|  | 5019 |  | 
|  | 5020 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5021 | } | 
|  | 5022 |  | 
|  | 5023 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5024 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { | 
|  | 5025 | const sp<FakeWindowHandle>& w1 = | 
|  | 5026 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, | 
|  | 5027 | OPACITY_BELOW_THRESHOLD); | 
|  | 5028 | const sp<FakeWindowHandle>& w2 = | 
|  | 5029 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 5030 | OPACITY_BELOW_THRESHOLD); | 
|  | 5031 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 5032 |  | 
|  | 5033 | touch(); | 
|  | 5034 |  | 
|  | 5035 | mTouchWindow->assertNoEvents(); | 
|  | 5036 | } | 
|  | 5037 |  | 
|  | 5038 | /** | 
|  | 5039 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the | 
|  | 5040 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold | 
|  | 5041 | * (which alone would result in allowing touches) does not affect the blocking behavior. | 
|  | 5042 | */ | 
|  | 5043 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5044 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { | 
|  | 5045 | const sp<FakeWindowHandle>& wB = | 
|  | 5046 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, | 
|  | 5047 | OPACITY_BELOW_THRESHOLD); | 
|  | 5048 | const sp<FakeWindowHandle>& wC = | 
|  | 5049 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 5050 | OPACITY_BELOW_THRESHOLD); | 
|  | 5051 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
|  | 5052 |  | 
|  | 5053 | touch(); | 
|  | 5054 |  | 
|  | 5055 | mTouchWindow->assertNoEvents(); | 
|  | 5056 | } | 
|  | 5057 |  | 
|  | 5058 | /** | 
|  | 5059 | * This test is testing that a window from a different UID but with same application token doesn't | 
|  | 5060 | * block the touch. Apps can share the application token for close UI collaboration for example. | 
|  | 5061 | */ | 
|  | 5062 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5063 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { | 
|  | 5064 | const sp<FakeWindowHandle>& w = | 
|  | 5065 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 5066 | w->setApplicationToken(mTouchWindow->getApplicationToken()); | 
| Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 5067 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5068 |  | 
|  | 5069 | touch(); | 
|  | 5070 |  | 
|  | 5071 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5072 | } | 
|  | 5073 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5074 | class InputDispatcherDragTests : public InputDispatcherTest { | 
|  | 5075 | protected: | 
|  | 5076 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 5077 | sp<FakeWindowHandle> mWindow; | 
|  | 5078 | sp<FakeWindowHandle> mSecondWindow; | 
|  | 5079 | sp<FakeWindowHandle> mDragWindow; | 
|  | 5080 |  | 
|  | 5081 | void SetUp() override { | 
|  | 5082 | InputDispatcherTest::SetUp(); | 
|  | 5083 | mApp = std::make_shared<FakeApplicationHandle>(); | 
|  | 5084 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 5085 | mWindow->setFrame(Rect(0, 0, 100, 100)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5086 | mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5087 |  | 
|  | 5088 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); | 
|  | 5089 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5090 | mSecondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5091 |  | 
|  | 5092 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
|  | 5093 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); | 
|  | 5094 | } | 
|  | 5095 |  | 
|  | 5096 | // Start performing drag, we will create a drag window and transfer touch to it. | 
|  | 5097 | void performDrag() { | 
|  | 5098 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5099 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5100 | {50, 50})) | 
|  | 5101 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5102 |  | 
|  | 5103 | // Window should receive motion event. | 
|  | 5104 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5105 |  | 
|  | 5106 | // The drag window covers the entire display | 
|  | 5107 | mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); | 
|  | 5108 | mDispatcher->setInputWindows( | 
|  | 5109 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); | 
|  | 5110 |  | 
|  | 5111 | // Transfer touch focus to the drag window | 
|  | 5112 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), | 
|  | 5113 | true /* isDragDrop */); | 
|  | 5114 | mWindow->consumeMotionCancel(); | 
|  | 5115 | mDragWindow->consumeMotionDown(); | 
|  | 5116 | } | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5117 |  | 
|  | 5118 | // Start performing drag, we will create a drag window and transfer touch to it. | 
|  | 5119 | void performStylusDrag() { | 
|  | 5120 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5121 | injectMotionEvent(mDispatcher, | 
|  | 5122 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, | 
|  | 5123 | AINPUT_SOURCE_STYLUS) | 
|  | 5124 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) | 
|  | 5125 | .pointer(PointerBuilder(0, | 
|  | 5126 | AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 5127 | .x(50) | 
|  | 5128 | .y(50)) | 
|  | 5129 | .build())); | 
|  | 5130 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5131 |  | 
|  | 5132 | // The drag window covers the entire display | 
|  | 5133 | mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); | 
|  | 5134 | mDispatcher->setInputWindows( | 
|  | 5135 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); | 
|  | 5136 |  | 
|  | 5137 | // Transfer touch focus to the drag window | 
|  | 5138 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), | 
|  | 5139 | true /* isDragDrop */); | 
|  | 5140 | mWindow->consumeMotionCancel(); | 
|  | 5141 | mDragWindow->consumeMotionDown(); | 
|  | 5142 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5143 | }; | 
|  | 5144 |  | 
|  | 5145 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { | 
|  | 5146 | performDrag(); | 
|  | 5147 |  | 
|  | 5148 | // Move on window. | 
|  | 5149 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5150 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5151 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5152 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5153 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5154 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5155 | mSecondWindow->assertNoEvents(); | 
|  | 5156 |  | 
|  | 5157 | // Move to another window. | 
|  | 5158 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5159 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5160 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 5161 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5162 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5163 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 5164 | mSecondWindow->consumeDragEvent(false, 50, 50); | 
|  | 5165 |  | 
|  | 5166 | // Move back to original window. | 
|  | 5167 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5168 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5169 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5170 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5171 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5172 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5173 | mSecondWindow->consumeDragEvent(true, -50, 50); | 
|  | 5174 |  | 
|  | 5175 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5176 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5177 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5178 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 5179 | mWindow->assertNoEvents(); | 
|  | 5180 | mSecondWindow->assertNoEvents(); | 
|  | 5181 | } | 
|  | 5182 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5183 | TEST_F(InputDispatcherDragTests, DragAndDrop) { | 
|  | 5184 | performDrag(); | 
|  | 5185 |  | 
|  | 5186 | // Move on window. | 
|  | 5187 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5188 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5189 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5190 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5191 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5192 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5193 | mSecondWindow->assertNoEvents(); | 
|  | 5194 |  | 
|  | 5195 | // Move to another window. | 
|  | 5196 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5197 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5198 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 5199 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5200 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5201 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 5202 | mSecondWindow->consumeDragEvent(false, 50, 50); | 
|  | 5203 |  | 
|  | 5204 | // drop to another window. | 
|  | 5205 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5206 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5207 | {150, 50})) | 
|  | 5208 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5209 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 5210 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); | 
|  | 5211 | mWindow->assertNoEvents(); | 
|  | 5212 | mSecondWindow->assertNoEvents(); | 
|  | 5213 | } | 
|  | 5214 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5215 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { | 
|  | 5216 | performStylusDrag(); | 
|  | 5217 |  | 
|  | 5218 | // Move on window and keep button pressed. | 
|  | 5219 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5220 | injectMotionEvent(mDispatcher, | 
|  | 5221 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) | 
|  | 5222 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) | 
|  | 5223 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 5224 | .x(50) | 
|  | 5225 | .y(50)) | 
|  | 5226 | .build())) | 
|  | 5227 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5228 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5229 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5230 | mSecondWindow->assertNoEvents(); | 
|  | 5231 |  | 
|  | 5232 | // Move to another window and release button, expect to drop item. | 
|  | 5233 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5234 | injectMotionEvent(mDispatcher, | 
|  | 5235 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) | 
|  | 5236 | .buttonState(0) | 
|  | 5237 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 5238 | .x(150) | 
|  | 5239 | .y(50)) | 
|  | 5240 | .build())) | 
|  | 5241 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5242 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5243 | mWindow->assertNoEvents(); | 
|  | 5244 | mSecondWindow->assertNoEvents(); | 
|  | 5245 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); | 
|  | 5246 |  | 
|  | 5247 | // nothing to the window. | 
|  | 5248 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5249 | injectMotionEvent(mDispatcher, | 
|  | 5250 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) | 
|  | 5251 | .buttonState(0) | 
|  | 5252 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 5253 | .x(150) | 
|  | 5254 | .y(50)) | 
|  | 5255 | .build())) | 
|  | 5256 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5257 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 5258 | mWindow->assertNoEvents(); | 
|  | 5259 | mSecondWindow->assertNoEvents(); | 
|  | 5260 | } | 
|  | 5261 |  | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 5262 | TEST_F(InputDispatcherDragTests, DragAndDrop_InvalidWindow) { | 
|  | 5263 | performDrag(); | 
|  | 5264 |  | 
|  | 5265 | // Set second window invisible. | 
|  | 5266 | mSecondWindow->setVisible(false); | 
|  | 5267 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); | 
|  | 5268 |  | 
|  | 5269 | // Move on window. | 
|  | 5270 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5271 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5272 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5273 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5274 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5275 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5276 | mSecondWindow->assertNoEvents(); | 
|  | 5277 |  | 
|  | 5278 | // Move to another window. | 
|  | 5279 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5280 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5281 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 5282 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5283 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5284 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 5285 | mSecondWindow->assertNoEvents(); | 
|  | 5286 |  | 
|  | 5287 | // drop to another window. | 
|  | 5288 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5289 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5290 | {150, 50})) | 
|  | 5291 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5292 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 5293 | mFakePolicy->assertDropTargetEquals(nullptr); | 
|  | 5294 | mWindow->assertNoEvents(); | 
|  | 5295 | mSecondWindow->assertNoEvents(); | 
|  | 5296 | } | 
|  | 5297 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5298 | } // namespace android::inputdispatcher |