| 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. | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 49 | static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; | 
|  | 50 | static constexpr int32_t SECOND_DISPLAY_ID = 1; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 51 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 52 | // An arbitrary injector pid / uid pair that has permission to inject events. | 
|  | 53 | static const int32_t INJECTOR_PID = 999; | 
|  | 54 | static const int32_t INJECTOR_UID = 1001; | 
|  | 55 |  | 
| Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 56 | // An arbitrary pid of the gesture monitor window | 
|  | 57 | static constexpr int32_t MONITOR_PID = 2001; | 
|  | 58 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 59 | struct PointF { | 
|  | 60 | float x; | 
|  | 61 | float y; | 
|  | 62 | }; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 63 |  | 
| Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 64 | /** | 
|  | 65 | * Return a DOWN key event with KEYCODE_A. | 
|  | 66 | */ | 
|  | 67 | static KeyEvent getTestKeyEvent() { | 
|  | 68 | KeyEvent event; | 
|  | 69 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 70 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 71 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, | 
|  | 72 | ARBITRARY_TIME, ARBITRARY_TIME); | 
| Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 73 | return event; | 
|  | 74 | } | 
|  | 75 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 76 | // --- FakeInputDispatcherPolicy --- | 
|  | 77 |  | 
|  | 78 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { | 
|  | 79 | InputDispatcherConfiguration mConfig; | 
|  | 80 |  | 
|  | 81 | protected: | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 82 | virtual ~FakeInputDispatcherPolicy() {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 83 |  | 
|  | 84 | public: | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 85 | FakeInputDispatcherPolicy() {} | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 86 |  | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 87 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { | 
| Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 88 | assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action, | 
|  | 89 | args.displayId); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 92 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) { | 
| Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 93 | assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action, | 
|  | 94 | args.displayId); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 97 | void assertFilterInputEventWasNotCalled() { | 
|  | 98 | std::scoped_lock lock(mLock); | 
|  | 99 | ASSERT_EQ(nullptr, mFilteredEvent); | 
|  | 100 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 101 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 102 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 103 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 104 | ASSERT_TRUE(mConfigurationChangedTime) | 
|  | 105 | << "Timed out waiting for configuration changed call"; | 
|  | 106 | ASSERT_EQ(*mConfigurationChangedTime, when); | 
|  | 107 | mConfigurationChangedTime = std::nullopt; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 111 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 112 | ASSERT_TRUE(mLastNotifySwitch); | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 113 | // We do not check id because it is not exposed to the policy | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 114 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); | 
|  | 115 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); | 
|  | 116 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); | 
|  | 117 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); | 
|  | 118 | mLastNotifySwitch = std::nullopt; | 
|  | 119 | } | 
|  | 120 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 121 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 122 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 123 | ASSERT_EQ(touchedToken, mOnPointerDownToken); | 
|  | 124 | mOnPointerDownToken.clear(); | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | void assertOnPointerDownWasNotCalled() { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 128 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 129 | ASSERT_TRUE(mOnPointerDownToken == nullptr) | 
|  | 130 | << "Expected onPointerDownOutsideFocus to not have been called"; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 131 | } | 
|  | 132 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 133 | // This function must be called soon after the expected ANR timer starts, | 
|  | 134 | // because we are also checking how much time has passed. | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 135 | void assertNotifyNoFocusedWindowAnrWasCalled( | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 136 | std::chrono::nanoseconds timeout, | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 137 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { | 
|  | 138 | std::shared_ptr<InputApplicationHandle> application; | 
|  | 139 | { // acquire lock | 
|  | 140 | std::unique_lock lock(mLock); | 
|  | 141 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 142 | ASSERT_NO_FATAL_FAILURE( | 
|  | 143 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); | 
|  | 144 | } // release lock | 
|  | 145 | ASSERT_EQ(expectedApplication, application); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 146 | } | 
|  | 147 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 148 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, | 
|  | 149 | const sp<IBinder>& expectedConnectionToken) { | 
|  | 150 | sp<IBinder> connectionToken = getUnresponsiveWindowToken(timeout); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 151 | ASSERT_EQ(expectedConnectionToken, connectionToken); | 
|  | 152 | } | 
|  | 153 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 154 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) { | 
|  | 155 | sp<IBinder> connectionToken = getResponsiveWindowToken(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 156 | ASSERT_EQ(expectedConnectionToken, connectionToken); | 
|  | 157 | } | 
|  | 158 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 159 | void assertNotifyMonitorUnresponsiveWasCalled(std::chrono::nanoseconds timeout) { | 
|  | 160 | int32_t pid = getUnresponsiveMonitorPid(timeout); | 
|  | 161 | ASSERT_EQ(MONITOR_PID, pid); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 162 | } | 
|  | 163 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 164 | void assertNotifyMonitorResponsiveWasCalled() { | 
|  | 165 | int32_t pid = getResponsiveMonitorPid(); | 
|  | 166 | ASSERT_EQ(MONITOR_PID, pid); | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 170 | std::unique_lock lock(mLock); | 
|  | 171 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 172 | return getAnrTokenLockedInterruptible(timeout, mAnrWindowTokens, lock); | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | sp<IBinder> getResponsiveWindowToken() { | 
|  | 176 | std::unique_lock lock(mLock); | 
|  | 177 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 178 | return getAnrTokenLockedInterruptible(0s, mResponsiveWindowTokens, lock); | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | int32_t getUnresponsiveMonitorPid(std::chrono::nanoseconds timeout) { | 
|  | 182 | std::unique_lock lock(mLock); | 
|  | 183 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 184 | return getAnrTokenLockedInterruptible(timeout, mAnrMonitorPids, lock); | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | int32_t getResponsiveMonitorPid() { | 
|  | 188 | std::unique_lock lock(mLock); | 
|  | 189 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 190 | return getAnrTokenLockedInterruptible(0s, mResponsiveMonitorPids, lock); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 191 | } | 
|  | 192 |  | 
|  | 193 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait | 
|  | 194 | // for a specific container to become non-empty. When the container is non-empty, return the | 
|  | 195 | // first entry from the container and erase it. | 
|  | 196 | template <class T> | 
|  | 197 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, | 
|  | 198 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { | 
|  | 199 | const std::chrono::time_point start = std::chrono::steady_clock::now(); | 
|  | 200 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 201 |  | 
|  | 202 | // If there is an ANR, Dispatcher won't be idle because there are still events | 
|  | 203 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle | 
|  | 204 | // before checking if ANR was called. | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 205 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need | 
|  | 206 | // to provide it some time to act. 100ms seems reasonable. | 
|  | 207 | mNotifyAnr.wait_for(lock, timeToWait, | 
|  | 208 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 209 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 210 | if (storage.empty()) { | 
|  | 211 | ADD_FAILURE() << "Did not receive the ANR callback"; | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 212 | return {}; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 213 | } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 214 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because | 
|  | 215 | // the dispatcher started counting before this function was called | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 216 | if (std::chrono::abs(timeout - waited) > 100ms) { | 
|  | 217 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " | 
|  | 218 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() | 
|  | 219 | << "ms, but waited " | 
|  | 220 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() | 
|  | 221 | << "ms instead"; | 
|  | 222 | } | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 223 | T token = storage.front(); | 
|  | 224 | storage.pop(); | 
|  | 225 | return token; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 226 | } | 
|  | 227 |  | 
|  | 228 | void assertNotifyAnrWasNotCalled() { | 
|  | 229 | std::scoped_lock lock(mLock); | 
|  | 230 | ASSERT_TRUE(mAnrApplications.empty()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 231 | ASSERT_TRUE(mAnrWindowTokens.empty()); | 
|  | 232 | ASSERT_TRUE(mAnrMonitorPids.empty()); | 
|  | 233 | ASSERT_TRUE(mResponsiveWindowTokens.empty()) | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 234 | << "ANR was not called, but please also consume the 'connection is responsive' " | 
|  | 235 | "signal"; | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 236 | ASSERT_TRUE(mResponsiveMonitorPids.empty()) | 
|  | 237 | << "Monitor ANR was not called, but please also consume the 'monitor is responsive'" | 
|  | 238 | " signal"; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 239 | } | 
|  | 240 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 241 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { | 
|  | 242 | mConfig.keyRepeatTimeout = timeout; | 
|  | 243 | mConfig.keyRepeatDelay = delay; | 
|  | 244 | } | 
|  | 245 |  | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 246 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 247 | std::unique_lock lock(mLock); | 
|  | 248 | base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 249 |  | 
|  | 250 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, | 
|  | 251 | [this, enabled]() REQUIRES(mLock) { | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 252 | return mPointerCaptureRequest->enable == | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 253 | enabled; | 
|  | 254 | })) { | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 255 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled | 
|  | 256 | << ") to be called."; | 
|  | 257 | return {}; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 258 | } | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 259 | auto request = *mPointerCaptureRequest; | 
|  | 260 | mPointerCaptureRequest.reset(); | 
|  | 261 | return request; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 262 | } | 
|  | 263 |  | 
|  | 264 | void assertSetPointerCaptureNotCalled() { | 
|  | 265 | std::unique_lock lock(mLock); | 
|  | 266 | base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 267 |  | 
|  | 268 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 269 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 270 | "enabled = " | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 271 | << std::to_string(mPointerCaptureRequest->enable); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 272 | } | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 273 | mPointerCaptureRequest.reset(); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 274 | } | 
|  | 275 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 276 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { | 
|  | 277 | std::scoped_lock lock(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 278 | ASSERT_TRUE(mNotifyDropWindowWasCalled); | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 279 | ASSERT_EQ(targetToken, mDropTargetWindowToken); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 280 | mNotifyDropWindowWasCalled = false; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 281 | } | 
|  | 282 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 283 | private: | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 284 | std::mutex mLock; | 
|  | 285 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); | 
|  | 286 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); | 
|  | 287 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); | 
|  | 288 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 289 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 290 | std::condition_variable mPointerCaptureChangedCondition; | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 291 |  | 
|  | 292 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 293 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 294 | // ANR handling | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 295 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 296 | std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock); | 
|  | 297 | std::queue<sp<IBinder>> mResponsiveWindowTokens GUARDED_BY(mLock); | 
|  | 298 | std::queue<int32_t> mAnrMonitorPids GUARDED_BY(mLock); | 
|  | 299 | std::queue<int32_t> mResponsiveMonitorPids GUARDED_BY(mLock); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 300 | std::condition_variable mNotifyAnr; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 301 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 302 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 303 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 304 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 305 | void notifyConfigurationChanged(nsecs_t when) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 306 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 307 | mConfigurationChangedTime = when; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 308 | } | 
|  | 309 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 310 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, const std::string&) override { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 311 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 312 | mAnrWindowTokens.push(connectionToken); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 313 | mNotifyAnr.notify_all(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 314 | } | 
|  | 315 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 316 | void notifyMonitorUnresponsive(int32_t pid, const std::string&) override { | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 317 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 318 | mAnrMonitorPids.push(pid); | 
|  | 319 | mNotifyAnr.notify_all(); | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | void notifyWindowResponsive(const sp<IBinder>& connectionToken) override { | 
|  | 323 | std::scoped_lock lock(mLock); | 
|  | 324 | mResponsiveWindowTokens.push(connectionToken); | 
|  | 325 | mNotifyAnr.notify_all(); | 
|  | 326 | } | 
|  | 327 |  | 
|  | 328 | void notifyMonitorResponsive(int32_t pid) override { | 
|  | 329 | std::scoped_lock lock(mLock); | 
|  | 330 | mResponsiveMonitorPids.push(pid); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 331 | mNotifyAnr.notify_all(); | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 | void notifyNoFocusedWindowAnr( | 
|  | 335 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { | 
|  | 336 | std::scoped_lock lock(mLock); | 
|  | 337 | mAnrApplications.push(applicationHandle); | 
|  | 338 | mNotifyAnr.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 339 | } | 
|  | 340 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 341 | void notifyInputChannelBroken(const sp<IBinder>&) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 342 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 343 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} | 
| Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 344 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 345 | void notifyUntrustedTouch(const std::string& obscuringPackage) override {} | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 346 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, | 
|  | 347 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, | 
|  | 348 | const std::vector<float>& values) override {} | 
|  | 349 |  | 
|  | 350 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, | 
|  | 351 | InputDeviceSensorAccuracy accuracy) override {} | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 352 |  | 
| Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 353 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} | 
|  | 354 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 355 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 356 | *outConfig = mConfig; | 
|  | 357 | } | 
|  | 358 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 359 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 360 | std::scoped_lock lock(mLock); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 361 | switch (inputEvent->getType()) { | 
|  | 362 | case AINPUT_EVENT_TYPE_KEY: { | 
|  | 363 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 364 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 365 | break; | 
|  | 366 | } | 
|  | 367 |  | 
|  | 368 | case AINPUT_EVENT_TYPE_MOTION: { | 
|  | 369 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 370 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 371 | break; | 
|  | 372 | } | 
|  | 373 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 374 | return true; | 
|  | 375 | } | 
|  | 376 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 377 | void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 378 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 379 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 380 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 381 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 382 | return 0; | 
|  | 383 | } | 
|  | 384 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 385 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 386 | return false; | 
|  | 387 | } | 
|  | 388 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 389 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, | 
|  | 390 | uint32_t policyFlags) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 391 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 392 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is | 
|  | 393 | * essentially a passthrough for notifySwitch. | 
|  | 394 | */ | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 395 | mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 396 | } | 
|  | 397 |  | 
| Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 398 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 399 |  | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 400 | bool checkInjectEventsPermissionNonReentrant(int32_t pid, int32_t uid) override { | 
|  | 401 | return pid == INJECTOR_PID && uid == INJECTOR_UID; | 
|  | 402 | } | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 403 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 404 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 405 | std::scoped_lock lock(mLock); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 406 | mOnPointerDownToken = newToken; | 
|  | 407 | } | 
|  | 408 |  | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 409 | void setPointerCapture(const PointerCaptureRequest& request) override { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 410 | std::scoped_lock lock(mLock); | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 411 | mPointerCaptureRequest = {request}; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 412 | mPointerCaptureChangedCondition.notify_all(); | 
|  | 413 | } | 
|  | 414 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 415 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { | 
|  | 416 | std::scoped_lock lock(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 417 | mNotifyDropWindowWasCalled = true; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 418 | mDropTargetWindowToken = token; | 
|  | 419 | } | 
|  | 420 |  | 
| Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 421 | void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action, | 
|  | 422 | int32_t displayId) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 423 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 424 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; | 
|  | 425 | ASSERT_EQ(mFilteredEvent->getType(), type); | 
|  | 426 |  | 
|  | 427 | if (type == AINPUT_EVENT_TYPE_KEY) { | 
|  | 428 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent); | 
|  | 429 | EXPECT_EQ(keyEvent.getEventTime(), eventTime); | 
|  | 430 | EXPECT_EQ(keyEvent.getAction(), action); | 
|  | 431 | EXPECT_EQ(keyEvent.getDisplayId(), displayId); | 
|  | 432 | } else if (type == AINPUT_EVENT_TYPE_MOTION) { | 
|  | 433 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent); | 
|  | 434 | EXPECT_EQ(motionEvent.getEventTime(), eventTime); | 
|  | 435 | EXPECT_EQ(motionEvent.getAction(), action); | 
|  | 436 | EXPECT_EQ(motionEvent.getDisplayId(), displayId); | 
|  | 437 | } else { | 
|  | 438 | FAIL() << "Unknown type: " << type; | 
|  | 439 | } | 
|  | 440 |  | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 441 | mFilteredEvent = nullptr; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 442 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 443 | }; | 
|  | 444 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 445 | // --- InputDispatcherTest --- | 
|  | 446 |  | 
|  | 447 | class InputDispatcherTest : public testing::Test { | 
|  | 448 | protected: | 
|  | 449 | sp<FakeInputDispatcherPolicy> mFakePolicy; | 
|  | 450 | sp<InputDispatcher> mDispatcher; | 
|  | 451 |  | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 452 | void SetUp() override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 453 | mFakePolicy = new FakeInputDispatcherPolicy(); | 
|  | 454 | mDispatcher = new InputDispatcher(mFakePolicy); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 455 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 456 | // Start InputDispatcher thread | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 457 | ASSERT_EQ(OK, mDispatcher->start()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 458 | } | 
|  | 459 |  | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 460 | void TearDown() override { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 461 | ASSERT_EQ(OK, mDispatcher->stop()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 462 | mFakePolicy.clear(); | 
|  | 463 | mDispatcher.clear(); | 
|  | 464 | } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 465 |  | 
|  | 466 | /** | 
|  | 467 | * Used for debugging when writing the test | 
|  | 468 | */ | 
|  | 469 | void dumpDispatcherState() { | 
|  | 470 | std::string dump; | 
|  | 471 | mDispatcher->dump(dump); | 
|  | 472 | std::stringstream ss(dump); | 
|  | 473 | std::string to; | 
|  | 474 |  | 
|  | 475 | while (std::getline(ss, to, '\n')) { | 
|  | 476 | ALOGE("%s", to.c_str()); | 
|  | 477 | } | 
|  | 478 | } | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 479 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 480 | void setFocusedWindow(const sp<WindowInfoHandle>& window, | 
|  | 481 | const sp<WindowInfoHandle>& focusedWindow = nullptr) { | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 482 | FocusRequest request; | 
|  | 483 | request.token = window->getToken(); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 484 | request.windowName = window->getName(); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 485 | if (focusedWindow) { | 
|  | 486 | request.focusedToken = focusedWindow->getToken(); | 
|  | 487 | } | 
|  | 488 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 489 | request.displayId = window->getInfo()->displayId; | 
|  | 490 | mDispatcher->setFocusedWindow(request); | 
|  | 491 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 492 | }; | 
|  | 493 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 494 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { | 
|  | 495 | KeyEvent event; | 
|  | 496 |  | 
|  | 497 | // Rejects undefined key actions. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 498 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 499 | INVALID_HMAC, | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 500 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, | 
|  | 501 | ARBITRARY_TIME); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 502 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 503 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 504 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 505 | << "Should reject key events with undefined action."; | 
|  | 506 |  | 
|  | 507 | // 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] | 508 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 509 | 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] | 510 | ARBITRARY_TIME, ARBITRARY_TIME); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 511 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 512 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 513 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 514 | << "Should reject key events with ACTION_MULTIPLE."; | 
|  | 515 | } | 
|  | 516 |  | 
|  | 517 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { | 
|  | 518 | MotionEvent event; | 
|  | 519 | PointerProperties pointerProperties[MAX_POINTERS + 1]; | 
|  | 520 | PointerCoords pointerCoords[MAX_POINTERS + 1]; | 
|  | 521 | for (int i = 0; i <= MAX_POINTERS; i++) { | 
|  | 522 | pointerProperties[i].clear(); | 
|  | 523 | pointerProperties[i].id = i; | 
|  | 524 | pointerCoords[i].clear(); | 
|  | 525 | } | 
|  | 526 |  | 
| Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 527 | // Some constants commonly used below | 
|  | 528 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; | 
|  | 529 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; | 
|  | 530 | constexpr int32_t metaState = AMETA_NONE; | 
|  | 531 | constexpr MotionClassification classification = MotionClassification::NONE; | 
|  | 532 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 533 | ui::Transform identityTransform; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 534 | // Rejects undefined motion actions. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 535 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 536 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, | 
|  | 537 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 538 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0, | 
|  | 539 | INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 540 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 541 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 542 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 543 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 544 | << "Should reject motion events with undefined action."; | 
|  | 545 |  | 
|  | 546 | // Rejects pointer down with invalid index. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 547 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 548 | AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 549 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 550 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 551 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 552 | ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, | 
|  | 553 | ARBITRARY_TIME, ARBITRARY_TIME, | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 554 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 555 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 556 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 557 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 558 | << "Should reject motion events with pointer down index too large."; | 
|  | 559 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 560 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 561 | AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 562 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 563 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 564 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 565 | ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, | 
|  | 566 | ARBITRARY_TIME, ARBITRARY_TIME, | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 567 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 568 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 569 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 570 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 571 | << "Should reject motion events with pointer down index too small."; | 
|  | 572 |  | 
|  | 573 | // Rejects pointer up with invalid index. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 574 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 575 | AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 576 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 577 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 578 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 579 | ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, | 
|  | 580 | ARBITRARY_TIME, ARBITRARY_TIME, | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 581 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 582 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 583 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 584 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 585 | << "Should reject motion events with pointer up index too large."; | 
|  | 586 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 587 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 588 | AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 589 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 590 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 591 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 592 | ui::Transform::ROT_0, INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, | 
|  | 593 | ARBITRARY_TIME, ARBITRARY_TIME, | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 594 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 595 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 596 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 597 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 598 | << "Should reject motion events with pointer up index too small."; | 
|  | 599 |  | 
|  | 600 | // Rejects motion events with invalid number of pointers. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 601 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 602 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 603 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 604 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0, | 
|  | 605 | INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 606 | /*pointerCount*/ 0, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 607 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 608 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 609 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 610 | << "Should reject motion events with 0 pointers."; | 
|  | 611 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 612 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 613 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 614 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 615 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0, | 
|  | 616 | INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 617 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 618 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 619 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 620 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 621 | << "Should reject motion events with more than MAX_POINTERS pointers."; | 
|  | 622 |  | 
|  | 623 | // Rejects motion events with invalid pointer ids. | 
|  | 624 | pointerProperties[0].id = -1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 625 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 626 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 627 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 628 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0, | 
|  | 629 | INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 630 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 631 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 632 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 633 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 634 | << "Should reject motion events with pointer ids less than 0."; | 
|  | 635 |  | 
|  | 636 | pointerProperties[0].id = MAX_POINTER_ID + 1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 637 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 638 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 639 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 640 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0, | 
|  | 641 | INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 642 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 643 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 644 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 645 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 646 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; | 
|  | 647 |  | 
|  | 648 | // Rejects motion events with duplicate pointer ids. | 
|  | 649 | pointerProperties[0].id = 1; | 
|  | 650 | pointerProperties[1].id = 1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 651 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 652 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 653 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 654 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0, | 
|  | 655 | INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 656 | /*pointerCount*/ 2, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 657 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 658 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 659 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 660 | << "Should reject motion events with duplicate pointer ids."; | 
|  | 661 | } | 
|  | 662 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 663 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ | 
|  | 664 |  | 
|  | 665 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { | 
|  | 666 | constexpr nsecs_t eventTime = 20; | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 667 | NotifyConfigurationChangedArgs args(10 /*id*/, eventTime); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 668 | mDispatcher->notifyConfigurationChanged(&args); | 
|  | 669 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 670 |  | 
|  | 671 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); | 
|  | 672 | } | 
|  | 673 |  | 
|  | 674 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 675 | NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/, | 
|  | 676 | 2 /*switchMask*/); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 677 | mDispatcher->notifySwitch(&args); | 
|  | 678 |  | 
|  | 679 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener | 
|  | 680 | args.policyFlags |= POLICY_FLAG_TRUSTED; | 
|  | 681 | mFakePolicy->assertNotifySwitchWasCalled(args); | 
|  | 682 | } | 
|  | 683 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 684 | // --- InputDispatcherTest SetInputWindowTest --- | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 685 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 686 | static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 687 |  | 
|  | 688 | class FakeApplicationHandle : public InputApplicationHandle { | 
|  | 689 | public: | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 690 | FakeApplicationHandle() { | 
|  | 691 | mInfo.name = "Fake Application"; | 
|  | 692 | mInfo.token = new BBinder(); | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 693 | mInfo.dispatchingTimeoutMillis = | 
|  | 694 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 695 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 696 | virtual ~FakeApplicationHandle() {} | 
|  | 697 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 698 | virtual bool updateInfo() override { return true; } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 699 |  | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 700 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { | 
|  | 701 | mInfo.dispatchingTimeoutMillis = timeout.count(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 702 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 703 | }; | 
|  | 704 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 705 | class FakeInputReceiver { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 706 | public: | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 707 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 708 | : mName(name) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 709 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 710 | } | 
|  | 711 |  | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 712 | InputEvent* consume() { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 713 | InputEvent* event; | 
|  | 714 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); | 
|  | 715 | if (!consumeSeq) { | 
|  | 716 | return nullptr; | 
|  | 717 | } | 
|  | 718 | finishEvent(*consumeSeq); | 
|  | 719 | return event; | 
|  | 720 | } | 
|  | 721 |  | 
|  | 722 | /** | 
|  | 723 | * Receive an event without acknowledging it. | 
|  | 724 | * Return the sequence number that could later be used to send finished signal. | 
|  | 725 | */ | 
|  | 726 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 727 | uint32_t consumeSeq; | 
|  | 728 | InputEvent* event; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 729 |  | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 730 | std::chrono::time_point start = std::chrono::steady_clock::now(); | 
|  | 731 | status_t status = WOULD_BLOCK; | 
|  | 732 | while (status == WOULD_BLOCK) { | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 733 | status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 734 | &event); | 
|  | 735 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; | 
|  | 736 | if (elapsed > 100ms) { | 
|  | 737 | break; | 
|  | 738 | } | 
|  | 739 | } | 
|  | 740 |  | 
|  | 741 | if (status == WOULD_BLOCK) { | 
|  | 742 | // Just means there's no event available. | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 743 | return std::nullopt; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 744 | } | 
|  | 745 |  | 
|  | 746 | if (status != OK) { | 
|  | 747 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 748 | return std::nullopt; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 749 | } | 
|  | 750 | if (event == nullptr) { | 
|  | 751 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 752 | return std::nullopt; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 753 | } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 754 | if (outEvent != nullptr) { | 
|  | 755 | *outEvent = event; | 
|  | 756 | } | 
|  | 757 | return consumeSeq; | 
|  | 758 | } | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 759 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 760 | /** | 
|  | 761 | * To be used together with "receiveEvent" to complete the consumption of an event. | 
|  | 762 | */ | 
|  | 763 | void finishEvent(uint32_t consumeSeq) { | 
|  | 764 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); | 
|  | 765 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 766 | } | 
|  | 767 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 768 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { | 
|  | 769 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); | 
|  | 770 | ASSERT_EQ(OK, status); | 
|  | 771 | } | 
|  | 772 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 773 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, | 
|  | 774 | std::optional<int32_t> expectedDisplayId, | 
|  | 775 | std::optional<int32_t> expectedFlags) { | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 776 | InputEvent* event = consume(); | 
|  | 777 |  | 
|  | 778 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 779 | << ": consumer should have returned non-NULL event."; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 780 | ASSERT_EQ(expectedEventType, event->getType()) | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 781 | << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType) | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 782 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 783 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 784 | if (expectedDisplayId.has_value()) { | 
|  | 785 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); | 
|  | 786 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 787 |  | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 788 | switch (expectedEventType) { | 
|  | 789 | case AINPUT_EVENT_TYPE_KEY: { | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 790 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); | 
|  | 791 | EXPECT_EQ(expectedAction, keyEvent.getAction()); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 792 | if (expectedFlags.has_value()) { | 
|  | 793 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); | 
|  | 794 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 795 | break; | 
|  | 796 | } | 
|  | 797 | case AINPUT_EVENT_TYPE_MOTION: { | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 798 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); | 
|  | 799 | EXPECT_EQ(expectedAction, motionEvent.getAction()); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 800 | if (expectedFlags.has_value()) { | 
|  | 801 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); | 
|  | 802 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 803 | break; | 
|  | 804 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 805 | case AINPUT_EVENT_TYPE_FOCUS: { | 
|  | 806 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; | 
|  | 807 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 808 | case AINPUT_EVENT_TYPE_CAPTURE: { | 
|  | 809 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; | 
|  | 810 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 811 | case AINPUT_EVENT_TYPE_DRAG: { | 
|  | 812 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; | 
|  | 813 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 814 | default: { | 
|  | 815 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; | 
|  | 816 | } | 
|  | 817 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 818 | } | 
|  | 819 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 820 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { | 
|  | 821 | InputEvent* event = consume(); | 
|  | 822 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 823 | << ": consumer should have returned non-NULL event."; | 
|  | 824 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType()) | 
|  | 825 | << "Got " << inputEventTypeToString(event->getType()) | 
|  | 826 | << " event instead of FOCUS event"; | 
|  | 827 |  | 
|  | 828 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 829 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 830 |  | 
|  | 831 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); | 
|  | 832 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); | 
|  | 833 | EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode()); | 
|  | 834 | } | 
|  | 835 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 836 | void consumeCaptureEvent(bool hasCapture) { | 
|  | 837 | const InputEvent* event = consume(); | 
|  | 838 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 839 | << ": consumer should have returned non-NULL event."; | 
|  | 840 | ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType()) | 
|  | 841 | << "Got " << inputEventTypeToString(event->getType()) | 
|  | 842 | << " event instead of CAPTURE event"; | 
|  | 843 |  | 
|  | 844 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 845 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 846 |  | 
|  | 847 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); | 
|  | 848 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); | 
|  | 849 | } | 
|  | 850 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 851 | void consumeDragEvent(bool isExiting, float x, float y) { | 
|  | 852 | const InputEvent* event = consume(); | 
|  | 853 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 854 | << ": consumer should have returned non-NULL event."; | 
|  | 855 | ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType()) | 
|  | 856 | << "Got " << inputEventTypeToString(event->getType()) | 
|  | 857 | << " event instead of DRAG event"; | 
|  | 858 |  | 
|  | 859 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 860 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 861 |  | 
|  | 862 | const auto& dragEvent = static_cast<const DragEvent&>(*event); | 
|  | 863 | EXPECT_EQ(isExiting, dragEvent.isExiting()); | 
|  | 864 | EXPECT_EQ(x, dragEvent.getX()); | 
|  | 865 | EXPECT_EQ(y, dragEvent.getY()); | 
|  | 866 | } | 
|  | 867 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 868 | void assertNoEvents() { | 
|  | 869 | InputEvent* event = consume(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 870 | if (event == nullptr) { | 
|  | 871 | return; | 
|  | 872 | } | 
|  | 873 | if (event->getType() == AINPUT_EVENT_TYPE_KEY) { | 
|  | 874 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); | 
|  | 875 | ADD_FAILURE() << "Received key event " | 
|  | 876 | << KeyEvent::actionToString(keyEvent.getAction()); | 
|  | 877 | } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) { | 
|  | 878 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 879 | ADD_FAILURE() << "Received motion event " | 
|  | 880 | << MotionEvent::actionToString(motionEvent.getAction()); | 
|  | 881 | } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) { | 
|  | 882 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); | 
|  | 883 | ADD_FAILURE() << "Received focus event, hasFocus = " | 
|  | 884 | << (focusEvent.getHasFocus() ? "true" : "false"); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 885 | } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) { | 
|  | 886 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); | 
|  | 887 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " | 
|  | 888 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 889 | } | 
|  | 890 | FAIL() << mName.c_str() | 
|  | 891 | << ": should not have received any events, so consume() should return NULL"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 892 | } | 
|  | 893 |  | 
|  | 894 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } | 
|  | 895 |  | 
|  | 896 | protected: | 
|  | 897 | std::unique_ptr<InputConsumer> mConsumer; | 
|  | 898 | PreallocatedInputEventFactory mEventFactory; | 
|  | 899 |  | 
|  | 900 | std::string mName; | 
|  | 901 | }; | 
|  | 902 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 903 | class FakeWindowHandle : public WindowInfoHandle { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 904 | public: | 
|  | 905 | static const int32_t WIDTH = 600; | 
|  | 906 | static const int32_t HEIGHT = 800; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 907 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 908 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 909 | const sp<InputDispatcher>& dispatcher, const std::string name, | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 910 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 911 | : mName(name) { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 912 | if (token == std::nullopt) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 913 | base::Result<std::unique_ptr<InputChannel>> channel = | 
|  | 914 | dispatcher->createInputChannel(name); | 
|  | 915 | token = (*channel)->getConnectionToken(); | 
|  | 916 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 917 | } | 
|  | 918 |  | 
|  | 919 | inputApplicationHandle->updateInfo(); | 
|  | 920 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); | 
|  | 921 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 922 | mInfo.token = *token; | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 923 | mInfo.id = sId++; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 924 | mInfo.name = name; | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 925 | mInfo.type = WindowInfo::Type::APPLICATION; | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 926 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 927 | mInfo.alpha = 1.0; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 928 | mInfo.frameLeft = 0; | 
|  | 929 | mInfo.frameTop = 0; | 
|  | 930 | mInfo.frameRight = WIDTH; | 
|  | 931 | mInfo.frameBottom = HEIGHT; | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 932 | mInfo.transform.set(0, 0); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 933 | mInfo.globalScaleFactor = 1.0; | 
|  | 934 | mInfo.touchableRegion.clear(); | 
|  | 935 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); | 
|  | 936 | mInfo.visible = true; | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 937 | mInfo.focusable = false; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 938 | mInfo.hasWallpaper = false; | 
|  | 939 | mInfo.paused = false; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 940 | mInfo.ownerPid = INJECTOR_PID; | 
|  | 941 | mInfo.ownerUid = INJECTOR_UID; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 942 | mInfo.displayId = displayId; | 
|  | 943 | } | 
|  | 944 |  | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 945 | sp<FakeWindowHandle> clone( | 
|  | 946 | const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, | 
|  | 947 | const sp<InputDispatcher>& dispatcher, int32_t displayId) { | 
|  | 948 | sp<FakeWindowHandle> handle = | 
|  | 949 | new FakeWindowHandle(inputApplicationHandle, dispatcher, mInfo.name + "(Mirror)", | 
|  | 950 | displayId, mInfo.token); | 
|  | 951 | return handle; | 
|  | 952 | } | 
|  | 953 |  | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 954 | void setFocusable(bool focusable) { mInfo.focusable = focusable; } | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 955 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 956 | void setVisible(bool visible) { mInfo.visible = visible; } | 
|  | 957 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 958 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 959 | mInfo.dispatchingTimeout = timeout; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 960 | } | 
|  | 961 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 962 | void setPaused(bool paused) { mInfo.paused = paused; } | 
|  | 963 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 964 | void setAlpha(float alpha) { mInfo.alpha = alpha; } | 
|  | 965 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 966 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 967 |  | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 968 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } | 
|  | 969 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 970 | void setFrame(const Rect& frame) { | 
|  | 971 | mInfo.frameLeft = frame.left; | 
|  | 972 | mInfo.frameTop = frame.top; | 
|  | 973 | mInfo.frameRight = frame.right; | 
|  | 974 | mInfo.frameBottom = frame.bottom; | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 975 | mInfo.transform.set(-frame.left, -frame.top); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 976 | mInfo.touchableRegion.clear(); | 
|  | 977 | mInfo.addTouchableRegion(frame); | 
|  | 978 | } | 
|  | 979 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 980 | void addFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags |= flags; } | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 981 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 982 | void setFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags = flags; } | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 983 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 984 | void setInputFeatures(WindowInfo::Feature features) { mInfo.inputFeatures = features; } | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 985 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 986 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { | 
|  | 987 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); | 
|  | 988 | } | 
|  | 989 |  | 
|  | 990 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 991 |  | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 992 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } | 
|  | 993 |  | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 994 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 995 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, | 
|  | 996 | expectedFlags); | 
|  | 997 | } | 
|  | 998 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 999 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 1000 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); | 
|  | 1001 | } | 
|  | 1002 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1003 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1004 | int32_t expectedFlags = 0) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1005 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId, | 
|  | 1006 | expectedFlags); | 
|  | 1007 | } | 
|  | 1008 |  | 
|  | 1009 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1010 | int32_t expectedFlags = 0) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1011 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId, | 
|  | 1012 | expectedFlags); | 
|  | 1013 | } | 
|  | 1014 |  | 
|  | 1015 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1016 | int32_t expectedFlags = 0) { | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1017 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); | 
|  | 1018 | } | 
|  | 1019 |  | 
|  | 1020 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, | 
|  | 1021 | std::optional<int32_t> expectedFlags = std::nullopt) { | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1022 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, | 
|  | 1023 | expectedFlags); | 
|  | 1024 | } | 
|  | 1025 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1026 | void consumeMotionPointerDown(int32_t pointerIdx, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1027 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
|  | 1028 | int32_t expectedFlags = 0) { | 
|  | 1029 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1030 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1031 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); | 
|  | 1032 | } | 
|  | 1033 |  | 
|  | 1034 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1035 | int32_t expectedFlags = 0) { | 
|  | 1036 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1037 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1038 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); | 
|  | 1039 | } | 
|  | 1040 |  | 
|  | 1041 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1042 | int32_t expectedFlags = 0) { | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1043 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, | 
|  | 1044 | expectedFlags); | 
|  | 1045 | } | 
|  | 1046 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1047 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
|  | 1048 | int32_t expectedFlags = 0) { | 
|  | 1049 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, | 
|  | 1050 | expectedFlags); | 
|  | 1051 | } | 
|  | 1052 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1053 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { | 
|  | 1054 | ASSERT_NE(mInputReceiver, nullptr) | 
|  | 1055 | << "Cannot consume events from a window with no receiver"; | 
|  | 1056 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); | 
|  | 1057 | } | 
|  | 1058 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1059 | void consumeCaptureEvent(bool hasCapture) { | 
|  | 1060 | ASSERT_NE(mInputReceiver, nullptr) | 
|  | 1061 | << "Cannot consume events from a window with no receiver"; | 
|  | 1062 | mInputReceiver->consumeCaptureEvent(hasCapture); | 
|  | 1063 | } | 
|  | 1064 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1065 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, | 
|  | 1066 | std::optional<int32_t> expectedDisplayId, | 
|  | 1067 | std::optional<int32_t> expectedFlags) { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1068 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; | 
|  | 1069 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, | 
|  | 1070 | expectedFlags); | 
|  | 1071 | } | 
|  | 1072 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1073 | void consumeDragEvent(bool isExiting, float x, float y) { | 
|  | 1074 | mInputReceiver->consumeDragEvent(isExiting, x, y); | 
|  | 1075 | } | 
|  | 1076 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1077 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1078 | if (mInputReceiver == nullptr) { | 
|  | 1079 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; | 
|  | 1080 | return std::nullopt; | 
|  | 1081 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1082 | return mInputReceiver->receiveEvent(outEvent); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1083 | } | 
|  | 1084 |  | 
|  | 1085 | void finishEvent(uint32_t sequenceNum) { | 
|  | 1086 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; | 
|  | 1087 | mInputReceiver->finishEvent(sequenceNum); | 
|  | 1088 | } | 
|  | 1089 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1090 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { | 
|  | 1091 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; | 
|  | 1092 | mInputReceiver->sendTimeline(inputEventId, timeline); | 
|  | 1093 | } | 
|  | 1094 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1095 | InputEvent* consume() { | 
|  | 1096 | if (mInputReceiver == nullptr) { | 
|  | 1097 | return nullptr; | 
|  | 1098 | } | 
|  | 1099 | return mInputReceiver->consume(); | 
|  | 1100 | } | 
|  | 1101 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1102 | MotionEvent* consumeMotion() { | 
|  | 1103 | InputEvent* event = consume(); | 
|  | 1104 | if (event == nullptr) { | 
|  | 1105 | ADD_FAILURE() << "Consume failed : no event"; | 
|  | 1106 | return nullptr; | 
|  | 1107 | } | 
|  | 1108 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { | 
|  | 1109 | ADD_FAILURE() << "Instead of motion event, got " | 
|  | 1110 | << inputEventTypeToString(event->getType()); | 
|  | 1111 | return nullptr; | 
|  | 1112 | } | 
|  | 1113 | return static_cast<MotionEvent*>(event); | 
|  | 1114 | } | 
|  | 1115 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1116 | void assertNoEvents() { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1117 | if (mInputReceiver == nullptr && | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1118 | mInfo.inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL)) { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1119 | return; // Can't receive events if the window does not have input channel | 
|  | 1120 | } | 
|  | 1121 | ASSERT_NE(nullptr, mInputReceiver) | 
|  | 1122 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1123 | mInputReceiver->assertNoEvents(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1124 | } | 
|  | 1125 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1126 | sp<IBinder> getToken() { return mInfo.token; } | 
|  | 1127 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1128 | const std::string& getName() { return mName; } | 
|  | 1129 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1130 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { | 
|  | 1131 | mInfo.ownerPid = ownerPid; | 
|  | 1132 | mInfo.ownerUid = ownerUid; | 
|  | 1133 | } | 
|  | 1134 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1135 | private: | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1136 | const std::string mName; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1137 | std::unique_ptr<FakeInputReceiver> mInputReceiver; | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1138 | 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] | 1139 | }; | 
|  | 1140 |  | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1141 | std::atomic<int32_t> FakeWindowHandle::sId{1}; | 
|  | 1142 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1143 | static InputEventInjectionResult injectKey( | 
|  | 1144 | const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, | 
|  | 1145 | int32_t displayId = ADISPLAY_ID_NONE, | 
|  | 1146 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1147 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
|  | 1148 | bool allowKeyRepeat = true) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1149 | KeyEvent event; | 
|  | 1150 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1151 |  | 
|  | 1152 | // Define a valid key down event. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1153 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1154 | INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE, | 
|  | 1155 | repeatCount, currentTime, currentTime); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1156 |  | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1157 | int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; | 
|  | 1158 | if (!allowKeyRepeat) { | 
|  | 1159 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; | 
|  | 1160 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1161 | // Inject event until dispatch out. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1162 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1163 | injectionTimeout, policyFlags); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1164 | } | 
|  | 1165 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1166 | static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher, | 
|  | 1167 | int32_t displayId = ADISPLAY_ID_NONE) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1168 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId); | 
|  | 1169 | } | 
|  | 1170 |  | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1171 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without | 
|  | 1172 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it | 
|  | 1173 | // has to be woken up to process the repeating key. | 
|  | 1174 | static InputEventInjectionResult injectKeyDownNoRepeat(const sp<InputDispatcher>& dispatcher, | 
|  | 1175 | int32_t displayId = ADISPLAY_ID_NONE) { | 
|  | 1176 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId, | 
|  | 1177 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, | 
|  | 1178 | /* allowKeyRepeat */ false); | 
|  | 1179 | } | 
|  | 1180 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1181 | static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher, | 
|  | 1182 | int32_t displayId = ADISPLAY_ID_NONE) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1183 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId); | 
|  | 1184 | } | 
|  | 1185 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1186 | class PointerBuilder { | 
|  | 1187 | public: | 
|  | 1188 | PointerBuilder(int32_t id, int32_t toolType) { | 
|  | 1189 | mProperties.clear(); | 
|  | 1190 | mProperties.id = id; | 
|  | 1191 | mProperties.toolType = toolType; | 
|  | 1192 | mCoords.clear(); | 
|  | 1193 | } | 
|  | 1194 |  | 
|  | 1195 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } | 
|  | 1196 |  | 
|  | 1197 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } | 
|  | 1198 |  | 
|  | 1199 | PointerBuilder& axis(int32_t axis, float value) { | 
|  | 1200 | mCoords.setAxisValue(axis, value); | 
|  | 1201 | return *this; | 
|  | 1202 | } | 
|  | 1203 |  | 
|  | 1204 | PointerProperties buildProperties() const { return mProperties; } | 
|  | 1205 |  | 
|  | 1206 | PointerCoords buildCoords() const { return mCoords; } | 
|  | 1207 |  | 
|  | 1208 | private: | 
|  | 1209 | PointerProperties mProperties; | 
|  | 1210 | PointerCoords mCoords; | 
|  | 1211 | }; | 
|  | 1212 |  | 
|  | 1213 | class MotionEventBuilder { | 
|  | 1214 | public: | 
|  | 1215 | MotionEventBuilder(int32_t action, int32_t source) { | 
|  | 1216 | mAction = action; | 
|  | 1217 | mSource = source; | 
|  | 1218 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1219 | } | 
|  | 1220 |  | 
|  | 1221 | MotionEventBuilder& eventTime(nsecs_t eventTime) { | 
|  | 1222 | mEventTime = eventTime; | 
|  | 1223 | return *this; | 
|  | 1224 | } | 
|  | 1225 |  | 
|  | 1226 | MotionEventBuilder& displayId(int32_t displayId) { | 
|  | 1227 | mDisplayId = displayId; | 
|  | 1228 | return *this; | 
|  | 1229 | } | 
|  | 1230 |  | 
|  | 1231 | MotionEventBuilder& actionButton(int32_t actionButton) { | 
|  | 1232 | mActionButton = actionButton; | 
|  | 1233 | return *this; | 
|  | 1234 | } | 
|  | 1235 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1236 | MotionEventBuilder& buttonState(int32_t buttonState) { | 
|  | 1237 | mButtonState = buttonState; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1238 | return *this; | 
|  | 1239 | } | 
|  | 1240 |  | 
|  | 1241 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { | 
|  | 1242 | mRawXCursorPosition = rawXCursorPosition; | 
|  | 1243 | return *this; | 
|  | 1244 | } | 
|  | 1245 |  | 
|  | 1246 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { | 
|  | 1247 | mRawYCursorPosition = rawYCursorPosition; | 
|  | 1248 | return *this; | 
|  | 1249 | } | 
|  | 1250 |  | 
|  | 1251 | MotionEventBuilder& pointer(PointerBuilder pointer) { | 
|  | 1252 | mPointers.push_back(pointer); | 
|  | 1253 | return *this; | 
|  | 1254 | } | 
|  | 1255 |  | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1256 | MotionEventBuilder& addFlag(uint32_t flags) { | 
|  | 1257 | mFlags |= flags; | 
|  | 1258 | return *this; | 
|  | 1259 | } | 
|  | 1260 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1261 | MotionEvent build() { | 
|  | 1262 | std::vector<PointerProperties> pointerProperties; | 
|  | 1263 | std::vector<PointerCoords> pointerCoords; | 
|  | 1264 | for (const PointerBuilder& pointer : mPointers) { | 
|  | 1265 | pointerProperties.push_back(pointer.buildProperties()); | 
|  | 1266 | pointerCoords.push_back(pointer.buildCoords()); | 
|  | 1267 | } | 
|  | 1268 |  | 
|  | 1269 | // Set mouse cursor position for the most common cases to avoid boilerplate. | 
|  | 1270 | if (mSource == AINPUT_SOURCE_MOUSE && | 
|  | 1271 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && | 
|  | 1272 | mPointers.size() == 1) { | 
|  | 1273 | mRawXCursorPosition = pointerCoords[0].getX(); | 
|  | 1274 | mRawYCursorPosition = pointerCoords[0].getY(); | 
|  | 1275 | } | 
|  | 1276 |  | 
|  | 1277 | MotionEvent event; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1278 | ui::Transform identityTransform; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1279 | event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC, | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1280 | mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1281 | mButtonState, MotionClassification::NONE, identityTransform, | 
|  | 1282 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 1283 | mRawYCursorPosition, mDisplayOrientation, mDisplayWidth, mDisplayHeight, | 
|  | 1284 | mEventTime, mEventTime, mPointers.size(), pointerProperties.data(), | 
|  | 1285 | pointerCoords.data()); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1286 |  | 
|  | 1287 | return event; | 
|  | 1288 | } | 
|  | 1289 |  | 
|  | 1290 | private: | 
|  | 1291 | int32_t mAction; | 
|  | 1292 | int32_t mSource; | 
|  | 1293 | nsecs_t mEventTime; | 
|  | 1294 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; | 
|  | 1295 | int32_t mActionButton{0}; | 
|  | 1296 | int32_t mButtonState{0}; | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1297 | int32_t mFlags{0}; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1298 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; | 
|  | 1299 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 1300 | uint32_t mDisplayOrientation{ui::Transform::ROT_0}; | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1301 | int32_t mDisplayWidth{INVALID_DISPLAY_SIZE}; | 
|  | 1302 | int32_t mDisplayHeight{INVALID_DISPLAY_SIZE}; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1303 |  | 
|  | 1304 | std::vector<PointerBuilder> mPointers; | 
|  | 1305 | }; | 
|  | 1306 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1307 | static InputEventInjectionResult injectMotionEvent( | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1308 | const sp<InputDispatcher>& dispatcher, const MotionEvent& event, | 
|  | 1309 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1310 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) { | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1311 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode, | 
|  | 1312 | injectionTimeout, | 
|  | 1313 | POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); | 
|  | 1314 | } | 
|  | 1315 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1316 | static InputEventInjectionResult injectMotionEvent( | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1317 | const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId, | 
|  | 1318 | const PointF& position, | 
|  | 1319 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1320 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
|  | 1321 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1322 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1323 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) { | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1324 | MotionEvent event = MotionEventBuilder(action, source) | 
|  | 1325 | .displayId(displayId) | 
|  | 1326 | .eventTime(eventTime) | 
|  | 1327 | .rawXCursorPosition(cursorPosition.x) | 
|  | 1328 | .rawYCursorPosition(cursorPosition.y) | 
|  | 1329 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 1330 | .x(position.x) | 
|  | 1331 | .y(position.y)) | 
|  | 1332 | .build(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1333 |  | 
|  | 1334 | // Inject event until dispatch out. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1335 | return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1336 | } | 
|  | 1337 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1338 | static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher, | 
|  | 1339 | int32_t source, int32_t displayId, | 
|  | 1340 | const PointF& location = {100, 200}) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1341 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1342 | } | 
|  | 1343 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1344 | static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher, | 
|  | 1345 | int32_t source, int32_t displayId, | 
|  | 1346 | const PointF& location = {100, 200}) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1347 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1348 | } | 
|  | 1349 |  | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1350 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { | 
|  | 1351 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1352 | // Define a valid key event. | 
| Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1353 | NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, | 
|  | 1354 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, | 
|  | 1355 | KEY_A, AMETA_NONE, currentTime); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1356 |  | 
|  | 1357 | return args; | 
|  | 1358 | } | 
|  | 1359 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1360 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, | 
|  | 1361 | const std::vector<PointF>& points) { | 
|  | 1362 | size_t pointerCount = points.size(); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1363 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { | 
|  | 1364 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; | 
|  | 1365 | } | 
|  | 1366 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1367 | PointerProperties pointerProperties[pointerCount]; | 
|  | 1368 | PointerCoords pointerCoords[pointerCount]; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1369 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1370 | for (size_t i = 0; i < pointerCount; i++) { | 
|  | 1371 | pointerProperties[i].clear(); | 
|  | 1372 | pointerProperties[i].id = i; | 
|  | 1373 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1374 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1375 | pointerCoords[i].clear(); | 
|  | 1376 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); | 
|  | 1377 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); | 
|  | 1378 | } | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1379 |  | 
|  | 1380 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1381 | // Define a valid motion event. | 
| Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1382 | NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1383 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, | 
|  | 1384 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1385 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, | 
|  | 1386 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1387 | AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 1388 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1389 |  | 
|  | 1390 | return args; | 
|  | 1391 | } | 
|  | 1392 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1393 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { | 
|  | 1394 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); | 
|  | 1395 | } | 
|  | 1396 |  | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1397 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( | 
|  | 1398 | const PointerCaptureRequest& request) { | 
|  | 1399 | return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), request); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1400 | } | 
|  | 1401 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1402 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1403 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1404 | sp<FakeWindowHandle> window = | 
|  | 1405 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1406 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1407 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1408 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1409 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 1410 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1411 |  | 
|  | 1412 | // Window should receive motion event. | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1413 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1414 | } | 
|  | 1415 |  | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1416 | /** | 
|  | 1417 | * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues. | 
|  | 1418 | * To ensure that window receives only events that were directly inside of it, add | 
|  | 1419 | * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input | 
|  | 1420 | * when finding touched windows. | 
|  | 1421 | * This test serves as a sanity check for the next test, where setInputWindows is | 
|  | 1422 | * called twice. | 
|  | 1423 | */ | 
|  | 1424 | TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1425 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1426 | sp<FakeWindowHandle> window = | 
|  | 1427 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 1428 | window->setFrame(Rect(0, 0, 100, 100)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1429 | window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1430 |  | 
|  | 1431 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1432 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1433 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1434 | {50, 50})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1435 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1436 |  | 
|  | 1437 | // Window should receive motion event. | 
|  | 1438 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1439 | } | 
|  | 1440 |  | 
|  | 1441 | /** | 
|  | 1442 | * Calling setInputWindows twice, with the same info, should not cause any issues. | 
|  | 1443 | * To ensure that window receives only events that were directly inside of it, add | 
|  | 1444 | * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input | 
|  | 1445 | * when finding touched windows. | 
|  | 1446 | */ | 
|  | 1447 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1448 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1449 | sp<FakeWindowHandle> window = | 
|  | 1450 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 1451 | window->setFrame(Rect(0, 0, 100, 100)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1452 | window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1453 |  | 
|  | 1454 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 1455 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1456 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1457 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1458 | {50, 50})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1459 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1460 |  | 
|  | 1461 | // Window should receive motion event. | 
|  | 1462 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1463 | } | 
|  | 1464 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1465 | // The foreground window should receive the first touch down event. | 
|  | 1466 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1467 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1468 | sp<FakeWindowHandle> windowTop = | 
|  | 1469 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 1470 | sp<FakeWindowHandle> windowSecond = | 
|  | 1471 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1472 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1473 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1474 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1475 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 1476 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1477 |  | 
|  | 1478 | // 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] | 1479 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1480 | windowSecond->assertNoEvents(); | 
|  | 1481 | } | 
|  | 1482 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1483 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1484 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1485 | sp<FakeWindowHandle> windowLeft = | 
|  | 1486 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
|  | 1487 | windowLeft->setFrame(Rect(0, 0, 600, 800)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1488 | windowLeft->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1489 | sp<FakeWindowHandle> windowRight = | 
|  | 1490 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
|  | 1491 | windowRight->setFrame(Rect(600, 0, 1200, 800)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1492 | windowRight->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1493 |  | 
|  | 1494 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 1495 |  | 
|  | 1496 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); | 
|  | 1497 |  | 
|  | 1498 | // 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] | 1499 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1500 | injectMotionEvent(mDispatcher, | 
|  | 1501 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1502 | AINPUT_SOURCE_MOUSE) | 
|  | 1503 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1504 | .x(900) | 
|  | 1505 | .y(400)) | 
|  | 1506 | .build())); | 
|  | 1507 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1508 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1509 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1510 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1511 |  | 
|  | 1512 | // Move cursor into left window | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1513 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1514 | injectMotionEvent(mDispatcher, | 
|  | 1515 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1516 | AINPUT_SOURCE_MOUSE) | 
|  | 1517 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1518 | .x(300) | 
|  | 1519 | .y(400)) | 
|  | 1520 | .build())); | 
|  | 1521 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 1522 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1523 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1524 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1525 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1526 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1527 |  | 
|  | 1528 | // Inject a series of mouse events for a mouse click | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1529 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1530 | injectMotionEvent(mDispatcher, | 
|  | 1531 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 1532 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1533 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1534 | .x(300) | 
|  | 1535 | .y(400)) | 
|  | 1536 | .build())); | 
|  | 1537 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1538 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1539 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1540 | injectMotionEvent(mDispatcher, | 
|  | 1541 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 1542 | AINPUT_SOURCE_MOUSE) | 
|  | 1543 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1544 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1545 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1546 | .x(300) | 
|  | 1547 | .y(400)) | 
|  | 1548 | .build())); | 
|  | 1549 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 1550 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1551 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1552 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1553 | injectMotionEvent(mDispatcher, | 
|  | 1554 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 1555 | AINPUT_SOURCE_MOUSE) | 
|  | 1556 | .buttonState(0) | 
|  | 1557 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1558 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1559 | .x(300) | 
|  | 1560 | .y(400)) | 
|  | 1561 | .build())); | 
|  | 1562 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 1563 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1564 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1565 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1566 | injectMotionEvent(mDispatcher, | 
|  | 1567 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) | 
|  | 1568 | .buttonState(0) | 
|  | 1569 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1570 | .x(300) | 
|  | 1571 | .y(400)) | 
|  | 1572 | .build())); | 
|  | 1573 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 1574 |  | 
|  | 1575 | // Move mouse cursor back to right window | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1576 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1577 | injectMotionEvent(mDispatcher, | 
|  | 1578 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1579 | AINPUT_SOURCE_MOUSE) | 
|  | 1580 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1581 | .x(900) | 
|  | 1582 | .y(400)) | 
|  | 1583 | .build())); | 
|  | 1584 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 1585 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1586 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1587 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1588 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1589 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1590 | } | 
|  | 1591 |  | 
|  | 1592 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected | 
|  | 1593 | // directly in this test. | 
|  | 1594 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1595 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1596 | sp<FakeWindowHandle> window = | 
|  | 1597 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 1598 | window->setFrame(Rect(0, 0, 1200, 800)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1599 | window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1600 |  | 
|  | 1601 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 1602 |  | 
|  | 1603 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 1604 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1605 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1606 | injectMotionEvent(mDispatcher, | 
|  | 1607 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1608 | AINPUT_SOURCE_MOUSE) | 
|  | 1609 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1610 | .x(300) | 
|  | 1611 | .y(400)) | 
|  | 1612 | .build())); | 
|  | 1613 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1614 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1615 |  | 
|  | 1616 | // Inject a series of mouse events for a mouse click | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1617 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1618 | injectMotionEvent(mDispatcher, | 
|  | 1619 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 1620 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1621 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1622 | .x(300) | 
|  | 1623 | .y(400)) | 
|  | 1624 | .build())); | 
|  | 1625 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1626 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1627 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1628 | injectMotionEvent(mDispatcher, | 
|  | 1629 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 1630 | AINPUT_SOURCE_MOUSE) | 
|  | 1631 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1632 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1633 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1634 | .x(300) | 
|  | 1635 | .y(400)) | 
|  | 1636 | .build())); | 
|  | 1637 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 1638 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1639 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1640 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1641 | injectMotionEvent(mDispatcher, | 
|  | 1642 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 1643 | AINPUT_SOURCE_MOUSE) | 
|  | 1644 | .buttonState(0) | 
|  | 1645 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1646 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1647 | .x(300) | 
|  | 1648 | .y(400)) | 
|  | 1649 | .build())); | 
|  | 1650 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 1651 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1652 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1653 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1654 | injectMotionEvent(mDispatcher, | 
|  | 1655 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) | 
|  | 1656 | .buttonState(0) | 
|  | 1657 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1658 | .x(300) | 
|  | 1659 | .y(400)) | 
|  | 1660 | .build())); | 
|  | 1661 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 1662 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1663 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1664 | injectMotionEvent(mDispatcher, | 
|  | 1665 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 1666 | AINPUT_SOURCE_MOUSE) | 
|  | 1667 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1668 | .x(300) | 
|  | 1669 | .y(400)) | 
|  | 1670 | .build())); | 
|  | 1671 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 1672 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1673 | } | 
|  | 1674 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1675 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1676 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1677 |  | 
|  | 1678 | sp<FakeWindowHandle> windowLeft = | 
|  | 1679 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
|  | 1680 | windowLeft->setFrame(Rect(0, 0, 600, 800)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1681 | windowLeft->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1682 | sp<FakeWindowHandle> windowRight = | 
|  | 1683 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
|  | 1684 | windowRight->setFrame(Rect(600, 0, 1200, 800)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1685 | windowRight->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1686 |  | 
|  | 1687 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 1688 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1689 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1690 |  | 
|  | 1691 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of | 
|  | 1692 | // left window. This event should be dispatched to the left window. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1693 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1694 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1695 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1696 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1697 | windowRight->assertNoEvents(); | 
|  | 1698 | } | 
|  | 1699 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1700 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1701 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1702 | sp<FakeWindowHandle> window = | 
|  | 1703 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1704 | window->setFocusable(true); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1705 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1706 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1707 | setFocusedWindow(window); | 
|  | 1708 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1709 | window->consumeFocusEvent(true); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1710 |  | 
|  | 1711 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 1712 | mDispatcher->notifyKey(&keyArgs); | 
|  | 1713 |  | 
|  | 1714 | // Window should receive key down event. | 
|  | 1715 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 1716 |  | 
|  | 1717 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED | 
|  | 1718 | // on the app side. | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1719 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1720 | mDispatcher->notifyDeviceReset(&args); | 
|  | 1721 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, | 
|  | 1722 | AKEY_EVENT_FLAG_CANCELED); | 
|  | 1723 | } | 
|  | 1724 |  | 
|  | 1725 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1726 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1727 | sp<FakeWindowHandle> window = | 
|  | 1728 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 1729 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1730 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1731 |  | 
|  | 1732 | NotifyMotionArgs motionArgs = | 
|  | 1733 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1734 | ADISPLAY_ID_DEFAULT); | 
|  | 1735 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 1736 |  | 
|  | 1737 | // Window should receive motion down event. | 
|  | 1738 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1739 |  | 
|  | 1740 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL | 
|  | 1741 | // on the app side. | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1742 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1743 | mDispatcher->notifyDeviceReset(&args); | 
|  | 1744 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, | 
|  | 1745 | 0 /*expectedFlags*/); | 
|  | 1746 | } | 
|  | 1747 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1748 | using TransferFunction = | 
|  | 1749 | std::function<bool(sp<InputDispatcher> dispatcher, sp<IBinder>, sp<IBinder>)>; | 
|  | 1750 |  | 
|  | 1751 | class TransferTouchFixture : public InputDispatcherTest, | 
|  | 1752 | public ::testing::WithParamInterface<TransferFunction> {}; | 
|  | 1753 |  | 
|  | 1754 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1755 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1756 |  | 
|  | 1757 | // Create a couple of windows | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1758 | sp<FakeWindowHandle> firstWindow = | 
|  | 1759 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
|  | 1760 | sp<FakeWindowHandle> secondWindow = | 
|  | 1761 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1762 |  | 
|  | 1763 | // Add the windows to the dispatcher | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1764 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1765 |  | 
|  | 1766 | // Send down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1767 | NotifyMotionArgs downMotionArgs = | 
|  | 1768 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1769 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1770 | mDispatcher->notifyMotion(&downMotionArgs); | 
|  | 1771 | // Only the first window should get the down event | 
|  | 1772 | firstWindow->consumeMotionDown(); | 
|  | 1773 | secondWindow->assertNoEvents(); | 
|  | 1774 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1775 | // Transfer touch to the second window | 
|  | 1776 | TransferFunction f = GetParam(); | 
|  | 1777 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); | 
|  | 1778 | ASSERT_TRUE(success); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1779 | // The first window gets cancel and the second gets down | 
|  | 1780 | firstWindow->consumeMotionCancel(); | 
|  | 1781 | secondWindow->consumeMotionDown(); | 
|  | 1782 |  | 
|  | 1783 | // Send up event to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1784 | NotifyMotionArgs upMotionArgs = | 
|  | 1785 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1786 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1787 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 1788 | // The first  window gets no events and the second gets up | 
|  | 1789 | firstWindow->assertNoEvents(); | 
|  | 1790 | secondWindow->consumeMotionUp(); | 
|  | 1791 | } | 
|  | 1792 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1793 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1794 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1795 |  | 
|  | 1796 | PointF touchPoint = {10, 10}; | 
|  | 1797 |  | 
|  | 1798 | // Create a couple of windows | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1799 | sp<FakeWindowHandle> firstWindow = | 
|  | 1800 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
|  | 1801 | sp<FakeWindowHandle> secondWindow = | 
|  | 1802 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1803 |  | 
|  | 1804 | // Add the windows to the dispatcher | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1805 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1806 |  | 
|  | 1807 | // Send down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1808 | NotifyMotionArgs downMotionArgs = | 
|  | 1809 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1810 | ADISPLAY_ID_DEFAULT, {touchPoint}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1811 | mDispatcher->notifyMotion(&downMotionArgs); | 
|  | 1812 | // Only the first window should get the down event | 
|  | 1813 | firstWindow->consumeMotionDown(); | 
|  | 1814 | secondWindow->assertNoEvents(); | 
|  | 1815 |  | 
|  | 1816 | // Send pointer down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1817 | NotifyMotionArgs pointerDownMotionArgs = | 
|  | 1818 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1819 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1820 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1821 | {touchPoint, touchPoint}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1822 | mDispatcher->notifyMotion(&pointerDownMotionArgs); | 
|  | 1823 | // Only the first window should get the pointer down event | 
|  | 1824 | firstWindow->consumeMotionPointerDown(1); | 
|  | 1825 | secondWindow->assertNoEvents(); | 
|  | 1826 |  | 
|  | 1827 | // Transfer touch focus to the second window | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1828 | TransferFunction f = GetParam(); | 
|  | 1829 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); | 
|  | 1830 | ASSERT_TRUE(success); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1831 | // The first window gets cancel and the second gets down and pointer down | 
|  | 1832 | firstWindow->consumeMotionCancel(); | 
|  | 1833 | secondWindow->consumeMotionDown(); | 
|  | 1834 | secondWindow->consumeMotionPointerDown(1); | 
|  | 1835 |  | 
|  | 1836 | // Send pointer up to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1837 | NotifyMotionArgs pointerUpMotionArgs = | 
|  | 1838 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1839 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1840 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1841 | {touchPoint, touchPoint}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1842 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 1843 | // The first window gets nothing and the second gets pointer up | 
|  | 1844 | firstWindow->assertNoEvents(); | 
|  | 1845 | secondWindow->consumeMotionPointerUp(1); | 
|  | 1846 |  | 
|  | 1847 | // Send up event to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1848 | NotifyMotionArgs upMotionArgs = | 
|  | 1849 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1850 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1851 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 1852 | // The first window gets nothing and the second gets up | 
|  | 1853 | firstWindow->assertNoEvents(); | 
|  | 1854 | secondWindow->consumeMotionUp(); | 
|  | 1855 | } | 
|  | 1856 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1857 | // For the cases of single pointer touch and two pointers non-split touch, the api's | 
|  | 1858 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ | 
|  | 1859 | // for the case where there are multiple pointers split across several windows. | 
|  | 1860 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, | 
|  | 1861 | ::testing::Values( | 
|  | 1862 | [&](sp<InputDispatcher> dispatcher, sp<IBinder> /*ignored*/, | 
|  | 1863 | sp<IBinder> destChannelToken) { | 
|  | 1864 | return dispatcher->transferTouch(destChannelToken); | 
|  | 1865 | }, | 
|  | 1866 | [&](sp<InputDispatcher> dispatcher, sp<IBinder> from, | 
|  | 1867 | sp<IBinder> to) { | 
|  | 1868 | return dispatcher->transferTouchFocus(from, to, | 
|  | 1869 | false /*isDragAndDrop*/); | 
|  | 1870 | })); | 
|  | 1871 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1872 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1873 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1874 |  | 
|  | 1875 | // Create a non touch modal window that supports split touch | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1876 | sp<FakeWindowHandle> firstWindow = | 
|  | 1877 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1878 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1879 | firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1880 |  | 
|  | 1881 | // Create a non touch modal window that supports split touch | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1882 | sp<FakeWindowHandle> secondWindow = | 
|  | 1883 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1884 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1885 | secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1886 |  | 
|  | 1887 | // Add the windows to the dispatcher | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1888 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1889 |  | 
|  | 1890 | PointF pointInFirst = {300, 200}; | 
|  | 1891 | PointF pointInSecond = {300, 600}; | 
|  | 1892 |  | 
|  | 1893 | // Send down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1894 | NotifyMotionArgs firstDownMotionArgs = | 
|  | 1895 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1896 | ADISPLAY_ID_DEFAULT, {pointInFirst}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1897 | mDispatcher->notifyMotion(&firstDownMotionArgs); | 
|  | 1898 | // Only the first window should get the down event | 
|  | 1899 | firstWindow->consumeMotionDown(); | 
|  | 1900 | secondWindow->assertNoEvents(); | 
|  | 1901 |  | 
|  | 1902 | // Send down to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1903 | NotifyMotionArgs secondDownMotionArgs = | 
|  | 1904 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1905 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1906 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1907 | {pointInFirst, pointInSecond}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1908 | mDispatcher->notifyMotion(&secondDownMotionArgs); | 
|  | 1909 | // The first window gets a move and the second a down | 
|  | 1910 | firstWindow->consumeMotionMove(); | 
|  | 1911 | secondWindow->consumeMotionDown(); | 
|  | 1912 |  | 
|  | 1913 | // Transfer touch focus to the second window | 
|  | 1914 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); | 
|  | 1915 | // The first window gets cancel and the new gets pointer down (it already saw down) | 
|  | 1916 | firstWindow->consumeMotionCancel(); | 
|  | 1917 | secondWindow->consumeMotionPointerDown(1); | 
|  | 1918 |  | 
|  | 1919 | // Send pointer up to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1920 | NotifyMotionArgs pointerUpMotionArgs = | 
|  | 1921 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1922 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1923 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1924 | {pointInFirst, pointInSecond}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1925 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 1926 | // The first window gets nothing and the second gets pointer up | 
|  | 1927 | firstWindow->assertNoEvents(); | 
|  | 1928 | secondWindow->consumeMotionPointerUp(1); | 
|  | 1929 |  | 
|  | 1930 | // Send up event to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1931 | NotifyMotionArgs upMotionArgs = | 
|  | 1932 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1933 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1934 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 1935 | // The first window gets nothing and the second gets up | 
|  | 1936 | firstWindow->assertNoEvents(); | 
|  | 1937 | secondWindow->consumeMotionUp(); | 
|  | 1938 | } | 
|  | 1939 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1940 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. | 
|  | 1941 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving | 
|  | 1942 | // touch is not supported, so the touch should continue on those windows and the transferred-to | 
|  | 1943 | // window should get nothing. | 
|  | 1944 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { | 
|  | 1945 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 1946 |  | 
|  | 1947 | // Create a non touch modal window that supports split touch | 
|  | 1948 | sp<FakeWindowHandle> firstWindow = | 
|  | 1949 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
|  | 1950 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1951 | firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1952 |  | 
|  | 1953 | // Create a non touch modal window that supports split touch | 
|  | 1954 | sp<FakeWindowHandle> secondWindow = | 
|  | 1955 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
|  | 1956 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1957 | secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1958 |  | 
|  | 1959 | // Add the windows to the dispatcher | 
|  | 1960 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
|  | 1961 |  | 
|  | 1962 | PointF pointInFirst = {300, 200}; | 
|  | 1963 | PointF pointInSecond = {300, 600}; | 
|  | 1964 |  | 
|  | 1965 | // Send down to the first window | 
|  | 1966 | NotifyMotionArgs firstDownMotionArgs = | 
|  | 1967 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1968 | ADISPLAY_ID_DEFAULT, {pointInFirst}); | 
|  | 1969 | mDispatcher->notifyMotion(&firstDownMotionArgs); | 
|  | 1970 | // Only the first window should get the down event | 
|  | 1971 | firstWindow->consumeMotionDown(); | 
|  | 1972 | secondWindow->assertNoEvents(); | 
|  | 1973 |  | 
|  | 1974 | // Send down to the second window | 
|  | 1975 | NotifyMotionArgs secondDownMotionArgs = | 
|  | 1976 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1977 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1978 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1979 | {pointInFirst, pointInSecond}); | 
|  | 1980 | mDispatcher->notifyMotion(&secondDownMotionArgs); | 
|  | 1981 | // The first window gets a move and the second a down | 
|  | 1982 | firstWindow->consumeMotionMove(); | 
|  | 1983 | secondWindow->consumeMotionDown(); | 
|  | 1984 |  | 
|  | 1985 | // Transfer touch focus to the second window | 
|  | 1986 | const bool transferred = mDispatcher->transferTouch(secondWindow->getToken()); | 
|  | 1987 | // The 'transferTouch' call should not succeed, because there are 2 touched windows | 
|  | 1988 | ASSERT_FALSE(transferred); | 
|  | 1989 | firstWindow->assertNoEvents(); | 
|  | 1990 | secondWindow->assertNoEvents(); | 
|  | 1991 |  | 
|  | 1992 | // The rest of the dispatch should proceed as normal | 
|  | 1993 | // Send pointer up to the second window | 
|  | 1994 | NotifyMotionArgs pointerUpMotionArgs = | 
|  | 1995 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1996 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1997 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1998 | {pointInFirst, pointInSecond}); | 
|  | 1999 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 2000 | // The first window gets MOVE and the second gets pointer up | 
|  | 2001 | firstWindow->consumeMotionMove(); | 
|  | 2002 | secondWindow->consumeMotionUp(); | 
|  | 2003 |  | 
|  | 2004 | // Send up event to the first window | 
|  | 2005 | NotifyMotionArgs upMotionArgs = | 
|  | 2006 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2007 | ADISPLAY_ID_DEFAULT); | 
|  | 2008 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 2009 | // The first window gets nothing and the second gets up | 
|  | 2010 | firstWindow->consumeMotionUp(); | 
|  | 2011 | secondWindow->assertNoEvents(); | 
|  | 2012 | } | 
|  | 2013 |  | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2014 | // This case will create two windows and one mirrored window on the default display and mirror | 
|  | 2015 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put | 
|  | 2016 | // the windows info of second display before default display. | 
|  | 2017 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { | 
|  | 2018 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2019 | sp<FakeWindowHandle> firstWindowInPrimary = | 
|  | 2020 | new FakeWindowHandle(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); | 
|  | 2021 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); | 
|  | 2022 | firstWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
|  | 2023 | sp<FakeWindowHandle> secondWindowInPrimary = | 
|  | 2024 | new FakeWindowHandle(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); | 
|  | 2025 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); | 
|  | 2026 | secondWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
|  | 2027 |  | 
|  | 2028 | sp<FakeWindowHandle> mirrorWindowInPrimary = | 
|  | 2029 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); | 
|  | 2030 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); | 
|  | 2031 | mirrorWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
|  | 2032 |  | 
|  | 2033 | sp<FakeWindowHandle> firstWindowInSecondary = | 
|  | 2034 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); | 
|  | 2035 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); | 
|  | 2036 | firstWindowInSecondary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
|  | 2037 |  | 
|  | 2038 | sp<FakeWindowHandle> secondWindowInSecondary = | 
|  | 2039 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); | 
|  | 2040 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); | 
|  | 2041 | secondWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
|  | 2042 |  | 
|  | 2043 | // Update window info, let it find window handle of second display first. | 
|  | 2044 | mDispatcher->setInputWindows( | 
|  | 2045 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, | 
|  | 2046 | {ADISPLAY_ID_DEFAULT, | 
|  | 2047 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); | 
|  | 2048 |  | 
|  | 2049 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2050 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 2051 | {50, 50})) | 
|  | 2052 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 2053 |  | 
|  | 2054 | // Window should receive motion event. | 
|  | 2055 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2056 |  | 
|  | 2057 | // Transfer touch focus | 
|  | 2058 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), | 
|  | 2059 | secondWindowInPrimary->getToken())); | 
|  | 2060 | // The first window gets cancel. | 
|  | 2061 | firstWindowInPrimary->consumeMotionCancel(); | 
|  | 2062 | secondWindowInPrimary->consumeMotionDown(); | 
|  | 2063 |  | 
|  | 2064 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2065 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2066 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 2067 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 2068 | firstWindowInPrimary->assertNoEvents(); | 
|  | 2069 | secondWindowInPrimary->consumeMotionMove(); | 
|  | 2070 |  | 
|  | 2071 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2072 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 2073 | {150, 50})) | 
|  | 2074 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 2075 | firstWindowInPrimary->assertNoEvents(); | 
|  | 2076 | secondWindowInPrimary->consumeMotionUp(); | 
|  | 2077 | } | 
|  | 2078 |  | 
|  | 2079 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use | 
|  | 2080 | // 'transferTouch' api. | 
|  | 2081 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { | 
|  | 2082 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2083 | sp<FakeWindowHandle> firstWindowInPrimary = | 
|  | 2084 | new FakeWindowHandle(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); | 
|  | 2085 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); | 
|  | 2086 | firstWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
|  | 2087 | sp<FakeWindowHandle> secondWindowInPrimary = | 
|  | 2088 | new FakeWindowHandle(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); | 
|  | 2089 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); | 
|  | 2090 | secondWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
|  | 2091 |  | 
|  | 2092 | sp<FakeWindowHandle> mirrorWindowInPrimary = | 
|  | 2093 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); | 
|  | 2094 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); | 
|  | 2095 | mirrorWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
|  | 2096 |  | 
|  | 2097 | sp<FakeWindowHandle> firstWindowInSecondary = | 
|  | 2098 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); | 
|  | 2099 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); | 
|  | 2100 | firstWindowInSecondary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
|  | 2101 |  | 
|  | 2102 | sp<FakeWindowHandle> secondWindowInSecondary = | 
|  | 2103 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); | 
|  | 2104 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); | 
|  | 2105 | secondWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
|  | 2106 |  | 
|  | 2107 | // Update window info, let it find window handle of second display first. | 
|  | 2108 | mDispatcher->setInputWindows( | 
|  | 2109 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, | 
|  | 2110 | {ADISPLAY_ID_DEFAULT, | 
|  | 2111 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); | 
|  | 2112 |  | 
|  | 2113 | // Touch on second display. | 
|  | 2114 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2115 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) | 
|  | 2116 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 2117 |  | 
|  | 2118 | // Window should receive motion event. | 
|  | 2119 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); | 
|  | 2120 |  | 
|  | 2121 | // Transfer touch focus | 
|  | 2122 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken())); | 
|  | 2123 |  | 
|  | 2124 | // The first window gets cancel. | 
|  | 2125 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); | 
|  | 2126 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); | 
|  | 2127 |  | 
|  | 2128 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2129 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2130 | SECOND_DISPLAY_ID, {150, 50})) | 
|  | 2131 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 2132 | firstWindowInPrimary->assertNoEvents(); | 
|  | 2133 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); | 
|  | 2134 |  | 
|  | 2135 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2136 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) | 
|  | 2137 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 2138 | firstWindowInPrimary->assertNoEvents(); | 
|  | 2139 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); | 
|  | 2140 | } | 
|  | 2141 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2142 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2143 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2144 | sp<FakeWindowHandle> window = | 
|  | 2145 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2146 |  | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2147 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2148 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2149 | setFocusedWindow(window); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2150 |  | 
|  | 2151 | window->consumeFocusEvent(true); | 
|  | 2152 |  | 
|  | 2153 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 2154 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2155 |  | 
|  | 2156 | // Window should receive key down event. | 
|  | 2157 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 2158 | } | 
|  | 2159 |  | 
|  | 2160 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2161 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2162 | sp<FakeWindowHandle> window = | 
|  | 2163 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2164 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2165 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2166 |  | 
|  | 2167 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 2168 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2169 | mDispatcher->waitForIdle(); | 
|  | 2170 |  | 
|  | 2171 | window->assertNoEvents(); | 
|  | 2172 | } | 
|  | 2173 |  | 
|  | 2174 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys | 
|  | 2175 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2176 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2177 | sp<FakeWindowHandle> window = | 
|  | 2178 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2179 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2180 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2181 |  | 
|  | 2182 | // Send key | 
|  | 2183 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 2184 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2185 | // Send motion | 
|  | 2186 | NotifyMotionArgs motionArgs = | 
|  | 2187 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2188 | ADISPLAY_ID_DEFAULT); | 
|  | 2189 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2190 |  | 
|  | 2191 | // Window should receive only the motion event | 
|  | 2192 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2193 | window->assertNoEvents(); // Key event or focus event will not be received | 
|  | 2194 | } | 
|  | 2195 |  | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2196 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { | 
|  | 2197 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2198 |  | 
|  | 2199 | // Create first non touch modal window that supports split touch | 
|  | 2200 | sp<FakeWindowHandle> firstWindow = | 
|  | 2201 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
|  | 2202 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2203 | firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2204 |  | 
|  | 2205 | // Create second non touch modal window that supports split touch | 
|  | 2206 | sp<FakeWindowHandle> secondWindow = | 
|  | 2207 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
|  | 2208 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2209 | secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2210 |  | 
|  | 2211 | // Add the windows to the dispatcher | 
|  | 2212 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
|  | 2213 |  | 
|  | 2214 | PointF pointInFirst = {300, 200}; | 
|  | 2215 | PointF pointInSecond = {300, 600}; | 
|  | 2216 |  | 
|  | 2217 | // Send down to the first window | 
|  | 2218 | NotifyMotionArgs firstDownMotionArgs = | 
|  | 2219 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2220 | ADISPLAY_ID_DEFAULT, {pointInFirst}); | 
|  | 2221 | mDispatcher->notifyMotion(&firstDownMotionArgs); | 
|  | 2222 | // Only the first window should get the down event | 
|  | 2223 | firstWindow->consumeMotionDown(); | 
|  | 2224 | secondWindow->assertNoEvents(); | 
|  | 2225 |  | 
|  | 2226 | // Send down to the second window | 
|  | 2227 | NotifyMotionArgs secondDownMotionArgs = | 
|  | 2228 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 2229 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 2230 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 2231 | {pointInFirst, pointInSecond}); | 
|  | 2232 | mDispatcher->notifyMotion(&secondDownMotionArgs); | 
|  | 2233 | // The first window gets a move and the second a down | 
|  | 2234 | firstWindow->consumeMotionMove(); | 
|  | 2235 | secondWindow->consumeMotionDown(); | 
|  | 2236 |  | 
|  | 2237 | // Send pointer cancel to the second window | 
|  | 2238 | NotifyMotionArgs pointerUpMotionArgs = | 
|  | 2239 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 2240 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 2241 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 2242 | {pointInFirst, pointInSecond}); | 
|  | 2243 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; | 
|  | 2244 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 2245 | // The first window gets move and the second gets cancel. | 
|  | 2246 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); | 
|  | 2247 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); | 
|  | 2248 |  | 
|  | 2249 | // Send up event. | 
|  | 2250 | NotifyMotionArgs upMotionArgs = | 
|  | 2251 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2252 | ADISPLAY_ID_DEFAULT); | 
|  | 2253 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 2254 | // The first window gets up and the second gets nothing. | 
|  | 2255 | firstWindow->consumeMotionUp(); | 
|  | 2256 | secondWindow->assertNoEvents(); | 
|  | 2257 | } | 
|  | 2258 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 2259 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { | 
|  | 2260 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2261 |  | 
|  | 2262 | sp<FakeWindowHandle> window = | 
|  | 2263 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 2264 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2265 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; | 
|  | 2266 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; | 
|  | 2267 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; | 
|  | 2268 |  | 
|  | 2269 | window->sendTimeline(1 /*inputEventId*/, graphicsTimeline); | 
|  | 2270 | window->assertNoEvents(); | 
|  | 2271 | mDispatcher->waitForIdle(); | 
|  | 2272 | } | 
|  | 2273 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2274 | class FakeMonitorReceiver { | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2275 | public: | 
|  | 2276 | FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name, | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2277 | int32_t displayId, bool isGestureMonitor = false) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 2278 | base::Result<std::unique_ptr<InputChannel>> channel = | 
| Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 2279 | dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 2280 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2281 | } | 
|  | 2282 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2283 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } | 
|  | 2284 |  | 
|  | 2285 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 2286 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, | 
|  | 2287 | expectedDisplayId, expectedFlags); | 
|  | 2288 | } | 
|  | 2289 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2290 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } | 
|  | 2291 |  | 
|  | 2292 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } | 
|  | 2293 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2294 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 2295 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, | 
|  | 2296 | expectedDisplayId, expectedFlags); | 
|  | 2297 | } | 
|  | 2298 |  | 
|  | 2299 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 2300 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, | 
|  | 2301 | expectedDisplayId, expectedFlags); | 
|  | 2302 | } | 
|  | 2303 |  | 
| Arthur Hung | 7162547 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 2304 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 2305 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, | 
|  | 2306 | expectedDisplayId, expectedFlags); | 
|  | 2307 | } | 
|  | 2308 |  | 
|  | 2309 | void consumeMotionPointerDown(int32_t pointerIdx) { | 
|  | 2310 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 2311 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
|  | 2312 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT, | 
|  | 2313 | 0 /*expectedFlags*/); | 
|  | 2314 | } | 
|  | 2315 |  | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2316 | MotionEvent* consumeMotion() { | 
|  | 2317 | InputEvent* event = mInputReceiver->consume(); | 
|  | 2318 | if (!event) { | 
|  | 2319 | ADD_FAILURE() << "No event was produced"; | 
|  | 2320 | return nullptr; | 
|  | 2321 | } | 
|  | 2322 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { | 
|  | 2323 | ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion"; | 
|  | 2324 | return nullptr; | 
|  | 2325 | } | 
|  | 2326 | return static_cast<MotionEvent*>(event); | 
|  | 2327 | } | 
|  | 2328 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2329 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } | 
|  | 2330 |  | 
|  | 2331 | private: | 
|  | 2332 | std::unique_ptr<FakeInputReceiver> mInputReceiver; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2333 | }; | 
|  | 2334 |  | 
|  | 2335 | // Tests for gesture monitors | 
|  | 2336 | TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2337 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2338 | sp<FakeWindowHandle> window = | 
|  | 2339 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2340 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2341 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2342 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2343 | true /*isGestureMonitor*/); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2344 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2345 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2346 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2347 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2348 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2349 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2350 | } | 
|  | 2351 |  | 
|  | 2352 | TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2353 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2354 | sp<FakeWindowHandle> window = | 
|  | 2355 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2356 |  | 
|  | 2357 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2358 | window->setFocusable(true); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2359 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2360 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2361 | setFocusedWindow(window); | 
|  | 2362 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2363 | window->consumeFocusEvent(true); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2364 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2365 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2366 | true /*isGestureMonitor*/); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2367 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2368 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
|  | 2369 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2370 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2371 | monitor.assertNoEvents(); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2372 | } | 
|  | 2373 |  | 
|  | 2374 | TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2375 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2376 | sp<FakeWindowHandle> window = | 
|  | 2377 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2378 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2379 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2380 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2381 | true /*isGestureMonitor*/); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2382 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2383 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2384 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2385 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2386 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2387 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2388 |  | 
|  | 2389 | window->releaseChannel(); | 
|  | 2390 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2391 | mDispatcher->pilferPointers(monitor.getToken()); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2392 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2393 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2394 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2395 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2396 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2397 | } | 
|  | 2398 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2399 | TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) { | 
|  | 2400 | FakeMonitorReceiver monitor = | 
|  | 2401 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, | 
|  | 2402 | true /*isGestureMonitor*/); | 
|  | 2403 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2404 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2405 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); | 
|  | 2406 | std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); | 
|  | 2407 | ASSERT_TRUE(consumeSeq); | 
|  | 2408 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 2409 | mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2410 | monitor.finishEvent(*consumeSeq); | 
|  | 2411 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 2412 | mFakePolicy->assertNotifyMonitorResponsiveWasCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2413 | } | 
|  | 2414 |  | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2415 | // Tests for gesture monitors | 
|  | 2416 | TEST_F(InputDispatcherTest, GestureMonitor_NoWindowTransform) { | 
|  | 2417 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2418 | sp<FakeWindowHandle> window = | 
|  | 2419 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2420 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2421 | window->setWindowOffset(20, 40); | 
|  | 2422 | window->setWindowTransform(0, 1, -1, 0); | 
|  | 2423 |  | 
|  | 2424 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2425 | true /*isGestureMonitor*/); | 
|  | 2426 |  | 
|  | 2427 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2428 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 2429 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 2430 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2431 | MotionEvent* event = monitor.consumeMotion(); | 
|  | 2432 | // Even though window has transform, gesture monitor must not. | 
|  | 2433 | ASSERT_EQ(ui::Transform(), event->getTransform()); | 
|  | 2434 | } | 
|  | 2435 |  | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2436 | TEST_F(InputDispatcherTest, TestMoveEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2437 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2438 | sp<FakeWindowHandle> window = | 
|  | 2439 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2440 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2441 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2442 |  | 
|  | 2443 | NotifyMotionArgs motionArgs = | 
|  | 2444 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2445 | ADISPLAY_ID_DEFAULT); | 
|  | 2446 |  | 
|  | 2447 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2448 | // Window should receive motion down event. | 
|  | 2449 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2450 |  | 
|  | 2451 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2452 | motionArgs.id += 1; | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2453 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 2454 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, | 
|  | 2455 | motionArgs.pointerCoords[0].getX() - 10); | 
|  | 2456 |  | 
|  | 2457 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2458 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, | 
|  | 2459 | 0 /*expectedFlags*/); | 
|  | 2460 | } | 
|  | 2461 |  | 
| Arthur Hung | 7162547 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 2462 | TEST_F(InputDispatcherTest, GestureMonitor_SplitIfNoWindowTouched) { | 
|  | 2463 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2464 | true /*isGestureMonitor*/); | 
|  | 2465 |  | 
|  | 2466 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2467 | // Create a non touch modal window that supports split touch | 
|  | 2468 | sp<FakeWindowHandle> window = | 
|  | 2469 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2470 | window->setFrame(Rect(0, 0, 100, 100)); | 
|  | 2471 | window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
|  | 2472 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2473 |  | 
|  | 2474 | // First finger down, no window touched. | 
|  | 2475 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2476 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 2477 | {100, 200})) | 
|  | 2478 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 2479 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2480 | window->assertNoEvents(); | 
|  | 2481 |  | 
|  | 2482 | // Second finger down on window, the window should receive touch down. | 
|  | 2483 | const MotionEvent secondFingerDownEvent = | 
|  | 2484 | MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 2485 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 2486 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2487 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 2488 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 2489 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 2490 | .x(100) | 
|  | 2491 | .y(200)) | 
|  | 2492 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) | 
|  | 2493 | .build(); | 
|  | 2494 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2495 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 2496 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 2497 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 2498 |  | 
|  | 2499 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2500 | monitor.consumeMotionPointerDown(1 /* pointerIndex */); | 
|  | 2501 | } | 
|  | 2502 |  | 
|  | 2503 | TEST_F(InputDispatcherTest, GestureMonitor_NoSplitAfterPilfer) { | 
|  | 2504 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2505 | true /*isGestureMonitor*/); | 
|  | 2506 |  | 
|  | 2507 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2508 | // Create a non touch modal window that supports split touch | 
|  | 2509 | sp<FakeWindowHandle> window = | 
|  | 2510 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2511 | window->setFrame(Rect(0, 0, 100, 100)); | 
|  | 2512 | window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
|  | 2513 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2514 |  | 
|  | 2515 | // First finger down, no window touched. | 
|  | 2516 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2517 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 2518 | {100, 200})) | 
|  | 2519 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 2520 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2521 | window->assertNoEvents(); | 
|  | 2522 |  | 
|  | 2523 | // Gesture monitor pilfer the pointers. | 
|  | 2524 | mDispatcher->pilferPointers(monitor.getToken()); | 
|  | 2525 |  | 
|  | 2526 | // Second finger down on window, the window should not receive touch down. | 
|  | 2527 | const MotionEvent secondFingerDownEvent = | 
|  | 2528 | MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 2529 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 2530 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2531 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 2532 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 2533 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 2534 | .x(100) | 
|  | 2535 | .y(200)) | 
|  | 2536 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) | 
|  | 2537 | .build(); | 
|  | 2538 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2539 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 2540 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 2541 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 2542 |  | 
|  | 2543 | window->assertNoEvents(); | 
|  | 2544 | monitor.consumeMotionPointerDown(1 /* pointerIndex */); | 
|  | 2545 | } | 
|  | 2546 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2547 | /** | 
|  | 2548 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to | 
|  | 2549 | * the device default right away. In the test scenario, we check both the default value, | 
|  | 2550 | * and the action of enabling / disabling. | 
|  | 2551 | */ | 
|  | 2552 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2553 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2554 | sp<FakeWindowHandle> window = | 
|  | 2555 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 2556 |  | 
|  | 2557 | // Set focused application. | 
|  | 2558 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2559 | window->setFocusable(true); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2560 |  | 
|  | 2561 | SCOPED_TRACE("Check default value of touch mode"); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2562 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2563 | setFocusedWindow(window); | 
|  | 2564 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2565 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 2566 |  | 
|  | 2567 | SCOPED_TRACE("Remove the window to trigger focus loss"); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2568 | window->setFocusable(false); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2569 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2570 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); | 
|  | 2571 |  | 
|  | 2572 | SCOPED_TRACE("Disable touch mode"); | 
|  | 2573 | mDispatcher->setInTouchMode(false); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2574 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2575 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2576 | setFocusedWindow(window); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2577 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); | 
|  | 2578 |  | 
|  | 2579 | SCOPED_TRACE("Remove the window to trigger focus loss"); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2580 | window->setFocusable(false); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2581 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2582 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); | 
|  | 2583 |  | 
|  | 2584 | SCOPED_TRACE("Enable touch mode again"); | 
|  | 2585 | mDispatcher->setInTouchMode(true); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2586 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2587 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2588 | setFocusedWindow(window); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2589 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 2590 |  | 
|  | 2591 | window->assertNoEvents(); | 
|  | 2592 | } | 
|  | 2593 |  | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2594 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2595 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2596 | sp<FakeWindowHandle> window = | 
|  | 2597 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 2598 |  | 
|  | 2599 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2600 | window->setFocusable(true); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2601 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2602 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2603 | setFocusedWindow(window); | 
|  | 2604 |  | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2605 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 2606 |  | 
|  | 2607 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); | 
|  | 2608 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2609 |  | 
|  | 2610 | InputEvent* event = window->consume(); | 
|  | 2611 | ASSERT_NE(event, nullptr); | 
|  | 2612 |  | 
|  | 2613 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); | 
|  | 2614 | ASSERT_NE(verified, nullptr); | 
|  | 2615 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); | 
|  | 2616 |  | 
|  | 2617 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); | 
|  | 2618 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); | 
|  | 2619 | ASSERT_EQ(keyArgs.source, verified->source); | 
|  | 2620 | ASSERT_EQ(keyArgs.displayId, verified->displayId); | 
|  | 2621 |  | 
|  | 2622 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); | 
|  | 2623 |  | 
|  | 2624 | ASSERT_EQ(keyArgs.action, verifiedKey.action); | 
|  | 2625 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2626 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); | 
|  | 2627 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); | 
|  | 2628 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); | 
|  | 2629 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); | 
|  | 2630 | ASSERT_EQ(0, verifiedKey.repeatCount); | 
|  | 2631 | } | 
|  | 2632 |  | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2633 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2634 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2635 | sp<FakeWindowHandle> window = | 
|  | 2636 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 2637 |  | 
|  | 2638 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2639 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2640 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2641 |  | 
|  | 2642 | NotifyMotionArgs motionArgs = | 
|  | 2643 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2644 | ADISPLAY_ID_DEFAULT); | 
|  | 2645 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2646 |  | 
|  | 2647 | InputEvent* event = window->consume(); | 
|  | 2648 | ASSERT_NE(event, nullptr); | 
|  | 2649 |  | 
|  | 2650 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); | 
|  | 2651 | ASSERT_NE(verified, nullptr); | 
|  | 2652 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); | 
|  | 2653 |  | 
|  | 2654 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); | 
|  | 2655 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); | 
|  | 2656 | EXPECT_EQ(motionArgs.source, verified->source); | 
|  | 2657 | EXPECT_EQ(motionArgs.displayId, verified->displayId); | 
|  | 2658 |  | 
|  | 2659 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); | 
|  | 2660 |  | 
|  | 2661 | EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX); | 
|  | 2662 | EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY); | 
|  | 2663 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); | 
|  | 2664 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); | 
|  | 2665 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); | 
|  | 2666 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); | 
|  | 2667 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); | 
|  | 2668 | } | 
|  | 2669 |  | 
| Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 2670 | TEST_F(InputDispatcherTest, NonPointerMotionEvent_JoystickAndTouchpadNotTransformed) { | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2671 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2672 | sp<FakeWindowHandle> window = | 
|  | 2673 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 2674 | const std::string name = window->getName(); | 
|  | 2675 |  | 
|  | 2676 | // Window gets transformed by offset values. | 
|  | 2677 | window->setWindowOffset(500.0f, 500.0f); | 
|  | 2678 |  | 
|  | 2679 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2680 | window->setFocusable(true); | 
|  | 2681 |  | 
|  | 2682 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2683 |  | 
|  | 2684 | // First, we set focused window so that focusedWindowHandle is not null. | 
|  | 2685 | setFocusedWindow(window); | 
|  | 2686 |  | 
|  | 2687 | // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked. | 
|  | 2688 | window->consumeFocusEvent(true); | 
|  | 2689 |  | 
| Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 2690 | constexpr const std::array nonTransformedSources = {std::pair(AINPUT_SOURCE_TOUCHPAD, | 
|  | 2691 | AMOTION_EVENT_ACTION_DOWN), | 
|  | 2692 | std::pair(AINPUT_SOURCE_JOYSTICK, | 
|  | 2693 | AMOTION_EVENT_ACTION_MOVE)}; | 
|  | 2694 | for (const auto& [source, action] : nonTransformedSources) { | 
|  | 2695 | const NotifyMotionArgs motionArgs = generateMotionArgs(action, source, ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2696 | mDispatcher->notifyMotion(&motionArgs); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2697 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 2698 | MotionEvent* event = window->consumeMotion(); | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2699 | ASSERT_NE(event, nullptr); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2700 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 2701 | const MotionEvent& motionEvent = *event; | 
| Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 2702 | EXPECT_EQ(action, motionEvent.getAction()); | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2703 | EXPECT_EQ(motionArgs.pointerCount, motionEvent.getPointerCount()); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2704 |  | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2705 | float expectedX = motionArgs.pointerCoords[0].getX(); | 
|  | 2706 | float expectedY = motionArgs.pointerCoords[0].getY(); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2707 |  | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2708 | // Ensure the axis values from the final motion event are not transformed. | 
|  | 2709 | EXPECT_EQ(expectedX, motionEvent.getX(0)) | 
|  | 2710 | << "expected " << expectedX << " for x coord of " << name.c_str() << ", got " | 
|  | 2711 | << motionEvent.getX(0); | 
|  | 2712 | EXPECT_EQ(expectedY, motionEvent.getY(0)) | 
|  | 2713 | << "expected " << expectedY << " for y coord of " << name.c_str() << ", got " | 
|  | 2714 | << motionEvent.getY(0); | 
|  | 2715 | // Ensure the raw and transformed axis values for the motion event are the same. | 
|  | 2716 | EXPECT_EQ(motionEvent.getRawX(0), motionEvent.getX(0)) | 
|  | 2717 | << "expected raw and transformed X-axis values to be equal"; | 
|  | 2718 | EXPECT_EQ(motionEvent.getRawY(0), motionEvent.getY(0)) | 
|  | 2719 | << "expected raw and transformed Y-axis values to be equal"; | 
|  | 2720 | } | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2721 | } | 
|  | 2722 |  | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2723 | /** | 
|  | 2724 | * Ensure that separate calls to sign the same data are generating the same key. | 
|  | 2725 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance | 
|  | 2726 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky | 
|  | 2727 | * tests. | 
|  | 2728 | */ | 
|  | 2729 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { | 
|  | 2730 | KeyEvent event = getTestKeyEvent(); | 
|  | 2731 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); | 
|  | 2732 |  | 
|  | 2733 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); | 
|  | 2734 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); | 
|  | 2735 | ASSERT_EQ(hmac1, hmac2); | 
|  | 2736 | } | 
|  | 2737 |  | 
|  | 2738 | /** | 
|  | 2739 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. | 
|  | 2740 | */ | 
|  | 2741 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { | 
|  | 2742 | KeyEvent event = getTestKeyEvent(); | 
|  | 2743 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); | 
|  | 2744 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); | 
|  | 2745 |  | 
|  | 2746 | verifiedEvent.deviceId += 1; | 
|  | 2747 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2748 |  | 
|  | 2749 | verifiedEvent.source += 1; | 
|  | 2750 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2751 |  | 
|  | 2752 | verifiedEvent.eventTimeNanos += 1; | 
|  | 2753 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2754 |  | 
|  | 2755 | verifiedEvent.displayId += 1; | 
|  | 2756 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2757 |  | 
|  | 2758 | verifiedEvent.action += 1; | 
|  | 2759 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2760 |  | 
|  | 2761 | verifiedEvent.downTimeNanos += 1; | 
|  | 2762 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2763 |  | 
|  | 2764 | verifiedEvent.flags += 1; | 
|  | 2765 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2766 |  | 
|  | 2767 | verifiedEvent.keyCode += 1; | 
|  | 2768 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2769 |  | 
|  | 2770 | verifiedEvent.scanCode += 1; | 
|  | 2771 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2772 |  | 
|  | 2773 | verifiedEvent.metaState += 1; | 
|  | 2774 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2775 |  | 
|  | 2776 | verifiedEvent.repeatCount += 1; | 
|  | 2777 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2778 | } | 
|  | 2779 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2780 | TEST_F(InputDispatcherTest, SetFocusedWindow) { | 
|  | 2781 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2782 | sp<FakeWindowHandle> windowTop = | 
|  | 2783 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 2784 | sp<FakeWindowHandle> windowSecond = | 
|  | 2785 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 2786 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2787 |  | 
|  | 2788 | // Top window is also focusable but is not granted focus. | 
|  | 2789 | windowTop->setFocusable(true); | 
|  | 2790 | windowSecond->setFocusable(true); | 
|  | 2791 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
|  | 2792 | setFocusedWindow(windowSecond); | 
|  | 2793 |  | 
|  | 2794 | windowSecond->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2795 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 2796 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2797 |  | 
|  | 2798 | // Focused window should receive event. | 
|  | 2799 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 2800 | windowTop->assertNoEvents(); | 
|  | 2801 | } | 
|  | 2802 |  | 
|  | 2803 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { | 
|  | 2804 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2805 | sp<FakeWindowHandle> window = | 
|  | 2806 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 2807 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2808 |  | 
|  | 2809 | window->setFocusable(true); | 
|  | 2810 | // Release channel for window is no longer valid. | 
|  | 2811 | window->releaseChannel(); | 
|  | 2812 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2813 | setFocusedWindow(window); | 
|  | 2814 |  | 
|  | 2815 | // Test inject a key down, should timeout. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2816 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 2817 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2818 |  | 
|  | 2819 | // window channel is invalid, so it should not receive any input event. | 
|  | 2820 | window->assertNoEvents(); | 
|  | 2821 | } | 
|  | 2822 |  | 
|  | 2823 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { | 
|  | 2824 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2825 | sp<FakeWindowHandle> window = | 
|  | 2826 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 2827 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2828 |  | 
|  | 2829 | // Window is not focusable. | 
|  | 2830 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2831 | setFocusedWindow(window); | 
|  | 2832 |  | 
|  | 2833 | // Test inject a key down, should timeout. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2834 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 2835 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2836 |  | 
|  | 2837 | // window is invalid, so it should not receive any input event. | 
|  | 2838 | window->assertNoEvents(); | 
|  | 2839 | } | 
|  | 2840 |  | 
|  | 2841 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { | 
|  | 2842 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2843 | sp<FakeWindowHandle> windowTop = | 
|  | 2844 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 2845 | sp<FakeWindowHandle> windowSecond = | 
|  | 2846 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 2847 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2848 |  | 
|  | 2849 | windowTop->setFocusable(true); | 
|  | 2850 | windowSecond->setFocusable(true); | 
|  | 2851 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
|  | 2852 | setFocusedWindow(windowTop); | 
|  | 2853 | windowTop->consumeFocusEvent(true); | 
|  | 2854 |  | 
|  | 2855 | setFocusedWindow(windowSecond, windowTop); | 
|  | 2856 | windowSecond->consumeFocusEvent(true); | 
|  | 2857 | windowTop->consumeFocusEvent(false); | 
|  | 2858 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2859 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 2860 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2861 |  | 
|  | 2862 | // Focused window should receive event. | 
|  | 2863 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 2864 | } | 
|  | 2865 |  | 
|  | 2866 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { | 
|  | 2867 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2868 | sp<FakeWindowHandle> windowTop = | 
|  | 2869 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 2870 | sp<FakeWindowHandle> windowSecond = | 
|  | 2871 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 2872 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2873 |  | 
|  | 2874 | windowTop->setFocusable(true); | 
|  | 2875 | windowSecond->setFocusable(true); | 
|  | 2876 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
|  | 2877 | setFocusedWindow(windowSecond, windowTop); | 
|  | 2878 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2879 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 2880 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2881 |  | 
|  | 2882 | // Event should be dropped. | 
|  | 2883 | windowTop->assertNoEvents(); | 
|  | 2884 | windowSecond->assertNoEvents(); | 
|  | 2885 | } | 
|  | 2886 |  | 
|  | 2887 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { | 
|  | 2888 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2889 | sp<FakeWindowHandle> window = | 
|  | 2890 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 2891 | sp<FakeWindowHandle> previousFocusedWindow = | 
|  | 2892 | new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow", | 
|  | 2893 | ADISPLAY_ID_DEFAULT); | 
|  | 2894 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2895 |  | 
|  | 2896 | window->setFocusable(true); | 
|  | 2897 | previousFocusedWindow->setFocusable(true); | 
|  | 2898 | window->setVisible(false); | 
|  | 2899 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); | 
|  | 2900 | setFocusedWindow(previousFocusedWindow); | 
|  | 2901 | previousFocusedWindow->consumeFocusEvent(true); | 
|  | 2902 |  | 
|  | 2903 | // Requesting focus on invisible window takes focus from currently focused window. | 
|  | 2904 | setFocusedWindow(window); | 
|  | 2905 | previousFocusedWindow->consumeFocusEvent(false); | 
|  | 2906 |  | 
|  | 2907 | // Injected key goes to pending queue. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2908 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2909 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2910 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2911 |  | 
|  | 2912 | // Window does not get focus event or key down. | 
|  | 2913 | window->assertNoEvents(); | 
|  | 2914 |  | 
|  | 2915 | // Window becomes visible. | 
|  | 2916 | window->setVisible(true); | 
|  | 2917 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2918 |  | 
|  | 2919 | // Window receives focus event. | 
|  | 2920 | window->consumeFocusEvent(true); | 
|  | 2921 | // Focused window receives key down. | 
|  | 2922 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 2923 | } | 
|  | 2924 |  | 
| Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 2925 | TEST_F(InputDispatcherTest, DisplayRemoved) { | 
|  | 2926 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2927 | sp<FakeWindowHandle> window = | 
|  | 2928 | new FakeWindowHandle(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); | 
|  | 2929 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2930 |  | 
|  | 2931 | // window is granted focus. | 
|  | 2932 | window->setFocusable(true); | 
|  | 2933 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2934 | setFocusedWindow(window); | 
|  | 2935 | window->consumeFocusEvent(true); | 
|  | 2936 |  | 
|  | 2937 | // When a display is removed window loses focus. | 
|  | 2938 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); | 
|  | 2939 | window->consumeFocusEvent(false); | 
|  | 2940 | } | 
|  | 2941 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2942 | /** | 
|  | 2943 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, | 
|  | 2944 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top | 
|  | 2945 | * of the 'slipperyEnterWindow'. | 
|  | 2946 | * | 
|  | 2947 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such | 
|  | 2948 | * a way so that the touched location is no longer covered by the top window. | 
|  | 2949 | * | 
|  | 2950 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now | 
|  | 2951 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had | 
|  | 2952 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive | 
|  | 2953 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting | 
|  | 2954 | * with ACTION_DOWN). | 
|  | 2955 | * Thus, the touch has been transferred from the top window into the bottom window, because the top | 
|  | 2956 | * window moved itself away from the touched location and had Flag::SLIPPERY. | 
|  | 2957 | * | 
|  | 2958 | * Even though the top window moved away from the touched location, it is still obscuring the bottom | 
|  | 2959 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ | 
|  | 2960 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. | 
|  | 2961 | * | 
|  | 2962 | * In this test, we ensure that the event received by the bottom window has | 
|  | 2963 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. | 
|  | 2964 | */ | 
|  | 2965 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { | 
|  | 2966 | constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1; | 
|  | 2967 | constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1; | 
|  | 2968 |  | 
|  | 2969 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2970 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2971 |  | 
|  | 2972 | sp<FakeWindowHandle> slipperyExitWindow = | 
|  | 2973 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2974 | slipperyExitWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SLIPPERY); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2975 | // Make sure this one overlaps the bottom window | 
|  | 2976 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); | 
|  | 2977 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom | 
|  | 2978 | // one. Windows with the same owner are not considered to be occluding each other. | 
|  | 2979 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); | 
|  | 2980 |  | 
|  | 2981 | sp<FakeWindowHandle> slipperyEnterWindow = | 
|  | 2982 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 2983 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 2984 |  | 
|  | 2985 | mDispatcher->setInputWindows( | 
|  | 2986 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); | 
|  | 2987 |  | 
|  | 2988 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions | 
|  | 2989 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2990 | ADISPLAY_ID_DEFAULT, {{50, 50}}); | 
|  | 2991 | mDispatcher->notifyMotion(&args); | 
|  | 2992 | slipperyExitWindow->consumeMotionDown(); | 
|  | 2993 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); | 
|  | 2994 | mDispatcher->setInputWindows( | 
|  | 2995 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); | 
|  | 2996 |  | 
|  | 2997 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2998 | ADISPLAY_ID_DEFAULT, {{51, 51}}); | 
|  | 2999 | mDispatcher->notifyMotion(&args); | 
|  | 3000 |  | 
|  | 3001 | slipperyExitWindow->consumeMotionCancel(); | 
|  | 3002 |  | 
|  | 3003 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, | 
|  | 3004 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); | 
|  | 3005 | } | 
|  | 3006 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3007 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { | 
|  | 3008 | protected: | 
|  | 3009 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms | 
|  | 3010 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000;   // 40 ms | 
|  | 3011 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3012 | std::shared_ptr<FakeApplicationHandle> mApp; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3013 | sp<FakeWindowHandle> mWindow; | 
|  | 3014 |  | 
|  | 3015 | virtual void SetUp() override { | 
|  | 3016 | mFakePolicy = new FakeInputDispatcherPolicy(); | 
|  | 3017 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); | 
|  | 3018 | mDispatcher = new InputDispatcher(mFakePolicy); | 
|  | 3019 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); | 
|  | 3020 | ASSERT_EQ(OK, mDispatcher->start()); | 
|  | 3021 |  | 
|  | 3022 | setUpWindow(); | 
|  | 3023 | } | 
|  | 3024 |  | 
|  | 3025 | void setUpWindow() { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3026 | mApp = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3027 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 3028 |  | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3029 | mWindow->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3030 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3031 | setFocusedWindow(mWindow); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3032 | mWindow->consumeFocusEvent(true); | 
|  | 3033 | } | 
|  | 3034 |  | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3035 | void sendAndConsumeKeyDown(int32_t deviceId) { | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3036 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3037 | keyArgs.deviceId = deviceId; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3038 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event | 
|  | 3039 | mDispatcher->notifyKey(&keyArgs); | 
|  | 3040 |  | 
|  | 3041 | // Window should receive key down event. | 
|  | 3042 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 3043 | } | 
|  | 3044 |  | 
|  | 3045 | void expectKeyRepeatOnce(int32_t repeatCount) { | 
|  | 3046 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); | 
|  | 3047 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 3048 | ASSERT_NE(nullptr, repeatEvent); | 
|  | 3049 |  | 
|  | 3050 | uint32_t eventType = repeatEvent->getType(); | 
|  | 3051 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); | 
|  | 3052 |  | 
|  | 3053 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); | 
|  | 3054 | uint32_t eventAction = repeatKeyEvent->getAction(); | 
|  | 3055 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); | 
|  | 3056 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); | 
|  | 3057 | } | 
|  | 3058 |  | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3059 | void sendAndConsumeKeyUp(int32_t deviceId) { | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3060 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3061 | keyArgs.deviceId = deviceId; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3062 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event | 
|  | 3063 | mDispatcher->notifyKey(&keyArgs); | 
|  | 3064 |  | 
|  | 3065 | // Window should receive key down event. | 
|  | 3066 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, | 
|  | 3067 | 0 /*expectedFlags*/); | 
|  | 3068 | } | 
|  | 3069 | }; | 
|  | 3070 |  | 
|  | 3071 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3072 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 3073 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 3074 | expectKeyRepeatOnce(repeatCount); | 
|  | 3075 | } | 
|  | 3076 | } | 
|  | 3077 |  | 
|  | 3078 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { | 
|  | 3079 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 3080 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 3081 | expectKeyRepeatOnce(repeatCount); | 
|  | 3082 | } | 
|  | 3083 | sendAndConsumeKeyDown(2 /* deviceId */); | 
|  | 3084 | /* repeatCount will start from 1 for deviceId 2 */ | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3085 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 3086 | expectKeyRepeatOnce(repeatCount); | 
|  | 3087 | } | 
|  | 3088 | } | 
|  | 3089 |  | 
|  | 3090 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3091 | sendAndConsumeKeyDown(1 /* deviceId */); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3092 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3093 | sendAndConsumeKeyUp(1 /* deviceId */); | 
|  | 3094 | mWindow->assertNoEvents(); | 
|  | 3095 | } | 
|  | 3096 |  | 
|  | 3097 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { | 
|  | 3098 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 3099 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 3100 | sendAndConsumeKeyDown(2 /* deviceId */); | 
|  | 3101 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 3102 | // Stale key up from device 1. | 
|  | 3103 | sendAndConsumeKeyUp(1 /* deviceId */); | 
|  | 3104 | // Device 2 is still down, keep repeating | 
|  | 3105 | expectKeyRepeatOnce(2 /*repeatCount*/); | 
|  | 3106 | expectKeyRepeatOnce(3 /*repeatCount*/); | 
|  | 3107 | // Device 2 key up | 
|  | 3108 | sendAndConsumeKeyUp(2 /* deviceId */); | 
|  | 3109 | mWindow->assertNoEvents(); | 
|  | 3110 | } | 
|  | 3111 |  | 
|  | 3112 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { | 
|  | 3113 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 3114 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 3115 | sendAndConsumeKeyDown(2 /* deviceId */); | 
|  | 3116 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 3117 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. | 
|  | 3118 | sendAndConsumeKeyUp(2 /* deviceId */); | 
|  | 3119 | // Device 1 still holds key down, but the repeating was already stopped | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3120 | mWindow->assertNoEvents(); | 
|  | 3121 | } | 
|  | 3122 |  | 
| liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 3123 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { | 
|  | 3124 | sendAndConsumeKeyDown(DEVICE_ID); | 
|  | 3125 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 3126 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); | 
|  | 3127 | mDispatcher->notifyDeviceReset(&args); | 
|  | 3128 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, | 
|  | 3129 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); | 
|  | 3130 | mWindow->assertNoEvents(); | 
|  | 3131 | } | 
|  | 3132 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3133 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3134 | sendAndConsumeKeyDown(1 /* deviceId */); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3135 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 3136 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 3137 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; | 
|  | 3138 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, | 
|  | 3139 | IdGenerator::getSource(repeatEvent->getId())); | 
|  | 3140 | } | 
|  | 3141 | } | 
|  | 3142 |  | 
|  | 3143 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3144 | sendAndConsumeKeyDown(1 /* deviceId */); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3145 |  | 
|  | 3146 | std::unordered_set<int32_t> idSet; | 
|  | 3147 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 3148 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 3149 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; | 
|  | 3150 | int32_t id = repeatEvent->getId(); | 
|  | 3151 | EXPECT_EQ(idSet.end(), idSet.find(id)); | 
|  | 3152 | idSet.insert(id); | 
|  | 3153 | } | 
|  | 3154 | } | 
|  | 3155 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3156 | /* Test InputDispatcher for MultiDisplay */ | 
|  | 3157 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { | 
|  | 3158 | public: | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3159 | virtual void SetUp() override { | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3160 | InputDispatcherTest::SetUp(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3161 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3162 | application1 = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3163 | windowInPrimary = | 
|  | 3164 | new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3165 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3166 | // Set focus window for primary display, but focused display would be second one. | 
|  | 3167 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3168 | windowInPrimary->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3169 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3170 | setFocusedWindow(windowInPrimary); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3171 | windowInPrimary->consumeFocusEvent(true); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3172 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3173 | application2 = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3174 | windowInSecondary = | 
|  | 3175 | new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3176 | // Set focus to second display window. | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3177 | // Set focus display to second one. | 
|  | 3178 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); | 
|  | 3179 | // Set focus window for second display. | 
|  | 3180 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3181 | windowInSecondary->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3182 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3183 | setFocusedWindow(windowInSecondary); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3184 | windowInSecondary->consumeFocusEvent(true); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3185 | } | 
|  | 3186 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3187 | virtual void TearDown() override { | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3188 | InputDispatcherTest::TearDown(); | 
|  | 3189 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3190 | application1.reset(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3191 | windowInPrimary.clear(); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3192 | application2.reset(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3193 | windowInSecondary.clear(); | 
|  | 3194 | } | 
|  | 3195 |  | 
|  | 3196 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3197 | std::shared_ptr<FakeApplicationHandle> application1; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3198 | sp<FakeWindowHandle> windowInPrimary; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3199 | std::shared_ptr<FakeApplicationHandle> application2; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3200 | sp<FakeWindowHandle> windowInSecondary; | 
|  | 3201 | }; | 
|  | 3202 |  | 
|  | 3203 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { | 
|  | 3204 | // Test touch down on primary display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3205 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3206 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 3207 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3208 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3209 | windowInSecondary->assertNoEvents(); | 
|  | 3210 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3211 | // Test touch down on second display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3212 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3213 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) | 
|  | 3214 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3215 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3216 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3217 | } | 
|  | 3218 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3219 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3220 | // Test inject a key down with display id specified. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3221 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3222 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3223 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3224 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3225 | windowInSecondary->assertNoEvents(); | 
|  | 3226 |  | 
|  | 3227 | // Test inject a key down without display id specified. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3228 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3229 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3230 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3231 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3232 |  | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3233 | // Remove all windows in secondary display. | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3234 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3235 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3236 | // Old focus should receive a cancel event. | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3237 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, | 
|  | 3238 | AKEY_EVENT_FLAG_CANCELED); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3239 |  | 
|  | 3240 | // Test inject a key down, should timeout because of no target window. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3241 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3242 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3243 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3244 | windowInSecondary->consumeFocusEvent(false); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3245 | windowInSecondary->assertNoEvents(); | 
|  | 3246 | } | 
|  | 3247 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3248 | // Test per-display input monitors for motion event. | 
|  | 3249 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3250 | FakeMonitorReceiver monitorInPrimary = | 
|  | 3251 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
|  | 3252 | FakeMonitorReceiver monitorInSecondary = | 
|  | 3253 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3254 |  | 
|  | 3255 | // Test touch down on primary display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3256 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3257 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 3258 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3259 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3260 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3261 | windowInSecondary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3262 | monitorInSecondary.assertNoEvents(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3263 |  | 
|  | 3264 | // Test touch down on second display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3265 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3266 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) | 
|  | 3267 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3268 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3269 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3270 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3271 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3272 |  | 
|  | 3273 | // Test inject a non-pointer motion event. | 
|  | 3274 | // If specific a display, it will dispatch to the focused window of particular display, | 
|  | 3275 | // or it will dispatch to the focused window of focused display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3276 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3277 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) | 
|  | 3278 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3279 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3280 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3281 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3282 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3283 | } | 
|  | 3284 |  | 
|  | 3285 | // Test per-display input monitors for key event. | 
|  | 3286 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3287 | // Input monitor per display. | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3288 | FakeMonitorReceiver monitorInPrimary = | 
|  | 3289 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
|  | 3290 | FakeMonitorReceiver monitorInSecondary = | 
|  | 3291 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3292 |  | 
|  | 3293 | // Test inject a key down. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3294 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 3295 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3296 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3297 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3298 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3299 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3300 | } | 
|  | 3301 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3302 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { | 
|  | 3303 | sp<FakeWindowHandle> secondWindowInPrimary = | 
|  | 3304 | new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); | 
|  | 3305 | secondWindowInPrimary->setFocusable(true); | 
|  | 3306 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); | 
|  | 3307 | setFocusedWindow(secondWindowInPrimary); | 
|  | 3308 | windowInPrimary->consumeFocusEvent(false); | 
|  | 3309 | secondWindowInPrimary->consumeFocusEvent(true); | 
|  | 3310 |  | 
|  | 3311 | // Test inject a key down. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3312 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
|  | 3313 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3314 | windowInPrimary->assertNoEvents(); | 
|  | 3315 | windowInSecondary->assertNoEvents(); | 
|  | 3316 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 3317 | } | 
|  | 3318 |  | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3319 | class InputFilterTest : public InputDispatcherTest { | 
|  | 3320 | protected: | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3321 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) { | 
|  | 3322 | NotifyMotionArgs motionArgs; | 
|  | 3323 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3324 | motionArgs = | 
|  | 3325 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3326 | mDispatcher->notifyMotion(&motionArgs); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3327 | motionArgs = | 
|  | 3328 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3329 | mDispatcher->notifyMotion(&motionArgs); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3330 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3331 | if (expectToBeFiltered) { | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 3332 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3333 | } else { | 
|  | 3334 | mFakePolicy->assertFilterInputEventWasNotCalled(); | 
|  | 3335 | } | 
|  | 3336 | } | 
|  | 3337 |  | 
|  | 3338 | void testNotifyKey(bool expectToBeFiltered) { | 
|  | 3339 | NotifyKeyArgs keyArgs; | 
|  | 3340 |  | 
|  | 3341 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); | 
|  | 3342 | mDispatcher->notifyKey(&keyArgs); | 
|  | 3343 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); | 
|  | 3344 | mDispatcher->notifyKey(&keyArgs); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3345 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3346 |  | 
|  | 3347 | if (expectToBeFiltered) { | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 3348 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3349 | } else { | 
|  | 3350 | mFakePolicy->assertFilterInputEventWasNotCalled(); | 
|  | 3351 | } | 
|  | 3352 | } | 
|  | 3353 | }; | 
|  | 3354 |  | 
|  | 3355 | // Test InputFilter for MotionEvent | 
|  | 3356 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { | 
|  | 3357 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. | 
|  | 3358 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); | 
|  | 3359 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); | 
|  | 3360 |  | 
|  | 3361 | // Enable InputFilter | 
|  | 3362 | mDispatcher->setInputFilterEnabled(true); | 
|  | 3363 | // Test touch on both primary and second display, and check if both events are filtered. | 
|  | 3364 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); | 
|  | 3365 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); | 
|  | 3366 |  | 
|  | 3367 | // Disable InputFilter | 
|  | 3368 | mDispatcher->setInputFilterEnabled(false); | 
|  | 3369 | // Test touch on both primary and second display, and check if both events aren't filtered. | 
|  | 3370 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); | 
|  | 3371 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); | 
|  | 3372 | } | 
|  | 3373 |  | 
|  | 3374 | // Test InputFilter for KeyEvent | 
|  | 3375 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { | 
|  | 3376 | // Since the InputFilter is disabled by default, check if key event aren't filtered. | 
|  | 3377 | testNotifyKey(/*expectToBeFiltered*/ false); | 
|  | 3378 |  | 
|  | 3379 | // Enable InputFilter | 
|  | 3380 | mDispatcher->setInputFilterEnabled(true); | 
|  | 3381 | // Send a key event, and check if it is filtered. | 
|  | 3382 | testNotifyKey(/*expectToBeFiltered*/ true); | 
|  | 3383 |  | 
|  | 3384 | // Disable InputFilter | 
|  | 3385 | mDispatcher->setInputFilterEnabled(false); | 
|  | 3386 | // Send a key event, and check if it isn't filtered. | 
|  | 3387 | testNotifyKey(/*expectToBeFiltered*/ false); | 
|  | 3388 | } | 
|  | 3389 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3390 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { | 
|  | 3391 | protected: | 
|  | 3392 | virtual void SetUp() override { | 
|  | 3393 | InputDispatcherTest::SetUp(); | 
|  | 3394 |  | 
|  | 3395 | /** | 
|  | 3396 | * We don't need to enable input filter to test the injected event policy, but we enabled it | 
|  | 3397 | * here to make the tests more realistic, since this policy only matters when inputfilter is | 
|  | 3398 | * on. | 
|  | 3399 | */ | 
|  | 3400 | mDispatcher->setInputFilterEnabled(true); | 
|  | 3401 |  | 
|  | 3402 | std::shared_ptr<InputApplicationHandle> application = | 
|  | 3403 | std::make_shared<FakeApplicationHandle>(); | 
|  | 3404 | mWindow = | 
|  | 3405 | new FakeWindowHandle(application, mDispatcher, "Test Window", ADISPLAY_ID_DEFAULT); | 
|  | 3406 |  | 
|  | 3407 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3408 | mWindow->setFocusable(true); | 
|  | 3409 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3410 | setFocusedWindow(mWindow); | 
|  | 3411 | mWindow->consumeFocusEvent(true); | 
|  | 3412 | } | 
|  | 3413 |  | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3414 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, | 
|  | 3415 | int32_t flags) { | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3416 | KeyEvent event; | 
|  | 3417 |  | 
|  | 3418 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 3419 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, | 
|  | 3420 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, | 
|  | 3421 | KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime); | 
|  | 3422 | const int32_t additionalPolicyFlags = | 
|  | 3423 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; | 
|  | 3424 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3425 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
|  | 3426 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, | 
|  | 3427 | policyFlags | additionalPolicyFlags)); | 
|  | 3428 |  | 
|  | 3429 | InputEvent* received = mWindow->consume(); | 
|  | 3430 | ASSERT_NE(nullptr, received); | 
|  | 3431 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3432 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY); | 
|  | 3433 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); | 
|  | 3434 | ASSERT_EQ(flags, keyEvent.getFlags()); | 
|  | 3435 | } | 
|  | 3436 |  | 
|  | 3437 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, | 
|  | 3438 | int32_t flags) { | 
|  | 3439 | MotionEvent event; | 
|  | 3440 | PointerProperties pointerProperties[1]; | 
|  | 3441 | PointerCoords pointerCoords[1]; | 
|  | 3442 | pointerProperties[0].clear(); | 
|  | 3443 | pointerProperties[0].id = 0; | 
|  | 3444 | pointerCoords[0].clear(); | 
|  | 3445 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); | 
|  | 3446 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); | 
|  | 3447 |  | 
|  | 3448 | ui::Transform identityTransform; | 
|  | 3449 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 3450 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3451 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, | 
|  | 3452 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, | 
|  | 3453 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 3454 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0, | 
|  | 3455 | 0 /*INVALID_DISPLAY_SIZE*/, 0 /*INVALID_DISPLAY_SIZE*/, eventTime, | 
|  | 3456 | eventTime, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3457 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
|  | 3458 |  | 
|  | 3459 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; | 
|  | 3460 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3461 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
|  | 3462 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, | 
|  | 3463 | policyFlags | additionalPolicyFlags)); | 
|  | 3464 |  | 
|  | 3465 | InputEvent* received = mWindow->consume(); | 
|  | 3466 | ASSERT_NE(nullptr, received); | 
|  | 3467 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); | 
|  | 3468 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION); | 
|  | 3469 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); | 
|  | 3470 | ASSERT_EQ(flags, motionEvent.getFlags()); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3471 | } | 
|  | 3472 |  | 
|  | 3473 | private: | 
|  | 3474 | sp<FakeWindowHandle> mWindow; | 
|  | 3475 | }; | 
|  | 3476 |  | 
|  | 3477 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3478 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input | 
|  | 3479 | // filter. Without it, the event will no different from a regularly injected event, and the | 
|  | 3480 | // injected device id will be overwritten. | 
|  | 3481 | testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, | 
|  | 3482 | 0 /*flags*/); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3483 | } | 
|  | 3484 |  | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3485 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3486 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3487 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, | 
|  | 3488 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); | 
|  | 3489 | } | 
|  | 3490 |  | 
|  | 3491 | TEST_F(InputFilterInjectionPolicyTest, | 
|  | 3492 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { | 
|  | 3493 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, | 
|  | 3494 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, | 
|  | 3495 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3496 | } | 
|  | 3497 |  | 
|  | 3498 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { | 
|  | 3499 | testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3500 | VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3501 | } | 
|  | 3502 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3503 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3504 | virtual void SetUp() override { | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3505 | InputDispatcherTest::SetUp(); | 
|  | 3506 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3507 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 3508 | std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3509 | mUnfocusedWindow = | 
|  | 3510 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3511 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); | 
|  | 3512 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this | 
|  | 3513 | // window. | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3514 | mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3515 |  | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3516 | mFocusedWindow = | 
|  | 3517 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 3518 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3519 | mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3520 |  | 
|  | 3521 | // Set focused application. | 
|  | 3522 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3523 | mFocusedWindow->setFocusable(true); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3524 |  | 
|  | 3525 | // Expect one focus window exist in display. | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3526 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3527 | setFocusedWindow(mFocusedWindow); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3528 | mFocusedWindow->consumeFocusEvent(true); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3529 | } | 
|  | 3530 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3531 | virtual void TearDown() override { | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3532 | InputDispatcherTest::TearDown(); | 
|  | 3533 |  | 
|  | 3534 | mUnfocusedWindow.clear(); | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3535 | mFocusedWindow.clear(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3536 | } | 
|  | 3537 |  | 
|  | 3538 | protected: | 
|  | 3539 | sp<FakeWindowHandle> mUnfocusedWindow; | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3540 | sp<FakeWindowHandle> mFocusedWindow; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3541 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3542 | }; | 
|  | 3543 |  | 
|  | 3544 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action | 
|  | 3545 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received | 
|  | 3546 | // the onPointerDownOutsideFocus callback. | 
|  | 3547 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3548 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3549 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3550 | {20, 20})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3551 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3552 | mUnfocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3553 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3554 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3555 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); | 
|  | 3556 | } | 
|  | 3557 |  | 
|  | 3558 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action | 
|  | 3559 | // DOWN on the window that doesn't have focus. Ensure no window received the | 
|  | 3560 | // onPointerDownOutsideFocus callback. | 
|  | 3561 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3562 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3563 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3564 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3565 | mFocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3566 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3567 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3568 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3569 | } | 
|  | 3570 |  | 
|  | 3571 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't | 
|  | 3572 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. | 
|  | 3573 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3574 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3575 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3576 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3577 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3578 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3579 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3580 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3581 | } | 
|  | 3582 |  | 
|  | 3583 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action | 
|  | 3584 | // DOWN on the window that already has focus. Ensure no window received the | 
|  | 3585 | // onPointerDownOutsideFocus callback. | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3586 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3587 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3588 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3589 | FOCUSED_WINDOW_TOUCH_POINT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3590 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3591 | mFocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3592 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3593 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3594 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3595 | } | 
|  | 3596 |  | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3597 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag | 
|  | 3598 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. | 
|  | 3599 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { | 
|  | 3600 | const MotionEvent event = | 
|  | 3601 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 3602 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 3603 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20)) | 
|  | 3604 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) | 
|  | 3605 | .build(); | 
|  | 3606 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) | 
|  | 3607 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 3608 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); | 
|  | 3609 |  | 
|  | 3610 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3611 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
|  | 3612 | // Ensure that the unfocused window did not receive any FOCUS events. | 
|  | 3613 | mUnfocusedWindow->assertNoEvents(); | 
|  | 3614 | } | 
|  | 3615 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3616 | // These tests ensures we can send touch events to a single client when there are multiple input | 
|  | 3617 | // windows that point to the same client token. | 
|  | 3618 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { | 
|  | 3619 | virtual void SetUp() override { | 
|  | 3620 | InputDispatcherTest::SetUp(); | 
|  | 3621 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3622 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 3623 | std::make_shared<FakeApplicationHandle>(); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3624 | mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1", | 
|  | 3625 | ADISPLAY_ID_DEFAULT); | 
|  | 3626 | // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window. | 
|  | 3627 | // 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] | 3628 | mWindow1->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3629 | mWindow1->setFrame(Rect(0, 0, 100, 100)); | 
|  | 3630 |  | 
|  | 3631 | mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2", | 
|  | 3632 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3633 | mWindow2->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3634 | mWindow2->setFrame(Rect(100, 100, 200, 200)); | 
|  | 3635 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3636 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3637 | } | 
|  | 3638 |  | 
|  | 3639 | protected: | 
|  | 3640 | sp<FakeWindowHandle> mWindow1; | 
|  | 3641 | sp<FakeWindowHandle> mWindow2; | 
|  | 3642 |  | 
|  | 3643 | // 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] | 3644 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3645 | vec2 vals = windowInfo->transform.transform(point.x, point.y); | 
|  | 3646 | return {vals.x, vals.y}; | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3647 | } | 
|  | 3648 |  | 
|  | 3649 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, | 
|  | 3650 | const std::vector<PointF>& points) { | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3651 | const std::string name = window->getName(); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3652 | InputEvent* event = window->consume(); | 
|  | 3653 |  | 
|  | 3654 | ASSERT_NE(nullptr, event) << name.c_str() | 
|  | 3655 | << ": consumer should have returned non-NULL event."; | 
|  | 3656 |  | 
|  | 3657 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) | 
|  | 3658 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) | 
|  | 3659 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; | 
|  | 3660 |  | 
|  | 3661 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); | 
|  | 3662 | EXPECT_EQ(expectedAction, motionEvent.getAction()); | 
|  | 3663 |  | 
|  | 3664 | for (size_t i = 0; i < points.size(); i++) { | 
|  | 3665 | float expectedX = points[i].x; | 
|  | 3666 | float expectedY = points[i].y; | 
|  | 3667 |  | 
|  | 3668 | EXPECT_EQ(expectedX, motionEvent.getX(i)) | 
|  | 3669 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() | 
|  | 3670 | << ", got " << motionEvent.getX(i); | 
|  | 3671 | EXPECT_EQ(expectedY, motionEvent.getY(i)) | 
|  | 3672 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() | 
|  | 3673 | << ", got " << motionEvent.getY(i); | 
|  | 3674 | } | 
|  | 3675 | } | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3676 |  | 
|  | 3677 | void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints, | 
|  | 3678 | std::vector<PointF> expectedPoints) { | 
|  | 3679 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3680 | ADISPLAY_ID_DEFAULT, touchedPoints); | 
|  | 3681 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 3682 |  | 
|  | 3683 | // Always consume from window1 since it's the window that has the InputReceiver | 
|  | 3684 | consumeMotionEvent(mWindow1, action, expectedPoints); | 
|  | 3685 | } | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3686 | }; | 
|  | 3687 |  | 
|  | 3688 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { | 
|  | 3689 | // Touch Window 1 | 
|  | 3690 | PointF touchedPoint = {10, 10}; | 
|  | 3691 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3692 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3693 |  | 
|  | 3694 | // Release touch on Window 1 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3695 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3696 |  | 
|  | 3697 | // Touch Window 2 | 
|  | 3698 | touchedPoint = {150, 150}; | 
|  | 3699 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3700 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3701 | } | 
|  | 3702 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3703 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { | 
|  | 3704 | // Set scale value for window2 | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3705 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 3706 |  | 
|  | 3707 | // Touch Window 1 | 
|  | 3708 | PointF touchedPoint = {10, 10}; | 
|  | 3709 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3710 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3711 | // Release touch on Window 1 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3712 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3713 |  | 
|  | 3714 | // Touch Window 2 | 
|  | 3715 | touchedPoint = {150, 150}; | 
|  | 3716 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3717 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
|  | 3718 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3719 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3720 | // Update the transform so rotation is set | 
|  | 3721 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 3722 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
|  | 3723 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3724 | } | 
|  | 3725 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3726 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3727 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 3728 |  | 
|  | 3729 | // Touch Window 1 | 
|  | 3730 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 3731 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3732 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3733 |  | 
|  | 3734 | // Touch Window 2 | 
|  | 3735 | int32_t actionPointerDown = | 
|  | 3736 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3737 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 3738 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
|  | 3739 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3740 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3741 | // Release Window 2 | 
|  | 3742 | int32_t actionPointerUp = | 
|  | 3743 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
|  | 3744 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); | 
|  | 3745 | expectedPoints.pop_back(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3746 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3747 | // Update the transform so rotation is set for Window 2 | 
|  | 3748 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 3749 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
|  | 3750 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3751 | } | 
|  | 3752 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3753 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3754 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 3755 |  | 
|  | 3756 | // Touch Window 1 | 
|  | 3757 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 3758 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3759 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3760 |  | 
|  | 3761 | // Touch Window 2 | 
|  | 3762 | int32_t actionPointerDown = | 
|  | 3763 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3764 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 3765 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3766 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3767 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3768 |  | 
|  | 3769 | // Move both windows | 
|  | 3770 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 3771 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 3772 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 3773 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3774 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3775 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3776 | // Release Window 2 | 
|  | 3777 | int32_t actionPointerUp = | 
|  | 3778 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
|  | 3779 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); | 
|  | 3780 | expectedPoints.pop_back(); | 
|  | 3781 |  | 
|  | 3782 | // Touch Window 2 | 
|  | 3783 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 3784 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
|  | 3785 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
|  | 3786 |  | 
|  | 3787 | // Move both windows | 
|  | 3788 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 3789 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 3790 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 3791 |  | 
|  | 3792 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3793 | } | 
|  | 3794 |  | 
|  | 3795 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { | 
|  | 3796 | mWindow1->setWindowScale(0.5f, 0.5f); | 
|  | 3797 |  | 
|  | 3798 | // Touch Window 1 | 
|  | 3799 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 3800 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3801 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3802 |  | 
|  | 3803 | // Touch Window 2 | 
|  | 3804 | int32_t actionPointerDown = | 
|  | 3805 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3806 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 3807 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3808 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3809 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3810 |  | 
|  | 3811 | // Move both windows | 
|  | 3812 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 3813 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 3814 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 3815 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3816 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3817 | } | 
|  | 3818 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3819 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { | 
|  | 3820 | virtual void SetUp() override { | 
|  | 3821 | InputDispatcherTest::SetUp(); | 
|  | 3822 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3823 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3824 | mApplication->setDispatchingTimeout(20ms); | 
|  | 3825 | mWindow = | 
|  | 3826 | new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 3827 | mWindow->setFrame(Rect(0, 0, 30, 30)); | 
| Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 3828 | mWindow->setDispatchingTimeout(30ms); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3829 | mWindow->setFocusable(true); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3830 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this | 
|  | 3831 | // window. | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3832 | mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3833 |  | 
|  | 3834 | // Set focused application. | 
|  | 3835 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); | 
|  | 3836 |  | 
|  | 3837 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3838 | setFocusedWindow(mWindow); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3839 | mWindow->consumeFocusEvent(true); | 
|  | 3840 | } | 
|  | 3841 |  | 
|  | 3842 | virtual void TearDown() override { | 
|  | 3843 | InputDispatcherTest::TearDown(); | 
|  | 3844 | mWindow.clear(); | 
|  | 3845 | } | 
|  | 3846 |  | 
|  | 3847 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3848 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3849 | sp<FakeWindowHandle> mWindow; | 
|  | 3850 | static constexpr PointF WINDOW_LOCATION = {20, 20}; | 
|  | 3851 |  | 
|  | 3852 | void tapOnWindow() { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3853 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3854 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3855 | WINDOW_LOCATION)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3856 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3857 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3858 | WINDOW_LOCATION)); | 
|  | 3859 | } | 
|  | 3860 | }; | 
|  | 3861 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3862 | // Send a tap and respond, which should not cause an ANR. | 
|  | 3863 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { | 
|  | 3864 | tapOnWindow(); | 
|  | 3865 | mWindow->consumeMotionDown(); | 
|  | 3866 | mWindow->consumeMotionUp(); | 
|  | 3867 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3868 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3869 | } | 
|  | 3870 |  | 
|  | 3871 | // Send a regular key and respond, which should not cause an ANR. | 
|  | 3872 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3873 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3874 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 3875 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3876 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3877 | } | 
|  | 3878 |  | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3879 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { | 
|  | 3880 | mWindow->setFocusable(false); | 
|  | 3881 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3882 | mWindow->consumeFocusEvent(false); | 
|  | 3883 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3884 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3885 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3886 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, | 
|  | 3887 | false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3888 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3889 | // Key will not go to window because we have no focused window. | 
|  | 3890 | // The 'no focused window' ANR timer should start instead. | 
|  | 3891 |  | 
|  | 3892 | // Now, the focused application goes away. | 
|  | 3893 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); | 
|  | 3894 | // The key should get dropped and there should be no ANR. | 
|  | 3895 |  | 
|  | 3896 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3897 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3898 | } | 
|  | 3899 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3900 | // 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] | 3901 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. | 
|  | 3902 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3903 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3904 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3905 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3906 | WINDOW_LOCATION)); | 
|  | 3907 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3908 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN | 
|  | 3909 | ASSERT_TRUE(sequenceNum); | 
|  | 3910 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3911 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3912 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3913 | mWindow->finishEvent(*sequenceNum); | 
|  | 3914 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, | 
|  | 3915 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3916 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3917 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3918 | } | 
|  | 3919 |  | 
|  | 3920 | // Send a key to the app and have the app not respond right away. | 
|  | 3921 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { | 
|  | 3922 | // Inject a key, and don't respond - expect that ANR is called. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3923 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3924 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); | 
|  | 3925 | ASSERT_TRUE(sequenceNum); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3926 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3927 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3928 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3929 | } | 
|  | 3930 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3931 | // We have a focused application, but no focused window | 
|  | 3932 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3933 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3934 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3935 | mWindow->consumeFocusEvent(false); | 
|  | 3936 |  | 
|  | 3937 | // taps on the window work as normal | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3938 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3939 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3940 | WINDOW_LOCATION)); | 
|  | 3941 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); | 
|  | 3942 | mDispatcher->waitForIdle(); | 
|  | 3943 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3944 |  | 
|  | 3945 | // Once a focused event arrives, we get an ANR for this application | 
|  | 3946 | // We specify the injection timeout to be smaller than the application timeout, to ensure that | 
|  | 3947 | // injection times out (instead of failing). | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3948 | const InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3949 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3950 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3951 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3952 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3953 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3954 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3955 | } | 
|  | 3956 |  | 
|  | 3957 | // We have a focused application, but no focused window | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3958 | // Make sure that we don't notify policy twice about the same ANR. | 
|  | 3959 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3960 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3961 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3962 | mWindow->consumeFocusEvent(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3963 |  | 
|  | 3964 | // Once a focused event arrives, we get an ANR for this application | 
|  | 3965 | // We specify the injection timeout to be smaller than the application timeout, to ensure that | 
|  | 3966 | // injection times out (instead of failing). | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3967 | const InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3968 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3969 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3970 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3971 | const std::chrono::duration appTimeout = | 
|  | 3972 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3973 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3974 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3975 | std::this_thread::sleep_for(appTimeout); | 
|  | 3976 | // ANR should not be raised again. It is up to policy to do that if it desires. | 
|  | 3977 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3978 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3979 | // If we now get a focused window, the ANR should stop, but the policy handles that via | 
|  | 3980 | // '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] | 3981 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3982 | } | 
|  | 3983 |  | 
|  | 3984 | // We have a focused application, but no focused window | 
|  | 3985 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3986 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3987 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3988 | mWindow->consumeFocusEvent(false); | 
|  | 3989 |  | 
|  | 3990 | // Once a focused event arrives, we get an ANR for this application | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3991 | const InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3992 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3993 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); | 
|  | 3994 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3995 |  | 
|  | 3996 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3997 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3998 |  | 
|  | 3999 | // Future focused events get dropped right away | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4000 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4001 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4002 | mWindow->assertNoEvents(); | 
|  | 4003 | } | 
|  | 4004 |  | 
|  | 4005 | /** | 
|  | 4006 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the | 
|  | 4007 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. | 
|  | 4008 | * If we process 1 of the events, but ANR on the second event with the same timestamp, | 
|  | 4009 | * the ANR mechanism should still work. | 
|  | 4010 | * | 
|  | 4011 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the | 
|  | 4012 | * DOWN event, while not responding on the second one. | 
|  | 4013 | */ | 
|  | 4014 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { | 
|  | 4015 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 4016 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4017 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, | 
|  | 4018 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 4019 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4020 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4021 |  | 
|  | 4022 | // Now send ACTION_UP, with identical timestamp | 
|  | 4023 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4024 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, | 
|  | 4025 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 4026 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4027 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4028 |  | 
|  | 4029 | // We have now sent down and up. Let's consume first event and then ANR on the second. | 
|  | 4030 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 4031 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4032 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4033 | } | 
|  | 4034 |  | 
|  | 4035 | // If an app is not responding to a key event, gesture monitors should continue to receive | 
|  | 4036 | // new motion events | 
|  | 4037 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) { | 
|  | 4038 | FakeMonitorReceiver monitor = | 
|  | 4039 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, | 
|  | 4040 | true /*isGestureMonitor*/); | 
|  | 4041 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4042 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 4043 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4044 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4045 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4046 |  | 
|  | 4047 | // Stuck on the ACTION_UP | 
|  | 4048 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4049 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4050 |  | 
|  | 4051 | // New tap will go to the gesture monitor, but not to the window | 
|  | 4052 | tapOnWindow(); | 
|  | 4053 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 4054 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 4055 |  | 
|  | 4056 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion | 
|  | 4057 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4058 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4059 | mWindow->assertNoEvents(); | 
|  | 4060 | monitor.assertNoEvents(); | 
|  | 4061 | } | 
|  | 4062 |  | 
|  | 4063 | // If an app is not responding to a motion event, gesture monitors should continue to receive | 
|  | 4064 | // new motion events | 
|  | 4065 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) { | 
|  | 4066 | FakeMonitorReceiver monitor = | 
|  | 4067 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, | 
|  | 4068 | true /*isGestureMonitor*/); | 
|  | 4069 |  | 
|  | 4070 | tapOnWindow(); | 
|  | 4071 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 4072 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 4073 |  | 
|  | 4074 | mWindow->consumeMotionDown(); | 
|  | 4075 | // Stuck on the ACTION_UP | 
|  | 4076 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4077 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4078 |  | 
|  | 4079 | // New tap will go to the gesture monitor, but not to the window | 
|  | 4080 | tapOnWindow(); | 
|  | 4081 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 4082 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 4083 |  | 
|  | 4084 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion | 
|  | 4085 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4086 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4087 | mWindow->assertNoEvents(); | 
|  | 4088 | monitor.assertNoEvents(); | 
|  | 4089 | } | 
|  | 4090 |  | 
|  | 4091 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to | 
|  | 4092 | // process events, you don't get an anr. When the window later becomes unresponsive again, you | 
|  | 4093 | // get an ANR again. | 
|  | 4094 | // 1. tap -> block on ACTION_UP -> receive ANR | 
|  | 4095 | // 2. consume all pending events (= queue becomes healthy again) | 
|  | 4096 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time | 
|  | 4097 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { | 
|  | 4098 | tapOnWindow(); | 
|  | 4099 |  | 
|  | 4100 | mWindow->consumeMotionDown(); | 
|  | 4101 | // Block on ACTION_UP | 
|  | 4102 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4103 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4104 | mWindow->consumeMotionUp(); // Now the connection should be healthy again | 
|  | 4105 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4106 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4107 | mWindow->assertNoEvents(); | 
|  | 4108 |  | 
|  | 4109 | tapOnWindow(); | 
|  | 4110 | mWindow->consumeMotionDown(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4111 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4112 | mWindow->consumeMotionUp(); | 
|  | 4113 |  | 
|  | 4114 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4115 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4116 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4117 | mWindow->assertNoEvents(); | 
|  | 4118 | } | 
|  | 4119 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4120 | // If a connection remains unresponsive for a while, make sure policy is only notified once about | 
|  | 4121 | // it. | 
|  | 4122 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4123 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4124 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4125 | WINDOW_LOCATION)); | 
|  | 4126 |  | 
|  | 4127 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4128 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4129 | std::this_thread::sleep_for(windowTimeout); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4130 | // 'notifyConnectionUnresponsive' should only be called once per connection | 
|  | 4131 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4132 | // 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] | 4133 | mWindow->consumeMotionDown(); | 
|  | 4134 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, | 
|  | 4135 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 4136 | mWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4137 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4138 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4139 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4140 | } | 
|  | 4141 |  | 
|  | 4142 | /** | 
|  | 4143 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 4144 | * not to to the focused window until the motion is processed. | 
|  | 4145 | * | 
|  | 4146 | * Warning!!! | 
|  | 4147 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT | 
|  | 4148 | * and the injection timeout that we specify when injecting the key. | 
|  | 4149 | * We must have the injection timeout (10ms) be smaller than | 
|  | 4150 | *  KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). | 
|  | 4151 | * | 
|  | 4152 | * If that value changes, this test should also change. | 
|  | 4153 | */ | 
|  | 4154 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { | 
|  | 4155 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering | 
|  | 4156 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 4157 |  | 
|  | 4158 | tapOnWindow(); | 
|  | 4159 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); | 
|  | 4160 | ASSERT_TRUE(downSequenceNum); | 
|  | 4161 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); | 
|  | 4162 | ASSERT_TRUE(upSequenceNum); | 
|  | 4163 | // Don't finish the events yet, and send a key | 
|  | 4164 | // Injection will "succeed" because we will eventually give up and send the key to the focused | 
|  | 4165 | // window even if motions are still being processed. But because the injection timeout is short, | 
|  | 4166 | // we will receive INJECTION_TIMED_OUT as the result. | 
|  | 4167 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4168 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4169 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4170 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); | 
|  | 4171 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4172 | // Key will not be sent to the window, yet, because the window is still processing events | 
|  | 4173 | // and the key remains pending, waiting for the touch events to be processed | 
|  | 4174 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); | 
|  | 4175 | ASSERT_FALSE(keySequenceNum); | 
|  | 4176 |  | 
|  | 4177 | std::this_thread::sleep_for(500ms); | 
|  | 4178 | // if we wait long enough though, dispatcher will give up, and still send the key | 
|  | 4179 | // to the focused window, even though we have not yet finished the motion event | 
|  | 4180 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 4181 | mWindow->finishEvent(*downSequenceNum); | 
|  | 4182 | mWindow->finishEvent(*upSequenceNum); | 
|  | 4183 | } | 
|  | 4184 |  | 
|  | 4185 | /** | 
|  | 4186 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 4187 | * not go to the focused window until the motion is processed. | 
|  | 4188 | * If then a new motion comes in, then the pending key event should be going to the currently | 
|  | 4189 | * focused window right away. | 
|  | 4190 | */ | 
|  | 4191 | TEST_F(InputDispatcherSingleWindowAnr, | 
|  | 4192 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { | 
|  | 4193 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering | 
|  | 4194 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 4195 |  | 
|  | 4196 | tapOnWindow(); | 
|  | 4197 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); | 
|  | 4198 | ASSERT_TRUE(downSequenceNum); | 
|  | 4199 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); | 
|  | 4200 | ASSERT_TRUE(upSequenceNum); | 
|  | 4201 | // Don't finish the events yet, and send a key | 
|  | 4202 | // Injection is async, so it will succeed | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4203 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4204 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4205 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4206 | // At this point, key is still pending, and should not be sent to the application yet. | 
|  | 4207 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); | 
|  | 4208 | ASSERT_FALSE(keySequenceNum); | 
|  | 4209 |  | 
|  | 4210 | // Now tap down again. It should cause the pending key to go to the focused window right away. | 
|  | 4211 | tapOnWindow(); | 
|  | 4212 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd | 
|  | 4213 | // the other events yet. We can finish events in any order. | 
|  | 4214 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN | 
|  | 4215 | mWindow->finishEvent(*upSequenceNum);   // first tap's ACTION_UP | 
|  | 4216 | mWindow->consumeMotionDown(); | 
|  | 4217 | mWindow->consumeMotionUp(); | 
|  | 4218 | mWindow->assertNoEvents(); | 
|  | 4219 | } | 
|  | 4220 |  | 
|  | 4221 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { | 
|  | 4222 | virtual void SetUp() override { | 
|  | 4223 | InputDispatcherTest::SetUp(); | 
|  | 4224 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4225 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4226 | mApplication->setDispatchingTimeout(10ms); | 
|  | 4227 | mUnfocusedWindow = | 
|  | 4228 | new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT); | 
|  | 4229 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); | 
|  | 4230 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this | 
|  | 4231 | // window. | 
|  | 4232 | // 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] | 4233 | mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 4234 | WindowInfo::Flag::WATCH_OUTSIDE_TOUCH | | 
|  | 4235 | WindowInfo::Flag::SPLIT_TOUCH); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4236 |  | 
|  | 4237 | mFocusedWindow = | 
|  | 4238 | new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 4239 | mFocusedWindow->setDispatchingTimeout(30ms); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4240 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4241 | mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4242 |  | 
|  | 4243 | // Set focused application. | 
|  | 4244 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4245 | mFocusedWindow->setFocusable(true); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4246 |  | 
|  | 4247 | // Expect one focus window exist in display. | 
|  | 4248 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4249 | setFocusedWindow(mFocusedWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4250 | mFocusedWindow->consumeFocusEvent(true); | 
|  | 4251 | } | 
|  | 4252 |  | 
|  | 4253 | virtual void TearDown() override { | 
|  | 4254 | InputDispatcherTest::TearDown(); | 
|  | 4255 |  | 
|  | 4256 | mUnfocusedWindow.clear(); | 
|  | 4257 | mFocusedWindow.clear(); | 
|  | 4258 | } | 
|  | 4259 |  | 
|  | 4260 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4261 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4262 | sp<FakeWindowHandle> mUnfocusedWindow; | 
|  | 4263 | sp<FakeWindowHandle> mFocusedWindow; | 
|  | 4264 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; | 
|  | 4265 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; | 
|  | 4266 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; | 
|  | 4267 |  | 
|  | 4268 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } | 
|  | 4269 |  | 
|  | 4270 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } | 
|  | 4271 |  | 
|  | 4272 | private: | 
|  | 4273 | void tap(const PointF& location) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4274 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4275 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4276 | location)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4277 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4278 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4279 | location)); | 
|  | 4280 | } | 
|  | 4281 | }; | 
|  | 4282 |  | 
|  | 4283 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout | 
|  | 4284 | // should be ANR'd first. | 
|  | 4285 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4286 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4287 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4288 | FOCUSED_WINDOW_LOCATION)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4289 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4290 | mFocusedWindow->consumeMotionDown(); | 
|  | 4291 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
|  | 4292 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 4293 | // We consumed all events, so no ANR | 
|  | 4294 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4295 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4296 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4297 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4298 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4299 | FOCUSED_WINDOW_LOCATION)); | 
|  | 4300 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 4301 | ASSERT_TRUE(unfocusedSequenceNum); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4302 |  | 
|  | 4303 | const std::chrono::duration timeout = | 
|  | 4304 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4305 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4306 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event | 
|  | 4307 | // sequence to make it consistent | 
|  | 4308 | mFocusedWindow->consumeMotionCancel(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4309 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4310 | mFocusedWindow->consumeMotionDown(); | 
|  | 4311 | // This cancel is generated because the connection was unresponsive | 
|  | 4312 | mFocusedWindow->consumeMotionCancel(); | 
|  | 4313 | mFocusedWindow->assertNoEvents(); | 
|  | 4314 | mUnfocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4315 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4316 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4317 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4318 | } | 
|  | 4319 |  | 
|  | 4320 | // If we have 2 windows with identical timeouts that are both unresponsive, | 
|  | 4321 | // it doesn't matter which order they should have ANR. | 
|  | 4322 | // But we should receive ANR for both. | 
|  | 4323 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { | 
|  | 4324 | // Set the timeout for unfocused window to match the focused window | 
|  | 4325 | mUnfocusedWindow->setDispatchingTimeout(10ms); | 
|  | 4326 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
|  | 4327 |  | 
|  | 4328 | tapOnFocusedWindow(); | 
|  | 4329 | // 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] | 4330 | sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms); | 
|  | 4331 | sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4332 |  | 
|  | 4333 | // 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] | 4334 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || | 
|  | 4335 | mFocusedWindow->getToken() == anrConnectionToken2); | 
|  | 4336 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || | 
|  | 4337 | mUnfocusedWindow->getToken() == anrConnectionToken2); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4338 |  | 
|  | 4339 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4340 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4341 |  | 
|  | 4342 | mFocusedWindow->consumeMotionDown(); | 
|  | 4343 | mFocusedWindow->consumeMotionUp(); | 
|  | 4344 | mUnfocusedWindow->consumeMotionOutside(); | 
|  | 4345 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4346 | sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken(); | 
|  | 4347 | sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4348 |  | 
|  | 4349 | // Both applications should be marked as responsive, in any order | 
|  | 4350 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || | 
|  | 4351 | mFocusedWindow->getToken() == responsiveToken2); | 
|  | 4352 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || | 
|  | 4353 | mUnfocusedWindow->getToken() == responsiveToken2); | 
|  | 4354 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4355 | } | 
|  | 4356 |  | 
|  | 4357 | // If a window is already not responding, the second tap on the same window should be ignored. | 
|  | 4358 | // We should also log an error to account for the dropped event (not tested here). | 
|  | 4359 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. | 
|  | 4360 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { | 
|  | 4361 | tapOnFocusedWindow(); | 
|  | 4362 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
|  | 4363 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 4364 | // Receive the events, but don't respond | 
|  | 4365 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN | 
|  | 4366 | ASSERT_TRUE(downEventSequenceNum); | 
|  | 4367 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP | 
|  | 4368 | ASSERT_TRUE(upEventSequenceNum); | 
|  | 4369 | const std::chrono::duration timeout = | 
|  | 4370 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4371 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4372 |  | 
|  | 4373 | // Tap once again | 
|  | 4374 | // 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] | 4375 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4376 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4377 | FOCUSED_WINDOW_LOCATION)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4378 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4379 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4380 | FOCUSED_WINDOW_LOCATION)); | 
|  | 4381 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a | 
|  | 4382 | // valid touch target | 
|  | 4383 | mUnfocusedWindow->assertNoEvents(); | 
|  | 4384 |  | 
|  | 4385 | // Consume the first tap | 
|  | 4386 | mFocusedWindow->finishEvent(*downEventSequenceNum); | 
|  | 4387 | mFocusedWindow->finishEvent(*upEventSequenceNum); | 
|  | 4388 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4389 | // The second tap did not go to the focused window | 
|  | 4390 | mFocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4391 | // Since all events are finished, connection should be deemed healthy again | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4392 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4393 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4394 | } | 
|  | 4395 |  | 
|  | 4396 | // If you tap outside of all windows, there will not be ANR | 
|  | 4397 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4398 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4399 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4400 | LOCATION_OUTSIDE_ALL_WINDOWS)); | 
|  | 4401 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4402 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4403 | } | 
|  | 4404 |  | 
|  | 4405 | // Since the focused window is paused, tapping on it should not produce any events | 
|  | 4406 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { | 
|  | 4407 | mFocusedWindow->setPaused(true); | 
|  | 4408 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
|  | 4409 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4410 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4411 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4412 | FOCUSED_WINDOW_LOCATION)); | 
|  | 4413 |  | 
|  | 4414 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); | 
|  | 4415 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4416 | // Should not ANR because the window is paused, and touches shouldn't go to it | 
|  | 4417 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4418 |  | 
|  | 4419 | mFocusedWindow->assertNoEvents(); | 
|  | 4420 | mUnfocusedWindow->assertNoEvents(); | 
|  | 4421 | } | 
|  | 4422 |  | 
|  | 4423 | /** | 
|  | 4424 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 4425 | * not to to the focused window until the motion is processed. | 
|  | 4426 | * If a different window becomes focused at this time, the key should go to that window instead. | 
|  | 4427 | * | 
|  | 4428 | * Warning!!! | 
|  | 4429 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT | 
|  | 4430 | * and the injection timeout that we specify when injecting the key. | 
|  | 4431 | * We must have the injection timeout (10ms) be smaller than | 
|  | 4432 | *  KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). | 
|  | 4433 | * | 
|  | 4434 | * If that value changes, this test should also change. | 
|  | 4435 | */ | 
|  | 4436 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { | 
|  | 4437 | // Set a long ANR timeout to prevent it from triggering | 
|  | 4438 | mFocusedWindow->setDispatchingTimeout(2s); | 
|  | 4439 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 4440 |  | 
|  | 4441 | tapOnUnfocusedWindow(); | 
|  | 4442 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 4443 | ASSERT_TRUE(downSequenceNum); | 
|  | 4444 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 4445 | ASSERT_TRUE(upSequenceNum); | 
|  | 4446 | // Don't finish the events yet, and send a key | 
|  | 4447 | // Injection will succeed because we will eventually give up and send the key to the focused | 
|  | 4448 | // window even if motions are still being processed. | 
|  | 4449 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4450 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4451 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4452 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); | 
|  | 4453 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4454 | // Key will not be sent to the window, yet, because the window is still processing events | 
|  | 4455 | // and the key remains pending, waiting for the touch events to be processed | 
|  | 4456 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); | 
|  | 4457 | ASSERT_FALSE(keySequenceNum); | 
|  | 4458 |  | 
|  | 4459 | // 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] | 4460 | mFocusedWindow->setFocusable(false); | 
|  | 4461 | mUnfocusedWindow->setFocusable(true); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4462 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4463 | setFocusedWindow(mUnfocusedWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4464 |  | 
|  | 4465 | // Focus events should precede the key events | 
|  | 4466 | mUnfocusedWindow->consumeFocusEvent(true); | 
|  | 4467 | mFocusedWindow->consumeFocusEvent(false); | 
|  | 4468 |  | 
|  | 4469 | // Finish the tap events, which should unblock dispatcher | 
|  | 4470 | mUnfocusedWindow->finishEvent(*downSequenceNum); | 
|  | 4471 | mUnfocusedWindow->finishEvent(*upSequenceNum); | 
|  | 4472 |  | 
|  | 4473 | // Now that all queues are cleared and no backlog in the connections, the key event | 
|  | 4474 | // can finally go to the newly focused "mUnfocusedWindow". | 
|  | 4475 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 4476 | mFocusedWindow->assertNoEvents(); | 
|  | 4477 | mUnfocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4478 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4479 | } | 
|  | 4480 |  | 
|  | 4481 | // When the touch stream is split across 2 windows, and one of them does not respond, | 
|  | 4482 | // then ANR should be raised and the touch should be canceled for the unresponsive window. | 
|  | 4483 | // The other window should not be affected by that. | 
|  | 4484 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { | 
|  | 4485 | // Touch Window 1 | 
|  | 4486 | NotifyMotionArgs motionArgs = | 
|  | 4487 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4488 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); | 
|  | 4489 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4490 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
|  | 4491 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 4492 |  | 
|  | 4493 | // Touch Window 2 | 
|  | 4494 | int32_t actionPointerDown = | 
|  | 4495 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
|  | 4496 |  | 
|  | 4497 | motionArgs = | 
|  | 4498 | generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4499 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); | 
|  | 4500 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4501 |  | 
|  | 4502 | const std::chrono::duration timeout = | 
|  | 4503 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4504 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4505 |  | 
|  | 4506 | mUnfocusedWindow->consumeMotionDown(); | 
|  | 4507 | mFocusedWindow->consumeMotionDown(); | 
|  | 4508 | // Focused window may or may not receive ACTION_MOVE | 
|  | 4509 | // But it should definitely receive ACTION_CANCEL due to the ANR | 
|  | 4510 | InputEvent* event; | 
|  | 4511 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); | 
|  | 4512 | ASSERT_TRUE(moveOrCancelSequenceNum); | 
|  | 4513 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); | 
|  | 4514 | ASSERT_NE(nullptr, event); | 
|  | 4515 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); | 
|  | 4516 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 4517 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { | 
|  | 4518 | mFocusedWindow->consumeMotionCancel(); | 
|  | 4519 | } else { | 
|  | 4520 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); | 
|  | 4521 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4522 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4523 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4524 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4525 | mUnfocusedWindow->assertNoEvents(); | 
|  | 4526 | mFocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4527 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4528 | } | 
|  | 4529 |  | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 4530 | /** | 
|  | 4531 | * If we have no focused window, and a key comes in, we start the ANR timer. | 
|  | 4532 | * The focused application should add a focused window before the timer runs out to prevent ANR. | 
|  | 4533 | * | 
|  | 4534 | * If the user touches another application during this time, the key should be dropped. | 
|  | 4535 | * Next, if a new focused window comes in, without toggling the focused application, | 
|  | 4536 | * then no ANR should occur. | 
|  | 4537 | * | 
|  | 4538 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', | 
|  | 4539 | * but in some cases the policy may not update the focused application. | 
|  | 4540 | */ | 
|  | 4541 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { | 
|  | 4542 | std::shared_ptr<FakeApplicationHandle> focusedApplication = | 
|  | 4543 | std::make_shared<FakeApplicationHandle>(); | 
|  | 4544 | focusedApplication->setDispatchingTimeout(60ms); | 
|  | 4545 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); | 
|  | 4546 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. | 
|  | 4547 | mFocusedWindow->setFocusable(false); | 
|  | 4548 |  | 
|  | 4549 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 4550 | mFocusedWindow->consumeFocusEvent(false); | 
|  | 4551 |  | 
|  | 4552 | // Send a key. The ANR timer should start because there is no focused window. | 
|  | 4553 | // 'focusedApplication' will get blamed if this timer completes. | 
|  | 4554 | // 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] | 4555 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 4556 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4557 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, | 
|  | 4558 | false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4559 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 4560 |  | 
|  | 4561 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, | 
|  | 4562 | // then the injected touches won't cause the focused event to get dropped. | 
|  | 4563 | // The dispatcher only checks for whether the queue should be pruned upon queueing. | 
|  | 4564 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would | 
|  | 4565 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. | 
|  | 4566 | // For this test, it means that the key would get delivered to the window once it becomes | 
|  | 4567 | // focused. | 
|  | 4568 | std::this_thread::sleep_for(10ms); | 
|  | 4569 |  | 
|  | 4570 | // Touch unfocused window. This should force the pending key to get dropped. | 
|  | 4571 | NotifyMotionArgs motionArgs = | 
|  | 4572 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4573 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); | 
|  | 4574 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4575 |  | 
|  | 4576 | // We do not consume the motion right away, because that would require dispatcher to first | 
|  | 4577 | // process (== drop) the key event, and by that time, ANR will be raised. | 
|  | 4578 | // Set the focused window first. | 
|  | 4579 | mFocusedWindow->setFocusable(true); | 
|  | 4580 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 4581 | setFocusedWindow(mFocusedWindow); | 
|  | 4582 | mFocusedWindow->consumeFocusEvent(true); | 
|  | 4583 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs | 
|  | 4584 | // to another application. This could be a bug / behaviour in the policy. | 
|  | 4585 |  | 
|  | 4586 | mUnfocusedWindow->consumeMotionDown(); | 
|  | 4587 |  | 
|  | 4588 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4589 | // Should not ANR because we actually have a focused window. It was just added too slowly. | 
|  | 4590 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); | 
|  | 4591 | } | 
|  | 4592 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4593 | // These tests ensure we cannot send touch events to a window that's positioned behind a window | 
|  | 4594 | // that has feature NO_INPUT_CHANNEL. | 
|  | 4595 | // Layout: | 
|  | 4596 | //   Top (closest to user) | 
|  | 4597 | //       mNoInputWindow (above all windows) | 
|  | 4598 | //       mBottomWindow | 
|  | 4599 | //   Bottom (furthest from user) | 
|  | 4600 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { | 
|  | 4601 | virtual void SetUp() override { | 
|  | 4602 | InputDispatcherTest::SetUp(); | 
|  | 4603 |  | 
|  | 4604 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
|  | 4605 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, | 
|  | 4606 | "Window without input channel", ADISPLAY_ID_DEFAULT, | 
|  | 4607 | std::make_optional<sp<IBinder>>(nullptr) /*token*/); | 
|  | 4608 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4609 | mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4610 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 4611 | // It's perfectly valid for this window to not have an associated input channel | 
|  | 4612 |  | 
|  | 4613 | mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window", | 
|  | 4614 | ADISPLAY_ID_DEFAULT); | 
|  | 4615 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 4616 |  | 
|  | 4617 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); | 
|  | 4618 | } | 
|  | 4619 |  | 
|  | 4620 | protected: | 
|  | 4621 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
|  | 4622 | sp<FakeWindowHandle> mNoInputWindow; | 
|  | 4623 | sp<FakeWindowHandle> mBottomWindow; | 
|  | 4624 | }; | 
|  | 4625 |  | 
|  | 4626 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { | 
|  | 4627 | PointF touchedPoint = {10, 10}; | 
|  | 4628 |  | 
|  | 4629 | NotifyMotionArgs motionArgs = | 
|  | 4630 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4631 | ADISPLAY_ID_DEFAULT, {touchedPoint}); | 
|  | 4632 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4633 |  | 
|  | 4634 | mNoInputWindow->assertNoEvents(); | 
|  | 4635 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have | 
|  | 4636 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, | 
|  | 4637 | // and therefore should prevent mBottomWindow from receiving touches | 
|  | 4638 | mBottomWindow->assertNoEvents(); | 
|  | 4639 | } | 
|  | 4640 |  | 
|  | 4641 | /** | 
|  | 4642 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, | 
|  | 4643 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. | 
|  | 4644 | */ | 
|  | 4645 | TEST_F(InputDispatcherMultiWindowOcclusionTests, | 
|  | 4646 | NoInputChannelFeature_DropsTouchesWithValidChannel) { | 
|  | 4647 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, | 
|  | 4648 | "Window with input channel and NO_INPUT_CHANNEL", | 
|  | 4649 | ADISPLAY_ID_DEFAULT); | 
|  | 4650 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4651 | mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4652 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 4653 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); | 
|  | 4654 |  | 
|  | 4655 | PointF touchedPoint = {10, 10}; | 
|  | 4656 |  | 
|  | 4657 | NotifyMotionArgs motionArgs = | 
|  | 4658 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4659 | ADISPLAY_ID_DEFAULT, {touchedPoint}); | 
|  | 4660 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4661 |  | 
|  | 4662 | mNoInputWindow->assertNoEvents(); | 
|  | 4663 | mBottomWindow->assertNoEvents(); | 
|  | 4664 | } | 
|  | 4665 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4666 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { | 
|  | 4667 | protected: | 
|  | 4668 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 4669 | sp<FakeWindowHandle> mWindow; | 
|  | 4670 | sp<FakeWindowHandle> mMirror; | 
|  | 4671 |  | 
|  | 4672 | virtual void SetUp() override { | 
|  | 4673 | InputDispatcherTest::SetUp(); | 
|  | 4674 | mApp = std::make_shared<FakeApplicationHandle>(); | 
|  | 4675 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 4676 | mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT, | 
|  | 4677 | mWindow->getToken()); | 
|  | 4678 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
|  | 4679 | mWindow->setFocusable(true); | 
|  | 4680 | mMirror->setFocusable(true); | 
|  | 4681 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4682 | } | 
|  | 4683 | }; | 
|  | 4684 |  | 
|  | 4685 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { | 
|  | 4686 | // Request focus on a mirrored window | 
|  | 4687 | setFocusedWindow(mMirror); | 
|  | 4688 |  | 
|  | 4689 | // window gets focused | 
|  | 4690 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4691 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4692 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4693 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 4694 | } | 
|  | 4695 |  | 
|  | 4696 | // A focused & mirrored window remains focused only if the window and its mirror are both | 
|  | 4697 | // focusable. | 
|  | 4698 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { | 
|  | 4699 | setFocusedWindow(mMirror); | 
|  | 4700 |  | 
|  | 4701 | // window gets focused | 
|  | 4702 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4703 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4704 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4705 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4706 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4707 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4708 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4709 |  | 
|  | 4710 | mMirror->setFocusable(false); | 
|  | 4711 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4712 |  | 
|  | 4713 | // window loses focus since one of the windows associated with the token in not focusable | 
|  | 4714 | mWindow->consumeFocusEvent(false); | 
|  | 4715 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4716 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 4717 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4718 | mWindow->assertNoEvents(); | 
|  | 4719 | } | 
|  | 4720 |  | 
|  | 4721 | // A focused & mirrored window remains focused until the window and its mirror both become | 
|  | 4722 | // invisible. | 
|  | 4723 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { | 
|  | 4724 | setFocusedWindow(mMirror); | 
|  | 4725 |  | 
|  | 4726 | // window gets focused | 
|  | 4727 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4728 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4729 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4730 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4731 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4732 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4733 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4734 |  | 
|  | 4735 | mMirror->setVisible(false); | 
|  | 4736 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4737 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4738 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4739 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4740 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4741 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4742 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4743 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4744 |  | 
|  | 4745 | mWindow->setVisible(false); | 
|  | 4746 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4747 |  | 
|  | 4748 | // window loses focus only after all windows associated with the token become invisible. | 
|  | 4749 | mWindow->consumeFocusEvent(false); | 
|  | 4750 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4751 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 4752 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4753 | mWindow->assertNoEvents(); | 
|  | 4754 | } | 
|  | 4755 |  | 
|  | 4756 | // A focused & mirrored window remains focused until both windows are removed. | 
|  | 4757 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { | 
|  | 4758 | setFocusedWindow(mMirror); | 
|  | 4759 |  | 
|  | 4760 | // window gets focused | 
|  | 4761 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4762 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4763 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4764 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4765 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4766 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4767 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4768 |  | 
|  | 4769 | // single window is removed but the window token remains focused | 
|  | 4770 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); | 
|  | 4771 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4772 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4773 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4774 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4775 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4776 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4777 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4778 |  | 
|  | 4779 | // Both windows are removed | 
|  | 4780 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); | 
|  | 4781 | mWindow->consumeFocusEvent(false); | 
|  | 4782 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4783 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 4784 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4785 | mWindow->assertNoEvents(); | 
|  | 4786 | } | 
|  | 4787 |  | 
|  | 4788 | // Focus request can be pending until one window becomes visible. | 
|  | 4789 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { | 
|  | 4790 | // Request focus on an invisible mirror. | 
|  | 4791 | mWindow->setVisible(false); | 
|  | 4792 | mMirror->setVisible(false); | 
|  | 4793 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4794 | setFocusedWindow(mMirror); | 
|  | 4795 |  | 
|  | 4796 | // Injected key goes to pending queue. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4797 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4798 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4799 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4800 |  | 
|  | 4801 | mMirror->setVisible(true); | 
|  | 4802 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4803 |  | 
|  | 4804 | // window gets focused | 
|  | 4805 | mWindow->consumeFocusEvent(true); | 
|  | 4806 | // window gets the pending key event | 
|  | 4807 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 4808 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4809 |  | 
|  | 4810 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { | 
|  | 4811 | protected: | 
|  | 4812 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 4813 | sp<FakeWindowHandle> mWindow; | 
|  | 4814 | sp<FakeWindowHandle> mSecondWindow; | 
|  | 4815 |  | 
|  | 4816 | void SetUp() override { | 
|  | 4817 | InputDispatcherTest::SetUp(); | 
|  | 4818 | mApp = std::make_shared<FakeApplicationHandle>(); | 
|  | 4819 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 4820 | mWindow->setFocusable(true); | 
|  | 4821 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); | 
|  | 4822 | mSecondWindow->setFocusable(true); | 
|  | 4823 |  | 
|  | 4824 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
|  | 4825 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); | 
|  | 4826 |  | 
|  | 4827 | setFocusedWindow(mWindow); | 
|  | 4828 | mWindow->consumeFocusEvent(true); | 
|  | 4829 | } | 
|  | 4830 |  | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4831 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { | 
|  | 4832 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4833 | mDispatcher->notifyPointerCaptureChanged(&args); | 
|  | 4834 | } | 
|  | 4835 |  | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4836 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, | 
|  | 4837 | bool enabled) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4838 | mDispatcher->requestPointerCapture(window->getToken(), enabled); | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4839 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); | 
|  | 4840 | notifyPointerCaptureChanged(request); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4841 | window->consumeCaptureEvent(enabled); | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4842 | return request; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4843 | } | 
|  | 4844 | }; | 
|  | 4845 |  | 
|  | 4846 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { | 
|  | 4847 | // Ensure that capture cannot be obtained for unfocused windows. | 
|  | 4848 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); | 
|  | 4849 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 4850 | mSecondWindow->assertNoEvents(); | 
|  | 4851 |  | 
|  | 4852 | // Ensure that capture can be enabled from the focus window. | 
|  | 4853 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 4854 |  | 
|  | 4855 | // Ensure that capture cannot be disabled from a window that does not have capture. | 
|  | 4856 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); | 
|  | 4857 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 4858 |  | 
|  | 4859 | // Ensure that capture can be disabled from the window with capture. | 
|  | 4860 | requestAndVerifyPointerCapture(mWindow, false); | 
|  | 4861 | } | 
|  | 4862 |  | 
|  | 4863 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4864 | auto request = requestAndVerifyPointerCapture(mWindow, true); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4865 |  | 
|  | 4866 | setFocusedWindow(mSecondWindow); | 
|  | 4867 |  | 
|  | 4868 | // Ensure that the capture disabled event was sent first. | 
|  | 4869 | mWindow->consumeCaptureEvent(false); | 
|  | 4870 | mWindow->consumeFocusEvent(false); | 
|  | 4871 | mSecondWindow->consumeFocusEvent(true); | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4872 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4873 |  | 
|  | 4874 | // Ensure that additional state changes from InputReader are not sent to the window. | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4875 | notifyPointerCaptureChanged({}); | 
|  | 4876 | notifyPointerCaptureChanged(request); | 
|  | 4877 | notifyPointerCaptureChanged({}); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4878 | mWindow->assertNoEvents(); | 
|  | 4879 | mSecondWindow->assertNoEvents(); | 
|  | 4880 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 4881 | } | 
|  | 4882 |  | 
|  | 4883 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4884 | auto request = requestAndVerifyPointerCapture(mWindow, true); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4885 |  | 
|  | 4886 | // InputReader unexpectedly disables and enables pointer capture. | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4887 | notifyPointerCaptureChanged({}); | 
|  | 4888 | notifyPointerCaptureChanged(request); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4889 |  | 
|  | 4890 | // Ensure that Pointer Capture is disabled. | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4891 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4892 | mWindow->consumeCaptureEvent(false); | 
|  | 4893 | mWindow->assertNoEvents(); | 
|  | 4894 | } | 
|  | 4895 |  | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 4896 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { | 
|  | 4897 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 4898 |  | 
|  | 4899 | // The first window loses focus. | 
|  | 4900 | setFocusedWindow(mSecondWindow); | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4901 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 4902 | mWindow->consumeCaptureEvent(false); | 
|  | 4903 |  | 
|  | 4904 | // Request Pointer Capture from the second window before the notification from InputReader | 
|  | 4905 | // arrives. | 
|  | 4906 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4907 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 4908 |  | 
|  | 4909 | // InputReader notifies Pointer Capture was disabled (because of the focus change). | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4910 | notifyPointerCaptureChanged({}); | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 4911 |  | 
|  | 4912 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4913 | notifyPointerCaptureChanged(request); | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 4914 |  | 
|  | 4915 | mSecondWindow->consumeFocusEvent(true); | 
|  | 4916 | mSecondWindow->consumeCaptureEvent(true); | 
|  | 4917 | } | 
|  | 4918 |  | 
| Prabir Pradhan | ac483a6 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4919 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { | 
|  | 4920 | // App repeatedly enables and disables capture. | 
|  | 4921 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); | 
|  | 4922 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); | 
|  | 4923 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); | 
|  | 4924 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
|  | 4925 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); | 
|  | 4926 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); | 
|  | 4927 |  | 
|  | 4928 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the | 
|  | 4929 | // first request is now stale, this should do nothing. | 
|  | 4930 | notifyPointerCaptureChanged(firstRequest); | 
|  | 4931 | mWindow->assertNoEvents(); | 
|  | 4932 |  | 
|  | 4933 | // InputReader notifies that the second request was enabled. | 
|  | 4934 | notifyPointerCaptureChanged(secondRequest); | 
|  | 4935 | mWindow->consumeCaptureEvent(true); | 
|  | 4936 | } | 
|  | 4937 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4938 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { | 
|  | 4939 | protected: | 
|  | 4940 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4941 |  | 
|  | 4942 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; | 
|  | 4943 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); | 
|  | 4944 |  | 
|  | 4945 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; | 
|  | 4946 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); | 
|  | 4947 |  | 
|  | 4948 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold | 
|  | 4949 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; | 
|  | 4950 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); | 
|  | 4951 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < | 
|  | 4952 | MAXIMUM_OBSCURING_OPACITY); | 
|  | 4953 |  | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4954 | static const int32_t TOUCHED_APP_UID = 10001; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4955 | static const int32_t APP_B_UID = 10002; | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4956 | static const int32_t APP_C_UID = 10003; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4957 |  | 
|  | 4958 | sp<FakeWindowHandle> mTouchWindow; | 
|  | 4959 |  | 
|  | 4960 | virtual void SetUp() override { | 
|  | 4961 | InputDispatcherTest::SetUp(); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4962 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4963 | mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK); | 
|  | 4964 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); | 
|  | 4965 | } | 
|  | 4966 |  | 
|  | 4967 | virtual void TearDown() override { | 
|  | 4968 | InputDispatcherTest::TearDown(); | 
|  | 4969 | mTouchWindow.clear(); | 
|  | 4970 | } | 
|  | 4971 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4972 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, | 
|  | 4973 | float alpha = 1.0f) { | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4974 | sp<FakeWindowHandle> window = getWindow(uid, name); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4975 | window->setFlags(WindowInfo::Flag::NOT_TOUCHABLE); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4976 | window->setTouchOcclusionMode(mode); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4977 | window->setAlpha(alpha); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4978 | return window; | 
|  | 4979 | } | 
|  | 4980 |  | 
|  | 4981 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { | 
|  | 4982 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); | 
|  | 4983 | sp<FakeWindowHandle> window = | 
|  | 4984 | new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); | 
|  | 4985 | // Generate an arbitrary PID based on the UID | 
|  | 4986 | window->setOwnerInfo(1777 + (uid % 10000), uid); | 
|  | 4987 | return window; | 
|  | 4988 | } | 
|  | 4989 |  | 
|  | 4990 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { | 
|  | 4991 | NotifyMotionArgs args = | 
|  | 4992 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4993 | ADISPLAY_ID_DEFAULT, points); | 
|  | 4994 | mDispatcher->notifyMotion(&args); | 
|  | 4995 | } | 
|  | 4996 | }; | 
|  | 4997 |  | 
|  | 4998 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4999 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5000 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5001 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5002 |  | 
|  | 5003 | touch(); | 
|  | 5004 |  | 
|  | 5005 | mTouchWindow->assertNoEvents(); | 
|  | 5006 | } | 
|  | 5007 |  | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5008 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5009 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { | 
|  | 5010 | const sp<FakeWindowHandle>& w = | 
|  | 5011 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); | 
|  | 5012 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5013 |  | 
|  | 5014 | touch(); | 
|  | 5015 |  | 
|  | 5016 | mTouchWindow->assertNoEvents(); | 
|  | 5017 | } | 
|  | 5018 |  | 
|  | 5019 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5020 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { | 
|  | 5021 | const sp<FakeWindowHandle>& w = | 
|  | 5022 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 5023 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5024 |  | 
|  | 5025 | touch(); | 
|  | 5026 |  | 
|  | 5027 | w->assertNoEvents(); | 
|  | 5028 | } | 
|  | 5029 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5030 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5031 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); | 
|  | 5032 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5033 |  | 
|  | 5034 | touch(); | 
|  | 5035 |  | 
|  | 5036 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5037 | } | 
|  | 5038 |  | 
|  | 5039 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5040 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5041 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5042 | w->setFrame(Rect(0, 0, 50, 50)); | 
|  | 5043 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5044 |  | 
|  | 5045 | touch({PointF{100, 100}}); | 
|  | 5046 |  | 
|  | 5047 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5048 | } | 
|  | 5049 |  | 
|  | 5050 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5051 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5052 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5053 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5054 |  | 
|  | 5055 | touch(); | 
|  | 5056 |  | 
|  | 5057 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5058 | } | 
|  | 5059 |  | 
|  | 5060 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { | 
|  | 5061 | const sp<FakeWindowHandle>& w = | 
|  | 5062 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
|  | 5063 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5064 |  | 
|  | 5065 | touch(); | 
|  | 5066 |  | 
|  | 5067 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5068 | } | 
|  | 5069 |  | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5070 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { | 
|  | 5071 | const sp<FakeWindowHandle>& w = | 
|  | 5072 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
|  | 5073 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5074 |  | 
|  | 5075 | touch(); | 
|  | 5076 |  | 
|  | 5077 | w->assertNoEvents(); | 
|  | 5078 | } | 
|  | 5079 |  | 
|  | 5080 | /** | 
|  | 5081 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs | 
|  | 5082 | * inside) while letting them pass-through. Note that even though touch passes through the occluding | 
|  | 5083 | * window, the occluding window will still receive ACTION_OUTSIDE event. | 
|  | 5084 | */ | 
|  | 5085 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5086 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { | 
|  | 5087 | const sp<FakeWindowHandle>& w = | 
|  | 5088 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5089 | w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH); | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5090 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5091 |  | 
|  | 5092 | touch(); | 
|  | 5093 |  | 
|  | 5094 | w->consumeMotionOutside(); | 
|  | 5095 | } | 
|  | 5096 |  | 
|  | 5097 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { | 
|  | 5098 | const sp<FakeWindowHandle>& w = | 
|  | 5099 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5100 | w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH); | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5101 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5102 |  | 
|  | 5103 | touch(); | 
|  | 5104 |  | 
|  | 5105 | InputEvent* event = w->consume(); | 
|  | 5106 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()); | 
|  | 5107 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 5108 | EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX()); | 
|  | 5109 | EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY()); | 
|  | 5110 | } | 
|  | 5111 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5112 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5113 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5114 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 5115 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5116 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5117 |  | 
|  | 5118 | touch(); | 
|  | 5119 |  | 
|  | 5120 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5121 | } | 
|  | 5122 |  | 
|  | 5123 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { | 
|  | 5124 | const sp<FakeWindowHandle>& w = | 
|  | 5125 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 5126 | MAXIMUM_OBSCURING_OPACITY); | 
|  | 5127 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5128 |  | 
|  | 5129 | touch(); | 
|  | 5130 |  | 
|  | 5131 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5132 | } | 
|  | 5133 |  | 
|  | 5134 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5135 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5136 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 5137 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5138 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5139 |  | 
|  | 5140 | touch(); | 
|  | 5141 |  | 
|  | 5142 | mTouchWindow->assertNoEvents(); | 
|  | 5143 | } | 
|  | 5144 |  | 
|  | 5145 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { | 
|  | 5146 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 | 
|  | 5147 | const sp<FakeWindowHandle>& w1 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5148 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, | 
|  | 5149 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5150 | const sp<FakeWindowHandle>& w2 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5151 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, | 
|  | 5152 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5153 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 5154 |  | 
|  | 5155 | touch(); | 
|  | 5156 |  | 
|  | 5157 | mTouchWindow->assertNoEvents(); | 
|  | 5158 | } | 
|  | 5159 |  | 
|  | 5160 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { | 
|  | 5161 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 | 
|  | 5162 | const sp<FakeWindowHandle>& w1 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5163 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, | 
|  | 5164 | OPACITY_FAR_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5165 | const sp<FakeWindowHandle>& w2 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5166 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, | 
|  | 5167 | OPACITY_FAR_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5168 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 5169 |  | 
|  | 5170 | touch(); | 
|  | 5171 |  | 
|  | 5172 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5173 | } | 
|  | 5174 |  | 
|  | 5175 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5176 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { | 
|  | 5177 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5178 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 5179 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5180 | const sp<FakeWindowHandle>& wC = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5181 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 5182 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5183 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
|  | 5184 |  | 
|  | 5185 | touch(); | 
|  | 5186 |  | 
|  | 5187 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5188 | } | 
|  | 5189 |  | 
|  | 5190 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { | 
|  | 5191 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5192 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 5193 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5194 | const sp<FakeWindowHandle>& wC = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5195 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 5196 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5197 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5198 |  | 
|  | 5199 | touch(); | 
|  | 5200 |  | 
|  | 5201 | mTouchWindow->assertNoEvents(); | 
|  | 5202 | } | 
|  | 5203 |  | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5204 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5205 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { | 
|  | 5206 | const sp<FakeWindowHandle>& wA = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5207 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 5208 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5209 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5210 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 5211 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5212 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); | 
|  | 5213 |  | 
|  | 5214 | touch(); | 
|  | 5215 |  | 
|  | 5216 | mTouchWindow->assertNoEvents(); | 
|  | 5217 | } | 
|  | 5218 |  | 
|  | 5219 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5220 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { | 
|  | 5221 | const sp<FakeWindowHandle>& wA = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5222 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 5223 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5224 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5225 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 5226 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5227 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); | 
|  | 5228 |  | 
|  | 5229 | touch(); | 
|  | 5230 |  | 
|  | 5231 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5232 | } | 
|  | 5233 |  | 
|  | 5234 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { | 
|  | 5235 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5236 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 5237 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5238 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5239 |  | 
|  | 5240 | touch(); | 
|  | 5241 |  | 
|  | 5242 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5243 | } | 
|  | 5244 |  | 
|  | 5245 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { | 
|  | 5246 | const sp<FakeWindowHandle>& w = | 
|  | 5247 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 5248 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5249 |  | 
|  | 5250 | touch(); | 
|  | 5251 |  | 
|  | 5252 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5253 | } | 
|  | 5254 |  | 
| Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 5255 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5256 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { | 
|  | 5257 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); | 
|  | 5258 | const sp<FakeWindowHandle>& w = | 
|  | 5259 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); | 
|  | 5260 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5261 |  | 
|  | 5262 | touch(); | 
|  | 5263 |  | 
|  | 5264 | mTouchWindow->assertNoEvents(); | 
|  | 5265 | } | 
|  | 5266 |  | 
|  | 5267 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { | 
|  | 5268 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); | 
|  | 5269 | const sp<FakeWindowHandle>& w = | 
|  | 5270 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); | 
|  | 5271 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5272 |  | 
|  | 5273 | touch(); | 
|  | 5274 |  | 
|  | 5275 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5276 | } | 
|  | 5277 |  | 
|  | 5278 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5279 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { | 
|  | 5280 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); | 
|  | 5281 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5282 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 5283 | OPACITY_ABOVE_THRESHOLD); | 
|  | 5284 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5285 |  | 
|  | 5286 | touch(); | 
|  | 5287 |  | 
|  | 5288 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5289 | } | 
|  | 5290 |  | 
|  | 5291 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5292 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { | 
|  | 5293 | const sp<FakeWindowHandle>& w1 = | 
|  | 5294 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, | 
|  | 5295 | OPACITY_BELOW_THRESHOLD); | 
|  | 5296 | const sp<FakeWindowHandle>& w2 = | 
|  | 5297 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 5298 | OPACITY_BELOW_THRESHOLD); | 
|  | 5299 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 5300 |  | 
|  | 5301 | touch(); | 
|  | 5302 |  | 
|  | 5303 | mTouchWindow->assertNoEvents(); | 
|  | 5304 | } | 
|  | 5305 |  | 
|  | 5306 | /** | 
|  | 5307 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the | 
|  | 5308 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold | 
|  | 5309 | * (which alone would result in allowing touches) does not affect the blocking behavior. | 
|  | 5310 | */ | 
|  | 5311 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5312 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { | 
|  | 5313 | const sp<FakeWindowHandle>& wB = | 
|  | 5314 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, | 
|  | 5315 | OPACITY_BELOW_THRESHOLD); | 
|  | 5316 | const sp<FakeWindowHandle>& wC = | 
|  | 5317 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 5318 | OPACITY_BELOW_THRESHOLD); | 
|  | 5319 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
|  | 5320 |  | 
|  | 5321 | touch(); | 
|  | 5322 |  | 
|  | 5323 | mTouchWindow->assertNoEvents(); | 
|  | 5324 | } | 
|  | 5325 |  | 
|  | 5326 | /** | 
|  | 5327 | * This test is testing that a window from a different UID but with same application token doesn't | 
|  | 5328 | * block the touch. Apps can share the application token for close UI collaboration for example. | 
|  | 5329 | */ | 
|  | 5330 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5331 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { | 
|  | 5332 | const sp<FakeWindowHandle>& w = | 
|  | 5333 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 5334 | w->setApplicationToken(mTouchWindow->getApplicationToken()); | 
| Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 5335 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5336 |  | 
|  | 5337 | touch(); | 
|  | 5338 |  | 
|  | 5339 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5340 | } | 
|  | 5341 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5342 | class InputDispatcherDragTests : public InputDispatcherTest { | 
|  | 5343 | protected: | 
|  | 5344 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 5345 | sp<FakeWindowHandle> mWindow; | 
|  | 5346 | sp<FakeWindowHandle> mSecondWindow; | 
|  | 5347 | sp<FakeWindowHandle> mDragWindow; | 
|  | 5348 |  | 
|  | 5349 | void SetUp() override { | 
|  | 5350 | InputDispatcherTest::SetUp(); | 
|  | 5351 | mApp = std::make_shared<FakeApplicationHandle>(); | 
|  | 5352 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 5353 | mWindow->setFrame(Rect(0, 0, 100, 100)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5354 | mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5355 |  | 
|  | 5356 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); | 
|  | 5357 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5358 | mSecondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5359 |  | 
|  | 5360 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
|  | 5361 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); | 
|  | 5362 | } | 
|  | 5363 |  | 
|  | 5364 | // Start performing drag, we will create a drag window and transfer touch to it. | 
|  | 5365 | void performDrag() { | 
|  | 5366 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5367 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5368 | {50, 50})) | 
|  | 5369 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5370 |  | 
|  | 5371 | // Window should receive motion event. | 
|  | 5372 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5373 |  | 
|  | 5374 | // The drag window covers the entire display | 
|  | 5375 | mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); | 
|  | 5376 | mDispatcher->setInputWindows( | 
|  | 5377 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); | 
|  | 5378 |  | 
|  | 5379 | // Transfer touch focus to the drag window | 
|  | 5380 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), | 
|  | 5381 | true /* isDragDrop */); | 
|  | 5382 | mWindow->consumeMotionCancel(); | 
|  | 5383 | mDragWindow->consumeMotionDown(); | 
|  | 5384 | } | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5385 |  | 
|  | 5386 | // Start performing drag, we will create a drag window and transfer touch to it. | 
|  | 5387 | void performStylusDrag() { | 
|  | 5388 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5389 | injectMotionEvent(mDispatcher, | 
|  | 5390 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, | 
|  | 5391 | AINPUT_SOURCE_STYLUS) | 
|  | 5392 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) | 
|  | 5393 | .pointer(PointerBuilder(0, | 
|  | 5394 | AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 5395 | .x(50) | 
|  | 5396 | .y(50)) | 
|  | 5397 | .build())); | 
|  | 5398 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5399 |  | 
|  | 5400 | // The drag window covers the entire display | 
|  | 5401 | mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); | 
|  | 5402 | mDispatcher->setInputWindows( | 
|  | 5403 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); | 
|  | 5404 |  | 
|  | 5405 | // Transfer touch focus to the drag window | 
|  | 5406 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), | 
|  | 5407 | true /* isDragDrop */); | 
|  | 5408 | mWindow->consumeMotionCancel(); | 
|  | 5409 | mDragWindow->consumeMotionDown(); | 
|  | 5410 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5411 | }; | 
|  | 5412 |  | 
|  | 5413 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { | 
|  | 5414 | performDrag(); | 
|  | 5415 |  | 
|  | 5416 | // Move on window. | 
|  | 5417 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5418 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5419 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5420 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5421 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5422 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5423 | mSecondWindow->assertNoEvents(); | 
|  | 5424 |  | 
|  | 5425 | // Move to another window. | 
|  | 5426 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5427 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5428 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 5429 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5430 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5431 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 5432 | mSecondWindow->consumeDragEvent(false, 50, 50); | 
|  | 5433 |  | 
|  | 5434 | // Move back to original window. | 
|  | 5435 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5436 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5437 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5438 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5439 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5440 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5441 | mSecondWindow->consumeDragEvent(true, -50, 50); | 
|  | 5442 |  | 
|  | 5443 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5444 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5445 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5446 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 5447 | mWindow->assertNoEvents(); | 
|  | 5448 | mSecondWindow->assertNoEvents(); | 
|  | 5449 | } | 
|  | 5450 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5451 | TEST_F(InputDispatcherDragTests, DragAndDrop) { | 
|  | 5452 | performDrag(); | 
|  | 5453 |  | 
|  | 5454 | // Move on window. | 
|  | 5455 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5456 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5457 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5458 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5459 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5460 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5461 | mSecondWindow->assertNoEvents(); | 
|  | 5462 |  | 
|  | 5463 | // Move to another window. | 
|  | 5464 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5465 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5466 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 5467 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5468 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5469 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 5470 | mSecondWindow->consumeDragEvent(false, 50, 50); | 
|  | 5471 |  | 
|  | 5472 | // drop to another window. | 
|  | 5473 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5474 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5475 | {150, 50})) | 
|  | 5476 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5477 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 5478 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); | 
|  | 5479 | mWindow->assertNoEvents(); | 
|  | 5480 | mSecondWindow->assertNoEvents(); | 
|  | 5481 | } | 
|  | 5482 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5483 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { | 
|  | 5484 | performStylusDrag(); | 
|  | 5485 |  | 
|  | 5486 | // Move on window and keep button pressed. | 
|  | 5487 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5488 | injectMotionEvent(mDispatcher, | 
|  | 5489 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) | 
|  | 5490 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) | 
|  | 5491 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 5492 | .x(50) | 
|  | 5493 | .y(50)) | 
|  | 5494 | .build())) | 
|  | 5495 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5496 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5497 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5498 | mSecondWindow->assertNoEvents(); | 
|  | 5499 |  | 
|  | 5500 | // Move to another window and release button, expect to drop item. | 
|  | 5501 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5502 | injectMotionEvent(mDispatcher, | 
|  | 5503 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) | 
|  | 5504 | .buttonState(0) | 
|  | 5505 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 5506 | .x(150) | 
|  | 5507 | .y(50)) | 
|  | 5508 | .build())) | 
|  | 5509 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5510 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5511 | mWindow->assertNoEvents(); | 
|  | 5512 | mSecondWindow->assertNoEvents(); | 
|  | 5513 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); | 
|  | 5514 |  | 
|  | 5515 | // nothing to the window. | 
|  | 5516 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5517 | injectMotionEvent(mDispatcher, | 
|  | 5518 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) | 
|  | 5519 | .buttonState(0) | 
|  | 5520 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 5521 | .x(150) | 
|  | 5522 | .y(50)) | 
|  | 5523 | .build())) | 
|  | 5524 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5525 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 5526 | mWindow->assertNoEvents(); | 
|  | 5527 | mSecondWindow->assertNoEvents(); | 
|  | 5528 | } | 
|  | 5529 |  | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 5530 | TEST_F(InputDispatcherDragTests, DragAndDrop_InvalidWindow) { | 
|  | 5531 | performDrag(); | 
|  | 5532 |  | 
|  | 5533 | // Set second window invisible. | 
|  | 5534 | mSecondWindow->setVisible(false); | 
|  | 5535 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); | 
|  | 5536 |  | 
|  | 5537 | // Move on window. | 
|  | 5538 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5539 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5540 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5541 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5542 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5543 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5544 | mSecondWindow->assertNoEvents(); | 
|  | 5545 |  | 
|  | 5546 | // Move to another window. | 
|  | 5547 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5548 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5549 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 5550 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5551 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5552 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 5553 | mSecondWindow->assertNoEvents(); | 
|  | 5554 |  | 
|  | 5555 | // drop to another window. | 
|  | 5556 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5557 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5558 | {150, 50})) | 
|  | 5559 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5560 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 5561 | mFakePolicy->assertDropTargetEquals(nullptr); | 
|  | 5562 | mWindow->assertNoEvents(); | 
|  | 5563 | mSecondWindow->assertNoEvents(); | 
|  | 5564 | } | 
|  | 5565 |  | 
| Vishnu Nair | 41f77b8 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 5566 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; | 
|  | 5567 |  | 
|  | 5568 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { | 
|  | 5569 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 5570 | sp<FakeWindowHandle> window = | 
|  | 5571 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 5572 | window->setInputFeatures(WindowInfo::Feature::DROP_INPUT); | 
|  | 5573 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 5574 | window->setFocusable(true); | 
|  | 5575 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 5576 | setFocusedWindow(window); | 
|  | 5577 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 5578 |  | 
|  | 5579 | // With the flag set, window should not get any input | 
|  | 5580 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 5581 | mDispatcher->notifyKey(&keyArgs); | 
|  | 5582 | window->assertNoEvents(); | 
|  | 5583 |  | 
|  | 5584 | NotifyMotionArgs motionArgs = | 
|  | 5585 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5586 | ADISPLAY_ID_DEFAULT); | 
|  | 5587 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 5588 | window->assertNoEvents(); | 
|  | 5589 |  | 
|  | 5590 | // With the flag cleared, the window should get input | 
|  | 5591 | window->setInputFeatures({}); | 
|  | 5592 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 5593 |  | 
|  | 5594 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); | 
|  | 5595 | mDispatcher->notifyKey(&keyArgs); | 
|  | 5596 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); | 
|  | 5597 |  | 
|  | 5598 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5599 | ADISPLAY_ID_DEFAULT); | 
|  | 5600 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 5601 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5602 | window->assertNoEvents(); | 
|  | 5603 | } | 
|  | 5604 |  | 
|  | 5605 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { | 
|  | 5606 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = | 
|  | 5607 | std::make_shared<FakeApplicationHandle>(); | 
|  | 5608 | sp<FakeWindowHandle> obscuringWindow = | 
|  | 5609 | new FakeWindowHandle(obscuringApplication, mDispatcher, "obscuringWindow", | 
|  | 5610 | ADISPLAY_ID_DEFAULT); | 
|  | 5611 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); | 
|  | 5612 | obscuringWindow->setOwnerInfo(111, 111); | 
|  | 5613 | obscuringWindow->setFlags(WindowInfo::Flag::NOT_TOUCHABLE); | 
|  | 5614 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 5615 | sp<FakeWindowHandle> window = | 
|  | 5616 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 5617 | window->setInputFeatures(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED); | 
|  | 5618 | window->setOwnerInfo(222, 222); | 
|  | 5619 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 5620 | window->setFocusable(true); | 
|  | 5621 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); | 
|  | 5622 | setFocusedWindow(window); | 
|  | 5623 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 5624 |  | 
|  | 5625 | // With the flag set, window should not get any input | 
|  | 5626 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 5627 | mDispatcher->notifyKey(&keyArgs); | 
|  | 5628 | window->assertNoEvents(); | 
|  | 5629 |  | 
|  | 5630 | NotifyMotionArgs motionArgs = | 
|  | 5631 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5632 | ADISPLAY_ID_DEFAULT); | 
|  | 5633 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 5634 | window->assertNoEvents(); | 
|  | 5635 |  | 
|  | 5636 | // With the flag cleared, the window should get input | 
|  | 5637 | window->setInputFeatures({}); | 
|  | 5638 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); | 
|  | 5639 |  | 
|  | 5640 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); | 
|  | 5641 | mDispatcher->notifyKey(&keyArgs); | 
|  | 5642 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); | 
|  | 5643 |  | 
|  | 5644 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5645 | ADISPLAY_ID_DEFAULT); | 
|  | 5646 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 5647 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); | 
|  | 5648 | window->assertNoEvents(); | 
|  | 5649 | } | 
|  | 5650 |  | 
|  | 5651 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { | 
|  | 5652 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = | 
|  | 5653 | std::make_shared<FakeApplicationHandle>(); | 
|  | 5654 | sp<FakeWindowHandle> obscuringWindow = | 
|  | 5655 | new FakeWindowHandle(obscuringApplication, mDispatcher, "obscuringWindow", | 
|  | 5656 | ADISPLAY_ID_DEFAULT); | 
|  | 5657 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); | 
|  | 5658 | obscuringWindow->setOwnerInfo(111, 111); | 
|  | 5659 | obscuringWindow->setFlags(WindowInfo::Flag::NOT_TOUCHABLE); | 
|  | 5660 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 5661 | sp<FakeWindowHandle> window = | 
|  | 5662 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 5663 | window->setInputFeatures(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED); | 
|  | 5664 | window->setOwnerInfo(222, 222); | 
|  | 5665 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 5666 | window->setFocusable(true); | 
|  | 5667 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); | 
|  | 5668 | setFocusedWindow(window); | 
|  | 5669 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 5670 |  | 
|  | 5671 | // With the flag set, window should not get any input | 
|  | 5672 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 5673 | mDispatcher->notifyKey(&keyArgs); | 
|  | 5674 | window->assertNoEvents(); | 
|  | 5675 |  | 
|  | 5676 | NotifyMotionArgs motionArgs = | 
|  | 5677 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5678 | ADISPLAY_ID_DEFAULT); | 
|  | 5679 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 5680 | window->assertNoEvents(); | 
|  | 5681 |  | 
|  | 5682 | // When the window is no longer obscured because it went on top, it should get input | 
|  | 5683 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); | 
|  | 5684 |  | 
|  | 5685 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); | 
|  | 5686 | mDispatcher->notifyKey(&keyArgs); | 
|  | 5687 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); | 
|  | 5688 |  | 
|  | 5689 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5690 | ADISPLAY_ID_DEFAULT); | 
|  | 5691 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 5692 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5693 | window->assertNoEvents(); | 
|  | 5694 | } | 
|  | 5695 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5696 | } // namespace android::inputdispatcher |