| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2010 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 17 | #include "../dispatcher/InputDispatcher.h" | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 18 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 19 | #include <android-base/stringprintf.h> | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 20 | #include <android-base/thread_annotations.h> | 
| Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 21 | #include <binder/Binder.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 22 | #include <gtest/gtest.h> | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 23 | #include <input/Input.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 24 | #include <linux/input.h> | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 25 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 26 | #include <cinttypes> | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 27 | #include <thread> | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 28 | #include <unordered_set> | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 29 | #include <vector> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 30 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 31 | using android::base::StringPrintf; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 32 | using android::os::InputEventInjectionResult; | 
|  | 33 | using android::os::InputEventInjectionSync; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 34 | using android::os::TouchOcclusionMode; | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 35 | using namespace android::flag_operators; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 36 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 37 | namespace android::inputdispatcher { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 38 |  | 
|  | 39 | // An arbitrary time value. | 
|  | 40 | static const nsecs_t ARBITRARY_TIME = 1234; | 
|  | 41 |  | 
|  | 42 | // An arbitrary device id. | 
|  | 43 | static const int32_t DEVICE_ID = 1; | 
|  | 44 |  | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 45 | // An arbitrary display id. | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 46 | static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 47 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 48 | // An arbitrary injector pid / uid pair that has permission to inject events. | 
|  | 49 | static const int32_t INJECTOR_PID = 999; | 
|  | 50 | static const int32_t INJECTOR_UID = 1001; | 
|  | 51 |  | 
| Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 52 | // An arbitrary pid of the gesture monitor window | 
|  | 53 | static constexpr int32_t MONITOR_PID = 2001; | 
|  | 54 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 55 | struct PointF { | 
|  | 56 | float x; | 
|  | 57 | float y; | 
|  | 58 | }; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 59 |  | 
| Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 60 | /** | 
|  | 61 | * Return a DOWN key event with KEYCODE_A. | 
|  | 62 | */ | 
|  | 63 | static KeyEvent getTestKeyEvent() { | 
|  | 64 | KeyEvent event; | 
|  | 65 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 66 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 67 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, | 
|  | 68 | ARBITRARY_TIME, ARBITRARY_TIME); | 
| Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 69 | return event; | 
|  | 70 | } | 
|  | 71 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 72 | // --- FakeInputDispatcherPolicy --- | 
|  | 73 |  | 
|  | 74 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { | 
|  | 75 | InputDispatcherConfiguration mConfig; | 
|  | 76 |  | 
|  | 77 | protected: | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 78 | virtual ~FakeInputDispatcherPolicy() {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 79 |  | 
|  | 80 | public: | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 81 | FakeInputDispatcherPolicy() {} | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 82 |  | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 83 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { | 
| Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 84 | assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action, | 
|  | 85 | args.displayId); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 88 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) { | 
| Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 89 | assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action, | 
|  | 90 | args.displayId); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 93 | void assertFilterInputEventWasNotCalled() { | 
|  | 94 | std::scoped_lock lock(mLock); | 
|  | 95 | ASSERT_EQ(nullptr, mFilteredEvent); | 
|  | 96 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 97 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 98 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 99 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 100 | ASSERT_TRUE(mConfigurationChangedTime) | 
|  | 101 | << "Timed out waiting for configuration changed call"; | 
|  | 102 | ASSERT_EQ(*mConfigurationChangedTime, when); | 
|  | 103 | mConfigurationChangedTime = std::nullopt; | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 107 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 108 | ASSERT_TRUE(mLastNotifySwitch); | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 109 | // 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] | 110 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); | 
|  | 111 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); | 
|  | 112 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); | 
|  | 113 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); | 
|  | 114 | mLastNotifySwitch = std::nullopt; | 
|  | 115 | } | 
|  | 116 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 117 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 118 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 119 | ASSERT_EQ(touchedToken, mOnPointerDownToken); | 
|  | 120 | mOnPointerDownToken.clear(); | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | void assertOnPointerDownWasNotCalled() { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 124 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 125 | ASSERT_TRUE(mOnPointerDownToken == nullptr) | 
|  | 126 | << "Expected onPointerDownOutsideFocus to not have been called"; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 127 | } | 
|  | 128 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 129 | // This function must be called soon after the expected ANR timer starts, | 
|  | 130 | // because we are also checking how much time has passed. | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 131 | void assertNotifyNoFocusedWindowAnrWasCalled( | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 132 | std::chrono::nanoseconds timeout, | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 133 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { | 
|  | 134 | std::shared_ptr<InputApplicationHandle> application; | 
|  | 135 | { // acquire lock | 
|  | 136 | std::unique_lock lock(mLock); | 
|  | 137 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 138 | ASSERT_NO_FATAL_FAILURE( | 
|  | 139 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); | 
|  | 140 | } // release lock | 
|  | 141 | ASSERT_EQ(expectedApplication, application); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 142 | } | 
|  | 143 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 144 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, | 
|  | 145 | const sp<IBinder>& expectedConnectionToken) { | 
|  | 146 | sp<IBinder> connectionToken = getUnresponsiveWindowToken(timeout); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 147 | ASSERT_EQ(expectedConnectionToken, connectionToken); | 
|  | 148 | } | 
|  | 149 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 150 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) { | 
|  | 151 | sp<IBinder> connectionToken = getResponsiveWindowToken(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 152 | ASSERT_EQ(expectedConnectionToken, connectionToken); | 
|  | 153 | } | 
|  | 154 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 155 | void assertNotifyMonitorUnresponsiveWasCalled(std::chrono::nanoseconds timeout) { | 
|  | 156 | int32_t pid = getUnresponsiveMonitorPid(timeout); | 
|  | 157 | ASSERT_EQ(MONITOR_PID, pid); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 158 | } | 
|  | 159 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 160 | void assertNotifyMonitorResponsiveWasCalled() { | 
|  | 161 | int32_t pid = getResponsiveMonitorPid(); | 
|  | 162 | ASSERT_EQ(MONITOR_PID, pid); | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 166 | std::unique_lock lock(mLock); | 
|  | 167 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 168 | return getAnrTokenLockedInterruptible(timeout, mAnrWindowTokens, lock); | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | sp<IBinder> getResponsiveWindowToken() { | 
|  | 172 | std::unique_lock lock(mLock); | 
|  | 173 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 174 | return getAnrTokenLockedInterruptible(0s, mResponsiveWindowTokens, lock); | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | int32_t getUnresponsiveMonitorPid(std::chrono::nanoseconds timeout) { | 
|  | 178 | std::unique_lock lock(mLock); | 
|  | 179 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 180 | return getAnrTokenLockedInterruptible(timeout, mAnrMonitorPids, lock); | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | int32_t getResponsiveMonitorPid() { | 
|  | 184 | std::unique_lock lock(mLock); | 
|  | 185 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 186 | return getAnrTokenLockedInterruptible(0s, mResponsiveMonitorPids, lock); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 187 | } | 
|  | 188 |  | 
|  | 189 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait | 
|  | 190 | // for a specific container to become non-empty. When the container is non-empty, return the | 
|  | 191 | // first entry from the container and erase it. | 
|  | 192 | template <class T> | 
|  | 193 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, | 
|  | 194 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { | 
|  | 195 | const std::chrono::time_point start = std::chrono::steady_clock::now(); | 
|  | 196 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 197 |  | 
|  | 198 | // If there is an ANR, Dispatcher won't be idle because there are still events | 
|  | 199 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle | 
|  | 200 | // before checking if ANR was called. | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 201 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need | 
|  | 202 | // to provide it some time to act. 100ms seems reasonable. | 
|  | 203 | mNotifyAnr.wait_for(lock, timeToWait, | 
|  | 204 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 205 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 206 | if (storage.empty()) { | 
|  | 207 | ADD_FAILURE() << "Did not receive the ANR callback"; | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 208 | return {}; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 209 | } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 210 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because | 
|  | 211 | // the dispatcher started counting before this function was called | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 212 | if (std::chrono::abs(timeout - waited) > 100ms) { | 
|  | 213 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " | 
|  | 214 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() | 
|  | 215 | << "ms, but waited " | 
|  | 216 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() | 
|  | 217 | << "ms instead"; | 
|  | 218 | } | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 219 | T token = storage.front(); | 
|  | 220 | storage.pop(); | 
|  | 221 | return token; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 222 | } | 
|  | 223 |  | 
|  | 224 | void assertNotifyAnrWasNotCalled() { | 
|  | 225 | std::scoped_lock lock(mLock); | 
|  | 226 | ASSERT_TRUE(mAnrApplications.empty()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 227 | ASSERT_TRUE(mAnrWindowTokens.empty()); | 
|  | 228 | ASSERT_TRUE(mAnrMonitorPids.empty()); | 
|  | 229 | ASSERT_TRUE(mResponsiveWindowTokens.empty()) | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 230 | << "ANR was not called, but please also consume the 'connection is responsive' " | 
|  | 231 | "signal"; | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 232 | ASSERT_TRUE(mResponsiveMonitorPids.empty()) | 
|  | 233 | << "Monitor ANR was not called, but please also consume the 'monitor is responsive'" | 
|  | 234 | " signal"; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 235 | } | 
|  | 236 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 237 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { | 
|  | 238 | mConfig.keyRepeatTimeout = timeout; | 
|  | 239 | mConfig.keyRepeatDelay = delay; | 
|  | 240 | } | 
|  | 241 |  | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 242 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 243 | std::unique_lock lock(mLock); | 
|  | 244 | base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 245 |  | 
|  | 246 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, | 
|  | 247 | [this, enabled]() REQUIRES(mLock) { | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 248 | return mPointerCaptureRequest->enable == | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 249 | enabled; | 
|  | 250 | })) { | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 251 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled | 
|  | 252 | << ") to be called."; | 
|  | 253 | return {}; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 254 | } | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 255 | auto request = *mPointerCaptureRequest; | 
|  | 256 | mPointerCaptureRequest.reset(); | 
|  | 257 | return request; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 258 | } | 
|  | 259 |  | 
|  | 260 | void assertSetPointerCaptureNotCalled() { | 
|  | 261 | std::unique_lock lock(mLock); | 
|  | 262 | base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 263 |  | 
|  | 264 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 265 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 266 | "enabled = " | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 267 | << std::to_string(mPointerCaptureRequest->enable); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 268 | } | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 269 | mPointerCaptureRequest.reset(); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 270 | } | 
|  | 271 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 272 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { | 
|  | 273 | std::scoped_lock lock(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 274 | ASSERT_TRUE(mNotifyDropWindowWasCalled); | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 275 | ASSERT_EQ(targetToken, mDropTargetWindowToken); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 276 | mNotifyDropWindowWasCalled = false; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 277 | } | 
|  | 278 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 279 | private: | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 280 | std::mutex mLock; | 
|  | 281 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); | 
|  | 282 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); | 
|  | 283 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); | 
|  | 284 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 285 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 286 | std::condition_variable mPointerCaptureChangedCondition; | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 287 |  | 
|  | 288 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 289 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 290 | // ANR handling | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 291 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 292 | std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock); | 
|  | 293 | std::queue<sp<IBinder>> mResponsiveWindowTokens GUARDED_BY(mLock); | 
|  | 294 | std::queue<int32_t> mAnrMonitorPids GUARDED_BY(mLock); | 
|  | 295 | std::queue<int32_t> mResponsiveMonitorPids GUARDED_BY(mLock); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 296 | std::condition_variable mNotifyAnr; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 297 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 298 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 299 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 300 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 301 | void notifyConfigurationChanged(nsecs_t when) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 302 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 303 | mConfigurationChangedTime = when; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 304 | } | 
|  | 305 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 306 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, const std::string&) override { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 307 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 308 | mAnrWindowTokens.push(connectionToken); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 309 | mNotifyAnr.notify_all(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 310 | } | 
|  | 311 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 312 | void notifyMonitorUnresponsive(int32_t pid, const std::string&) override { | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 313 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 314 | mAnrMonitorPids.push(pid); | 
|  | 315 | mNotifyAnr.notify_all(); | 
|  | 316 | } | 
|  | 317 |  | 
|  | 318 | void notifyWindowResponsive(const sp<IBinder>& connectionToken) override { | 
|  | 319 | std::scoped_lock lock(mLock); | 
|  | 320 | mResponsiveWindowTokens.push(connectionToken); | 
|  | 321 | mNotifyAnr.notify_all(); | 
|  | 322 | } | 
|  | 323 |  | 
|  | 324 | void notifyMonitorResponsive(int32_t pid) override { | 
|  | 325 | std::scoped_lock lock(mLock); | 
|  | 326 | mResponsiveMonitorPids.push(pid); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 327 | mNotifyAnr.notify_all(); | 
|  | 328 | } | 
|  | 329 |  | 
|  | 330 | void notifyNoFocusedWindowAnr( | 
|  | 331 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { | 
|  | 332 | std::scoped_lock lock(mLock); | 
|  | 333 | mAnrApplications.push(applicationHandle); | 
|  | 334 | mNotifyAnr.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 335 | } | 
|  | 336 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 337 | void notifyInputChannelBroken(const sp<IBinder>&) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 338 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 339 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} | 
| Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 340 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 341 | void notifyUntrustedTouch(const std::string& obscuringPackage) override {} | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 342 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, | 
|  | 343 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, | 
|  | 344 | const std::vector<float>& values) override {} | 
|  | 345 |  | 
|  | 346 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, | 
|  | 347 | InputDeviceSensorAccuracy accuracy) override {} | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 348 |  | 
| Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 349 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} | 
|  | 350 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 351 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 352 | *outConfig = mConfig; | 
|  | 353 | } | 
|  | 354 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 355 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 356 | std::scoped_lock lock(mLock); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 357 | switch (inputEvent->getType()) { | 
|  | 358 | case AINPUT_EVENT_TYPE_KEY: { | 
|  | 359 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 360 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 361 | break; | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | case AINPUT_EVENT_TYPE_MOTION: { | 
|  | 365 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 366 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 367 | break; | 
|  | 368 | } | 
|  | 369 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 370 | return true; | 
|  | 371 | } | 
|  | 372 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 373 | void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 374 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 375 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 376 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 377 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 378 | return 0; | 
|  | 379 | } | 
|  | 380 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 381 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 382 | return false; | 
|  | 383 | } | 
|  | 384 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 385 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, | 
|  | 386 | uint32_t policyFlags) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 387 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 388 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is | 
|  | 389 | * essentially a passthrough for notifySwitch. | 
|  | 390 | */ | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 391 | mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 392 | } | 
|  | 393 |  | 
| Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 394 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 395 |  | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 396 | bool checkInjectEventsPermissionNonReentrant(int32_t pid, int32_t uid) override { | 
|  | 397 | return pid == INJECTOR_PID && uid == INJECTOR_UID; | 
|  | 398 | } | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 399 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 400 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 401 | std::scoped_lock lock(mLock); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 402 | mOnPointerDownToken = newToken; | 
|  | 403 | } | 
|  | 404 |  | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 405 | void setPointerCapture(const PointerCaptureRequest& request) override { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 406 | std::scoped_lock lock(mLock); | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 407 | mPointerCaptureRequest = {request}; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 408 | mPointerCaptureChangedCondition.notify_all(); | 
|  | 409 | } | 
|  | 410 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 411 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { | 
|  | 412 | std::scoped_lock lock(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 413 | mNotifyDropWindowWasCalled = true; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 414 | mDropTargetWindowToken = token; | 
|  | 415 | } | 
|  | 416 |  | 
| Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 417 | void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action, | 
|  | 418 | int32_t displayId) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 419 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 420 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; | 
|  | 421 | ASSERT_EQ(mFilteredEvent->getType(), type); | 
|  | 422 |  | 
|  | 423 | if (type == AINPUT_EVENT_TYPE_KEY) { | 
|  | 424 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent); | 
|  | 425 | EXPECT_EQ(keyEvent.getEventTime(), eventTime); | 
|  | 426 | EXPECT_EQ(keyEvent.getAction(), action); | 
|  | 427 | EXPECT_EQ(keyEvent.getDisplayId(), displayId); | 
|  | 428 | } else if (type == AINPUT_EVENT_TYPE_MOTION) { | 
|  | 429 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent); | 
|  | 430 | EXPECT_EQ(motionEvent.getEventTime(), eventTime); | 
|  | 431 | EXPECT_EQ(motionEvent.getAction(), action); | 
|  | 432 | EXPECT_EQ(motionEvent.getDisplayId(), displayId); | 
|  | 433 | } else { | 
|  | 434 | FAIL() << "Unknown type: " << type; | 
|  | 435 | } | 
|  | 436 |  | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 437 | mFilteredEvent = nullptr; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 438 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 439 | }; | 
|  | 440 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 441 | // --- InputDispatcherTest --- | 
|  | 442 |  | 
|  | 443 | class InputDispatcherTest : public testing::Test { | 
|  | 444 | protected: | 
|  | 445 | sp<FakeInputDispatcherPolicy> mFakePolicy; | 
|  | 446 | sp<InputDispatcher> mDispatcher; | 
|  | 447 |  | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 448 | void SetUp() override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 449 | mFakePolicy = new FakeInputDispatcherPolicy(); | 
|  | 450 | mDispatcher = new InputDispatcher(mFakePolicy); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 451 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 452 | // Start InputDispatcher thread | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 453 | ASSERT_EQ(OK, mDispatcher->start()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 454 | } | 
|  | 455 |  | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 456 | void TearDown() override { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 457 | ASSERT_EQ(OK, mDispatcher->stop()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 458 | mFakePolicy.clear(); | 
|  | 459 | mDispatcher.clear(); | 
|  | 460 | } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 461 |  | 
|  | 462 | /** | 
|  | 463 | * Used for debugging when writing the test | 
|  | 464 | */ | 
|  | 465 | void dumpDispatcherState() { | 
|  | 466 | std::string dump; | 
|  | 467 | mDispatcher->dump(dump); | 
|  | 468 | std::stringstream ss(dump); | 
|  | 469 | std::string to; | 
|  | 470 |  | 
|  | 471 | while (std::getline(ss, to, '\n')) { | 
|  | 472 | ALOGE("%s", to.c_str()); | 
|  | 473 | } | 
|  | 474 | } | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 475 |  | 
|  | 476 | void setFocusedWindow(const sp<InputWindowHandle>& window, | 
|  | 477 | const sp<InputWindowHandle>& focusedWindow = nullptr) { | 
|  | 478 | FocusRequest request; | 
|  | 479 | request.token = window->getToken(); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 480 | request.windowName = window->getName(); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 481 | if (focusedWindow) { | 
|  | 482 | request.focusedToken = focusedWindow->getToken(); | 
|  | 483 | } | 
|  | 484 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 485 | request.displayId = window->getInfo()->displayId; | 
|  | 486 | mDispatcher->setFocusedWindow(request); | 
|  | 487 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 488 | }; | 
|  | 489 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 490 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { | 
|  | 491 | KeyEvent event; | 
|  | 492 |  | 
|  | 493 | // Rejects undefined key actions. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 494 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 495 | INVALID_HMAC, | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 496 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, | 
|  | 497 | ARBITRARY_TIME); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 498 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 499 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 500 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 501 | << "Should reject key events with undefined action."; | 
|  | 502 |  | 
|  | 503 | // 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] | 504 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 505 | 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] | 506 | ARBITRARY_TIME, ARBITRARY_TIME); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 507 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 508 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 509 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 510 | << "Should reject key events with ACTION_MULTIPLE."; | 
|  | 511 | } | 
|  | 512 |  | 
|  | 513 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { | 
|  | 514 | MotionEvent event; | 
|  | 515 | PointerProperties pointerProperties[MAX_POINTERS + 1]; | 
|  | 516 | PointerCoords pointerCoords[MAX_POINTERS + 1]; | 
|  | 517 | for (int i = 0; i <= MAX_POINTERS; i++) { | 
|  | 518 | pointerProperties[i].clear(); | 
|  | 519 | pointerProperties[i].id = i; | 
|  | 520 | pointerCoords[i].clear(); | 
|  | 521 | } | 
|  | 522 |  | 
| Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 523 | // Some constants commonly used below | 
|  | 524 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; | 
|  | 525 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; | 
|  | 526 | constexpr int32_t metaState = AMETA_NONE; | 
|  | 527 | constexpr MotionClassification classification = MotionClassification::NONE; | 
|  | 528 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 529 | ui::Transform identityTransform; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 530 | // Rejects undefined motion actions. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 531 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 532 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, | 
|  | 533 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 534 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
|  | 535 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 536 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 537 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 538 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 539 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 540 | << "Should reject motion events with undefined action."; | 
|  | 541 |  | 
|  | 542 | // Rejects pointer down with invalid index. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 543 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 544 | AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 545 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 546 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 547 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 548 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 549 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, | 
|  | 550 | pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 551 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 552 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 553 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 554 | << "Should reject motion events with pointer down index too large."; | 
|  | 555 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 556 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 557 | AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 558 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 559 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 560 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 561 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 562 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, | 
|  | 563 | pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 564 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 565 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 566 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 567 | << "Should reject motion events with pointer down index too small."; | 
|  | 568 |  | 
|  | 569 | // Rejects pointer up with invalid index. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 570 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 571 | AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 572 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 573 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 574 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 575 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 576 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, | 
|  | 577 | pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 578 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 579 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 580 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 581 | << "Should reject motion events with pointer up index too large."; | 
|  | 582 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 583 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 584 | AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 585 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 586 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 587 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 588 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 589 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, | 
|  | 590 | pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 591 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 592 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 593 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 594 | << "Should reject motion events with pointer up index too small."; | 
|  | 595 |  | 
|  | 596 | // Rejects motion events with invalid number of pointers. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 597 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 598 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 599 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 600 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
|  | 601 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 602 | /*pointerCount*/ 0, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 603 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 604 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 605 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 606 | << "Should reject motion events with 0 pointers."; | 
|  | 607 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 608 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 609 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 610 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 611 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
|  | 612 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 613 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 614 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 615 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 616 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 617 | << "Should reject motion events with more than MAX_POINTERS pointers."; | 
|  | 618 |  | 
|  | 619 | // Rejects motion events with invalid pointer ids. | 
|  | 620 | pointerProperties[0].id = -1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 621 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 622 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 623 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 624 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
|  | 625 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 626 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 627 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 628 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 629 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 630 | << "Should reject motion events with pointer ids less than 0."; | 
|  | 631 |  | 
|  | 632 | pointerProperties[0].id = MAX_POINTER_ID + 1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 633 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 634 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 635 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 636 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
|  | 637 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 638 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 639 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 640 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 641 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 642 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; | 
|  | 643 |  | 
|  | 644 | // Rejects motion events with duplicate pointer ids. | 
|  | 645 | pointerProperties[0].id = 1; | 
|  | 646 | pointerProperties[1].id = 1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 647 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 648 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 649 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 650 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
|  | 651 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 652 | /*pointerCount*/ 2, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 653 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 654 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 655 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 656 | << "Should reject motion events with duplicate pointer ids."; | 
|  | 657 | } | 
|  | 658 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 659 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ | 
|  | 660 |  | 
|  | 661 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { | 
|  | 662 | constexpr nsecs_t eventTime = 20; | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 663 | NotifyConfigurationChangedArgs args(10 /*id*/, eventTime); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 664 | mDispatcher->notifyConfigurationChanged(&args); | 
|  | 665 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 666 |  | 
|  | 667 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); | 
|  | 668 | } | 
|  | 669 |  | 
|  | 670 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 671 | NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/, | 
|  | 672 | 2 /*switchMask*/); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 673 | mDispatcher->notifySwitch(&args); | 
|  | 674 |  | 
|  | 675 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener | 
|  | 676 | args.policyFlags |= POLICY_FLAG_TRUSTED; | 
|  | 677 | mFakePolicy->assertNotifySwitchWasCalled(args); | 
|  | 678 | } | 
|  | 679 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 680 | // --- InputDispatcherTest SetInputWindowTest --- | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 681 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 682 | static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 683 |  | 
|  | 684 | class FakeApplicationHandle : public InputApplicationHandle { | 
|  | 685 | public: | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 686 | FakeApplicationHandle() { | 
|  | 687 | mInfo.name = "Fake Application"; | 
|  | 688 | mInfo.token = new BBinder(); | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 689 | mInfo.dispatchingTimeoutMillis = | 
|  | 690 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 691 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 692 | virtual ~FakeApplicationHandle() {} | 
|  | 693 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 694 | virtual bool updateInfo() override { return true; } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 695 |  | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 696 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { | 
|  | 697 | mInfo.dispatchingTimeoutMillis = timeout.count(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 698 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 699 | }; | 
|  | 700 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 701 | class FakeInputReceiver { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 702 | public: | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 703 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 704 | : mName(name) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 705 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 706 | } | 
|  | 707 |  | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 708 | InputEvent* consume() { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 709 | InputEvent* event; | 
|  | 710 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); | 
|  | 711 | if (!consumeSeq) { | 
|  | 712 | return nullptr; | 
|  | 713 | } | 
|  | 714 | finishEvent(*consumeSeq); | 
|  | 715 | return event; | 
|  | 716 | } | 
|  | 717 |  | 
|  | 718 | /** | 
|  | 719 | * Receive an event without acknowledging it. | 
|  | 720 | * Return the sequence number that could later be used to send finished signal. | 
|  | 721 | */ | 
|  | 722 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 723 | uint32_t consumeSeq; | 
|  | 724 | InputEvent* event; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 725 |  | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 726 | std::chrono::time_point start = std::chrono::steady_clock::now(); | 
|  | 727 | status_t status = WOULD_BLOCK; | 
|  | 728 | while (status == WOULD_BLOCK) { | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 729 | status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 730 | &event); | 
|  | 731 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; | 
|  | 732 | if (elapsed > 100ms) { | 
|  | 733 | break; | 
|  | 734 | } | 
|  | 735 | } | 
|  | 736 |  | 
|  | 737 | if (status == WOULD_BLOCK) { | 
|  | 738 | // Just means there's no event available. | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 739 | return std::nullopt; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 740 | } | 
|  | 741 |  | 
|  | 742 | if (status != OK) { | 
|  | 743 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 744 | return std::nullopt; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 745 | } | 
|  | 746 | if (event == nullptr) { | 
|  | 747 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; | 
| 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 | } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 750 | if (outEvent != nullptr) { | 
|  | 751 | *outEvent = event; | 
|  | 752 | } | 
|  | 753 | return consumeSeq; | 
|  | 754 | } | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 755 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 756 | /** | 
|  | 757 | * To be used together with "receiveEvent" to complete the consumption of an event. | 
|  | 758 | */ | 
|  | 759 | void finishEvent(uint32_t consumeSeq) { | 
|  | 760 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); | 
|  | 761 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 762 | } | 
|  | 763 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 764 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { | 
|  | 765 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); | 
|  | 766 | ASSERT_EQ(OK, status); | 
|  | 767 | } | 
|  | 768 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 769 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, | 
|  | 770 | std::optional<int32_t> expectedDisplayId, | 
|  | 771 | std::optional<int32_t> expectedFlags) { | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 772 | InputEvent* event = consume(); | 
|  | 773 |  | 
|  | 774 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 775 | << ": consumer should have returned non-NULL event."; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 776 | ASSERT_EQ(expectedEventType, event->getType()) | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 777 | << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType) | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 778 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 779 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 780 | if (expectedDisplayId.has_value()) { | 
|  | 781 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); | 
|  | 782 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 783 |  | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 784 | switch (expectedEventType) { | 
|  | 785 | case AINPUT_EVENT_TYPE_KEY: { | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 786 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); | 
|  | 787 | EXPECT_EQ(expectedAction, keyEvent.getAction()); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 788 | if (expectedFlags.has_value()) { | 
|  | 789 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); | 
|  | 790 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 791 | break; | 
|  | 792 | } | 
|  | 793 | case AINPUT_EVENT_TYPE_MOTION: { | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 794 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); | 
|  | 795 | EXPECT_EQ(expectedAction, motionEvent.getAction()); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 796 | if (expectedFlags.has_value()) { | 
|  | 797 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); | 
|  | 798 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 799 | break; | 
|  | 800 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 801 | case AINPUT_EVENT_TYPE_FOCUS: { | 
|  | 802 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; | 
|  | 803 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 804 | case AINPUT_EVENT_TYPE_CAPTURE: { | 
|  | 805 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; | 
|  | 806 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 807 | case AINPUT_EVENT_TYPE_DRAG: { | 
|  | 808 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; | 
|  | 809 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 810 | default: { | 
|  | 811 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; | 
|  | 812 | } | 
|  | 813 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 814 | } | 
|  | 815 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 816 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { | 
|  | 817 | InputEvent* event = consume(); | 
|  | 818 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 819 | << ": consumer should have returned non-NULL event."; | 
|  | 820 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType()) | 
|  | 821 | << "Got " << inputEventTypeToString(event->getType()) | 
|  | 822 | << " event instead of FOCUS event"; | 
|  | 823 |  | 
|  | 824 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 825 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 826 |  | 
|  | 827 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); | 
|  | 828 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); | 
|  | 829 | EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode()); | 
|  | 830 | } | 
|  | 831 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 832 | void consumeCaptureEvent(bool hasCapture) { | 
|  | 833 | const InputEvent* event = consume(); | 
|  | 834 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 835 | << ": consumer should have returned non-NULL event."; | 
|  | 836 | ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType()) | 
|  | 837 | << "Got " << inputEventTypeToString(event->getType()) | 
|  | 838 | << " event instead of CAPTURE event"; | 
|  | 839 |  | 
|  | 840 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 841 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 842 |  | 
|  | 843 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); | 
|  | 844 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); | 
|  | 845 | } | 
|  | 846 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 847 | void consumeDragEvent(bool isExiting, float x, float y) { | 
|  | 848 | const InputEvent* event = consume(); | 
|  | 849 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 850 | << ": consumer should have returned non-NULL event."; | 
|  | 851 | ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType()) | 
|  | 852 | << "Got " << inputEventTypeToString(event->getType()) | 
|  | 853 | << " event instead of DRAG event"; | 
|  | 854 |  | 
|  | 855 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 856 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 857 |  | 
|  | 858 | const auto& dragEvent = static_cast<const DragEvent&>(*event); | 
|  | 859 | EXPECT_EQ(isExiting, dragEvent.isExiting()); | 
|  | 860 | EXPECT_EQ(x, dragEvent.getX()); | 
|  | 861 | EXPECT_EQ(y, dragEvent.getY()); | 
|  | 862 | } | 
|  | 863 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 864 | void assertNoEvents() { | 
|  | 865 | InputEvent* event = consume(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 866 | if (event == nullptr) { | 
|  | 867 | return; | 
|  | 868 | } | 
|  | 869 | if (event->getType() == AINPUT_EVENT_TYPE_KEY) { | 
|  | 870 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); | 
|  | 871 | ADD_FAILURE() << "Received key event " | 
|  | 872 | << KeyEvent::actionToString(keyEvent.getAction()); | 
|  | 873 | } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) { | 
|  | 874 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 875 | ADD_FAILURE() << "Received motion event " | 
|  | 876 | << MotionEvent::actionToString(motionEvent.getAction()); | 
|  | 877 | } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) { | 
|  | 878 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); | 
|  | 879 | ADD_FAILURE() << "Received focus event, hasFocus = " | 
|  | 880 | << (focusEvent.getHasFocus() ? "true" : "false"); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 881 | } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) { | 
|  | 882 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); | 
|  | 883 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " | 
|  | 884 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 885 | } | 
|  | 886 | FAIL() << mName.c_str() | 
|  | 887 | << ": should not have received any events, so consume() should return NULL"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 888 | } | 
|  | 889 |  | 
|  | 890 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } | 
|  | 891 |  | 
|  | 892 | protected: | 
|  | 893 | std::unique_ptr<InputConsumer> mConsumer; | 
|  | 894 | PreallocatedInputEventFactory mEventFactory; | 
|  | 895 |  | 
|  | 896 | std::string mName; | 
|  | 897 | }; | 
|  | 898 |  | 
|  | 899 | class FakeWindowHandle : public InputWindowHandle { | 
|  | 900 | public: | 
|  | 901 | static const int32_t WIDTH = 600; | 
|  | 902 | static const int32_t HEIGHT = 800; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 903 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 904 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 905 | const sp<InputDispatcher>& dispatcher, const std::string name, | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 906 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 907 | : mName(name) { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 908 | if (token == std::nullopt) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 909 | base::Result<std::unique_ptr<InputChannel>> channel = | 
|  | 910 | dispatcher->createInputChannel(name); | 
|  | 911 | token = (*channel)->getConnectionToken(); | 
|  | 912 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 913 | } | 
|  | 914 |  | 
|  | 915 | inputApplicationHandle->updateInfo(); | 
|  | 916 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); | 
|  | 917 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 918 | mInfo.token = *token; | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 919 | mInfo.id = sId++; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 920 | mInfo.name = name; | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 921 | mInfo.type = InputWindowInfo::Type::APPLICATION; | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 922 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 923 | mInfo.alpha = 1.0; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 924 | mInfo.frameLeft = 0; | 
|  | 925 | mInfo.frameTop = 0; | 
|  | 926 | mInfo.frameRight = WIDTH; | 
|  | 927 | mInfo.frameBottom = HEIGHT; | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 928 | mInfo.transform.set(0, 0); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 929 | mInfo.globalScaleFactor = 1.0; | 
|  | 930 | mInfo.touchableRegion.clear(); | 
|  | 931 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); | 
|  | 932 | mInfo.visible = true; | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 933 | mInfo.focusable = false; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 934 | mInfo.hasWallpaper = false; | 
|  | 935 | mInfo.paused = false; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 936 | mInfo.ownerPid = INJECTOR_PID; | 
|  | 937 | mInfo.ownerUid = INJECTOR_UID; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 938 | mInfo.displayId = displayId; | 
|  | 939 | } | 
|  | 940 |  | 
|  | 941 | virtual bool updateInfo() { return true; } | 
|  | 942 |  | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 943 | void setFocusable(bool focusable) { mInfo.focusable = focusable; } | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 944 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 945 | void setVisible(bool visible) { mInfo.visible = visible; } | 
|  | 946 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 947 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 948 | mInfo.dispatchingTimeout = timeout; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 949 | } | 
|  | 950 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 951 | void setPaused(bool paused) { mInfo.paused = paused; } | 
|  | 952 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 953 | void setAlpha(float alpha) { mInfo.alpha = alpha; } | 
|  | 954 |  | 
|  | 955 | void setTouchOcclusionMode(android::os::TouchOcclusionMode mode) { | 
|  | 956 | mInfo.touchOcclusionMode = mode; | 
|  | 957 | } | 
|  | 958 |  | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 959 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } | 
|  | 960 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 961 | void setFrame(const Rect& frame) { | 
|  | 962 | mInfo.frameLeft = frame.left; | 
|  | 963 | mInfo.frameTop = frame.top; | 
|  | 964 | mInfo.frameRight = frame.right; | 
|  | 965 | mInfo.frameBottom = frame.bottom; | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 966 | mInfo.transform.set(-frame.left, -frame.top); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 967 | mInfo.touchableRegion.clear(); | 
|  | 968 | mInfo.addTouchableRegion(frame); | 
|  | 969 | } | 
|  | 970 |  | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 971 | void addFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags |= flags; } | 
|  | 972 |  | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 973 | void setFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags = flags; } | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 974 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 975 | void setInputFeatures(InputWindowInfo::Feature features) { mInfo.inputFeatures = features; } | 
|  | 976 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 977 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { | 
|  | 978 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); | 
|  | 979 | } | 
|  | 980 |  | 
|  | 981 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 982 |  | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 983 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } | 
|  | 984 |  | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 985 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 986 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, | 
|  | 987 | expectedFlags); | 
|  | 988 | } | 
|  | 989 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 990 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 991 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); | 
|  | 992 | } | 
|  | 993 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 994 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 995 | int32_t expectedFlags = 0) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 996 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId, | 
|  | 997 | expectedFlags); | 
|  | 998 | } | 
|  | 999 |  | 
|  | 1000 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1001 | int32_t expectedFlags = 0) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1002 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId, | 
|  | 1003 | expectedFlags); | 
|  | 1004 | } | 
|  | 1005 |  | 
|  | 1006 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1007 | int32_t expectedFlags = 0) { | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1008 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); | 
|  | 1009 | } | 
|  | 1010 |  | 
|  | 1011 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, | 
|  | 1012 | std::optional<int32_t> expectedFlags = std::nullopt) { | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1013 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, | 
|  | 1014 | expectedFlags); | 
|  | 1015 | } | 
|  | 1016 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1017 | void consumeMotionPointerDown(int32_t pointerIdx, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1018 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
|  | 1019 | int32_t expectedFlags = 0) { | 
|  | 1020 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1021 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1022 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); | 
|  | 1023 | } | 
|  | 1024 |  | 
|  | 1025 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1026 | int32_t expectedFlags = 0) { | 
|  | 1027 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1028 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1029 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); | 
|  | 1030 | } | 
|  | 1031 |  | 
|  | 1032 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1033 | int32_t expectedFlags = 0) { | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1034 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, | 
|  | 1035 | expectedFlags); | 
|  | 1036 | } | 
|  | 1037 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1038 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
|  | 1039 | int32_t expectedFlags = 0) { | 
|  | 1040 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, | 
|  | 1041 | expectedFlags); | 
|  | 1042 | } | 
|  | 1043 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1044 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { | 
|  | 1045 | ASSERT_NE(mInputReceiver, nullptr) | 
|  | 1046 | << "Cannot consume events from a window with no receiver"; | 
|  | 1047 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); | 
|  | 1048 | } | 
|  | 1049 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1050 | void consumeCaptureEvent(bool hasCapture) { | 
|  | 1051 | ASSERT_NE(mInputReceiver, nullptr) | 
|  | 1052 | << "Cannot consume events from a window with no receiver"; | 
|  | 1053 | mInputReceiver->consumeCaptureEvent(hasCapture); | 
|  | 1054 | } | 
|  | 1055 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1056 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, | 
|  | 1057 | std::optional<int32_t> expectedDisplayId, | 
|  | 1058 | std::optional<int32_t> expectedFlags) { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1059 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; | 
|  | 1060 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, | 
|  | 1061 | expectedFlags); | 
|  | 1062 | } | 
|  | 1063 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1064 | void consumeDragEvent(bool isExiting, float x, float y) { | 
|  | 1065 | mInputReceiver->consumeDragEvent(isExiting, x, y); | 
|  | 1066 | } | 
|  | 1067 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1068 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1069 | if (mInputReceiver == nullptr) { | 
|  | 1070 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; | 
|  | 1071 | return std::nullopt; | 
|  | 1072 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1073 | return mInputReceiver->receiveEvent(outEvent); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1074 | } | 
|  | 1075 |  | 
|  | 1076 | void finishEvent(uint32_t sequenceNum) { | 
|  | 1077 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; | 
|  | 1078 | mInputReceiver->finishEvent(sequenceNum); | 
|  | 1079 | } | 
|  | 1080 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1081 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { | 
|  | 1082 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; | 
|  | 1083 | mInputReceiver->sendTimeline(inputEventId, timeline); | 
|  | 1084 | } | 
|  | 1085 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1086 | InputEvent* consume() { | 
|  | 1087 | if (mInputReceiver == nullptr) { | 
|  | 1088 | return nullptr; | 
|  | 1089 | } | 
|  | 1090 | return mInputReceiver->consume(); | 
|  | 1091 | } | 
|  | 1092 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1093 | MotionEvent* consumeMotion() { | 
|  | 1094 | InputEvent* event = consume(); | 
|  | 1095 | if (event == nullptr) { | 
|  | 1096 | ADD_FAILURE() << "Consume failed : no event"; | 
|  | 1097 | return nullptr; | 
|  | 1098 | } | 
|  | 1099 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { | 
|  | 1100 | ADD_FAILURE() << "Instead of motion event, got " | 
|  | 1101 | << inputEventTypeToString(event->getType()); | 
|  | 1102 | return nullptr; | 
|  | 1103 | } | 
|  | 1104 | return static_cast<MotionEvent*>(event); | 
|  | 1105 | } | 
|  | 1106 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1107 | void assertNoEvents() { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1108 | if (mInputReceiver == nullptr && | 
|  | 1109 | mInfo.inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL)) { | 
|  | 1110 | return; // Can't receive events if the window does not have input channel | 
|  | 1111 | } | 
|  | 1112 | ASSERT_NE(nullptr, mInputReceiver) | 
|  | 1113 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1114 | mInputReceiver->assertNoEvents(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1115 | } | 
|  | 1116 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1117 | sp<IBinder> getToken() { return mInfo.token; } | 
|  | 1118 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1119 | const std::string& getName() { return mName; } | 
|  | 1120 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1121 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { | 
|  | 1122 | mInfo.ownerPid = ownerPid; | 
|  | 1123 | mInfo.ownerUid = ownerUid; | 
|  | 1124 | } | 
|  | 1125 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1126 | private: | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1127 | const std::string mName; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1128 | std::unique_ptr<FakeInputReceiver> mInputReceiver; | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1129 | 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] | 1130 | }; | 
|  | 1131 |  | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1132 | std::atomic<int32_t> FakeWindowHandle::sId{1}; | 
|  | 1133 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1134 | static InputEventInjectionResult injectKey( | 
|  | 1135 | const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, | 
|  | 1136 | int32_t displayId = ADISPLAY_ID_NONE, | 
|  | 1137 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1138 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
|  | 1139 | bool allowKeyRepeat = true) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1140 | KeyEvent event; | 
|  | 1141 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1142 |  | 
|  | 1143 | // Define a valid key down event. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1144 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1145 | INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE, | 
|  | 1146 | repeatCount, currentTime, currentTime); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1147 |  | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1148 | int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; | 
|  | 1149 | if (!allowKeyRepeat) { | 
|  | 1150 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; | 
|  | 1151 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1152 | // Inject event until dispatch out. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1153 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1154 | injectionTimeout, policyFlags); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1155 | } | 
|  | 1156 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1157 | static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher, | 
|  | 1158 | int32_t displayId = ADISPLAY_ID_NONE) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1159 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId); | 
|  | 1160 | } | 
|  | 1161 |  | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1162 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without | 
|  | 1163 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it | 
|  | 1164 | // has to be woken up to process the repeating key. | 
|  | 1165 | static InputEventInjectionResult injectKeyDownNoRepeat(const sp<InputDispatcher>& dispatcher, | 
|  | 1166 | int32_t displayId = ADISPLAY_ID_NONE) { | 
|  | 1167 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId, | 
|  | 1168 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, | 
|  | 1169 | /* allowKeyRepeat */ false); | 
|  | 1170 | } | 
|  | 1171 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1172 | static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher, | 
|  | 1173 | int32_t displayId = ADISPLAY_ID_NONE) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1174 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId); | 
|  | 1175 | } | 
|  | 1176 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1177 | class PointerBuilder { | 
|  | 1178 | public: | 
|  | 1179 | PointerBuilder(int32_t id, int32_t toolType) { | 
|  | 1180 | mProperties.clear(); | 
|  | 1181 | mProperties.id = id; | 
|  | 1182 | mProperties.toolType = toolType; | 
|  | 1183 | mCoords.clear(); | 
|  | 1184 | } | 
|  | 1185 |  | 
|  | 1186 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } | 
|  | 1187 |  | 
|  | 1188 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } | 
|  | 1189 |  | 
|  | 1190 | PointerBuilder& axis(int32_t axis, float value) { | 
|  | 1191 | mCoords.setAxisValue(axis, value); | 
|  | 1192 | return *this; | 
|  | 1193 | } | 
|  | 1194 |  | 
|  | 1195 | PointerProperties buildProperties() const { return mProperties; } | 
|  | 1196 |  | 
|  | 1197 | PointerCoords buildCoords() const { return mCoords; } | 
|  | 1198 |  | 
|  | 1199 | private: | 
|  | 1200 | PointerProperties mProperties; | 
|  | 1201 | PointerCoords mCoords; | 
|  | 1202 | }; | 
|  | 1203 |  | 
|  | 1204 | class MotionEventBuilder { | 
|  | 1205 | public: | 
|  | 1206 | MotionEventBuilder(int32_t action, int32_t source) { | 
|  | 1207 | mAction = action; | 
|  | 1208 | mSource = source; | 
|  | 1209 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1210 | } | 
|  | 1211 |  | 
|  | 1212 | MotionEventBuilder& eventTime(nsecs_t eventTime) { | 
|  | 1213 | mEventTime = eventTime; | 
|  | 1214 | return *this; | 
|  | 1215 | } | 
|  | 1216 |  | 
|  | 1217 | MotionEventBuilder& displayId(int32_t displayId) { | 
|  | 1218 | mDisplayId = displayId; | 
|  | 1219 | return *this; | 
|  | 1220 | } | 
|  | 1221 |  | 
|  | 1222 | MotionEventBuilder& actionButton(int32_t actionButton) { | 
|  | 1223 | mActionButton = actionButton; | 
|  | 1224 | return *this; | 
|  | 1225 | } | 
|  | 1226 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1227 | MotionEventBuilder& buttonState(int32_t buttonState) { | 
|  | 1228 | mButtonState = buttonState; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1229 | return *this; | 
|  | 1230 | } | 
|  | 1231 |  | 
|  | 1232 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { | 
|  | 1233 | mRawXCursorPosition = rawXCursorPosition; | 
|  | 1234 | return *this; | 
|  | 1235 | } | 
|  | 1236 |  | 
|  | 1237 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { | 
|  | 1238 | mRawYCursorPosition = rawYCursorPosition; | 
|  | 1239 | return *this; | 
|  | 1240 | } | 
|  | 1241 |  | 
|  | 1242 | MotionEventBuilder& pointer(PointerBuilder pointer) { | 
|  | 1243 | mPointers.push_back(pointer); | 
|  | 1244 | return *this; | 
|  | 1245 | } | 
|  | 1246 |  | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1247 | MotionEventBuilder& addFlag(uint32_t flags) { | 
|  | 1248 | mFlags |= flags; | 
|  | 1249 | return *this; | 
|  | 1250 | } | 
|  | 1251 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1252 | MotionEvent build() { | 
|  | 1253 | std::vector<PointerProperties> pointerProperties; | 
|  | 1254 | std::vector<PointerCoords> pointerCoords; | 
|  | 1255 | for (const PointerBuilder& pointer : mPointers) { | 
|  | 1256 | pointerProperties.push_back(pointer.buildProperties()); | 
|  | 1257 | pointerCoords.push_back(pointer.buildCoords()); | 
|  | 1258 | } | 
|  | 1259 |  | 
|  | 1260 | // Set mouse cursor position for the most common cases to avoid boilerplate. | 
|  | 1261 | if (mSource == AINPUT_SOURCE_MOUSE && | 
|  | 1262 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && | 
|  | 1263 | mPointers.size() == 1) { | 
|  | 1264 | mRawXCursorPosition = pointerCoords[0].getX(); | 
|  | 1265 | mRawYCursorPosition = pointerCoords[0].getY(); | 
|  | 1266 | } | 
|  | 1267 |  | 
|  | 1268 | MotionEvent event; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1269 | ui::Transform identityTransform; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1270 | event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC, | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1271 | mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1272 | mButtonState, MotionClassification::NONE, identityTransform, | 
|  | 1273 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 1274 | mRawYCursorPosition, mDisplayWidth, mDisplayHeight, mEventTime, mEventTime, | 
|  | 1275 | mPointers.size(), pointerProperties.data(), pointerCoords.data()); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1276 |  | 
|  | 1277 | return event; | 
|  | 1278 | } | 
|  | 1279 |  | 
|  | 1280 | private: | 
|  | 1281 | int32_t mAction; | 
|  | 1282 | int32_t mSource; | 
|  | 1283 | nsecs_t mEventTime; | 
|  | 1284 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; | 
|  | 1285 | int32_t mActionButton{0}; | 
|  | 1286 | int32_t mButtonState{0}; | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1287 | int32_t mFlags{0}; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1288 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; | 
|  | 1289 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 1290 | int32_t mDisplayWidth{AMOTION_EVENT_INVALID_DISPLAY_SIZE}; | 
|  | 1291 | int32_t mDisplayHeight{AMOTION_EVENT_INVALID_DISPLAY_SIZE}; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1292 |  | 
|  | 1293 | std::vector<PointerBuilder> mPointers; | 
|  | 1294 | }; | 
|  | 1295 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1296 | static InputEventInjectionResult injectMotionEvent( | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1297 | const sp<InputDispatcher>& dispatcher, const MotionEvent& event, | 
|  | 1298 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1299 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) { | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1300 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode, | 
|  | 1301 | injectionTimeout, | 
|  | 1302 | POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); | 
|  | 1303 | } | 
|  | 1304 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1305 | static InputEventInjectionResult injectMotionEvent( | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1306 | const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId, | 
|  | 1307 | const PointF& position, | 
|  | 1308 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1309 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
|  | 1310 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1311 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1312 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) { | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1313 | MotionEvent event = MotionEventBuilder(action, source) | 
|  | 1314 | .displayId(displayId) | 
|  | 1315 | .eventTime(eventTime) | 
|  | 1316 | .rawXCursorPosition(cursorPosition.x) | 
|  | 1317 | .rawYCursorPosition(cursorPosition.y) | 
|  | 1318 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 1319 | .x(position.x) | 
|  | 1320 | .y(position.y)) | 
|  | 1321 | .build(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1322 |  | 
|  | 1323 | // Inject event until dispatch out. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1324 | return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1325 | } | 
|  | 1326 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1327 | static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher, | 
|  | 1328 | int32_t source, int32_t displayId, | 
|  | 1329 | const PointF& location = {100, 200}) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1330 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1331 | } | 
|  | 1332 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1333 | static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher, | 
|  | 1334 | int32_t source, int32_t displayId, | 
|  | 1335 | const PointF& location = {100, 200}) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1336 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1337 | } | 
|  | 1338 |  | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1339 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { | 
|  | 1340 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1341 | // Define a valid key event. | 
| Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1342 | NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, | 
|  | 1343 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, | 
|  | 1344 | KEY_A, AMETA_NONE, currentTime); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1345 |  | 
|  | 1346 | return args; | 
|  | 1347 | } | 
|  | 1348 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1349 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, | 
|  | 1350 | const std::vector<PointF>& points) { | 
|  | 1351 | size_t pointerCount = points.size(); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1352 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { | 
|  | 1353 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; | 
|  | 1354 | } | 
|  | 1355 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1356 | PointerProperties pointerProperties[pointerCount]; | 
|  | 1357 | PointerCoords pointerCoords[pointerCount]; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1358 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1359 | for (size_t i = 0; i < pointerCount; i++) { | 
|  | 1360 | pointerProperties[i].clear(); | 
|  | 1361 | pointerProperties[i].id = i; | 
|  | 1362 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1363 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1364 | pointerCoords[i].clear(); | 
|  | 1365 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); | 
|  | 1366 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); | 
|  | 1367 | } | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1368 |  | 
|  | 1369 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1370 | // Define a valid motion event. | 
| Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1371 | NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1372 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, | 
|  | 1373 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1374 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, | 
|  | 1375 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1376 | AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 1377 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1378 |  | 
|  | 1379 | return args; | 
|  | 1380 | } | 
|  | 1381 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1382 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { | 
|  | 1383 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); | 
|  | 1384 | } | 
|  | 1385 |  | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1386 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( | 
|  | 1387 | const PointerCaptureRequest& request) { | 
|  | 1388 | return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), request); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1389 | } | 
|  | 1390 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1391 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1392 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1393 | sp<FakeWindowHandle> window = | 
|  | 1394 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1395 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1396 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1397 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1398 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 1399 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1400 |  | 
|  | 1401 | // Window should receive motion event. | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1402 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1403 | } | 
|  | 1404 |  | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1405 | /** | 
|  | 1406 | * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues. | 
|  | 1407 | * To ensure that window receives only events that were directly inside of it, add | 
|  | 1408 | * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input | 
|  | 1409 | * when finding touched windows. | 
|  | 1410 | * This test serves as a sanity check for the next test, where setInputWindows is | 
|  | 1411 | * called twice. | 
|  | 1412 | */ | 
|  | 1413 | TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1414 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1415 | sp<FakeWindowHandle> window = | 
|  | 1416 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 1417 | window->setFrame(Rect(0, 0, 100, 100)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1418 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1419 |  | 
|  | 1420 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1421 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1422 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1423 | {50, 50})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1424 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1425 |  | 
|  | 1426 | // Window should receive motion event. | 
|  | 1427 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1428 | } | 
|  | 1429 |  | 
|  | 1430 | /** | 
|  | 1431 | * Calling setInputWindows twice, with the same info, should not cause any issues. | 
|  | 1432 | * To ensure that window receives only events that were directly inside of it, add | 
|  | 1433 | * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input | 
|  | 1434 | * when finding touched windows. | 
|  | 1435 | */ | 
|  | 1436 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1437 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1438 | sp<FakeWindowHandle> window = | 
|  | 1439 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 1440 | window->setFrame(Rect(0, 0, 100, 100)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1441 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1442 |  | 
|  | 1443 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 1444 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1445 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1446 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1447 | {50, 50})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1448 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1449 |  | 
|  | 1450 | // Window should receive motion event. | 
|  | 1451 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1452 | } | 
|  | 1453 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1454 | // The foreground window should receive the first touch down event. | 
|  | 1455 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1456 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1457 | sp<FakeWindowHandle> windowTop = | 
|  | 1458 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 1459 | sp<FakeWindowHandle> windowSecond = | 
|  | 1460 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1461 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1462 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1463 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1464 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 1465 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1466 |  | 
|  | 1467 | // 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] | 1468 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1469 | windowSecond->assertNoEvents(); | 
|  | 1470 | } | 
|  | 1471 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1472 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1473 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1474 | sp<FakeWindowHandle> windowLeft = | 
|  | 1475 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
|  | 1476 | windowLeft->setFrame(Rect(0, 0, 600, 800)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1477 | windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1478 | sp<FakeWindowHandle> windowRight = | 
|  | 1479 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
|  | 1480 | windowRight->setFrame(Rect(600, 0, 1200, 800)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1481 | windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1482 |  | 
|  | 1483 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 1484 |  | 
|  | 1485 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); | 
|  | 1486 |  | 
|  | 1487 | // 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] | 1488 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1489 | injectMotionEvent(mDispatcher, | 
|  | 1490 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1491 | AINPUT_SOURCE_MOUSE) | 
|  | 1492 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1493 | .x(900) | 
|  | 1494 | .y(400)) | 
|  | 1495 | .build())); | 
|  | 1496 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1497 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1498 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1499 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1500 |  | 
|  | 1501 | // Move cursor into left window | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1502 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1503 | injectMotionEvent(mDispatcher, | 
|  | 1504 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1505 | AINPUT_SOURCE_MOUSE) | 
|  | 1506 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1507 | .x(300) | 
|  | 1508 | .y(400)) | 
|  | 1509 | .build())); | 
|  | 1510 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 1511 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1512 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1513 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1514 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1515 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1516 |  | 
|  | 1517 | // Inject a series of mouse events for a mouse click | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1518 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1519 | injectMotionEvent(mDispatcher, | 
|  | 1520 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 1521 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1522 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1523 | .x(300) | 
|  | 1524 | .y(400)) | 
|  | 1525 | .build())); | 
|  | 1526 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1527 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1528 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1529 | injectMotionEvent(mDispatcher, | 
|  | 1530 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 1531 | AINPUT_SOURCE_MOUSE) | 
|  | 1532 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1533 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1534 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1535 | .x(300) | 
|  | 1536 | .y(400)) | 
|  | 1537 | .build())); | 
|  | 1538 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 1539 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1540 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1541 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1542 | injectMotionEvent(mDispatcher, | 
|  | 1543 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 1544 | AINPUT_SOURCE_MOUSE) | 
|  | 1545 | .buttonState(0) | 
|  | 1546 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1547 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1548 | .x(300) | 
|  | 1549 | .y(400)) | 
|  | 1550 | .build())); | 
|  | 1551 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 1552 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1553 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1554 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1555 | injectMotionEvent(mDispatcher, | 
|  | 1556 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) | 
|  | 1557 | .buttonState(0) | 
|  | 1558 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1559 | .x(300) | 
|  | 1560 | .y(400)) | 
|  | 1561 | .build())); | 
|  | 1562 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 1563 |  | 
|  | 1564 | // Move mouse cursor back to right window | 
| 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_HOVER_MOVE, | 
|  | 1568 | AINPUT_SOURCE_MOUSE) | 
|  | 1569 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1570 | .x(900) | 
|  | 1571 | .y(400)) | 
|  | 1572 | .build())); | 
|  | 1573 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 1574 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1575 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1576 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1577 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1578 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1579 | } | 
|  | 1580 |  | 
|  | 1581 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected | 
|  | 1582 | // directly in this test. | 
|  | 1583 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1584 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1585 | sp<FakeWindowHandle> window = | 
|  | 1586 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 1587 | window->setFrame(Rect(0, 0, 1200, 800)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1588 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1589 |  | 
|  | 1590 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 1591 |  | 
|  | 1592 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 1593 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1594 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1595 | injectMotionEvent(mDispatcher, | 
|  | 1596 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1597 | AINPUT_SOURCE_MOUSE) | 
|  | 1598 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1599 | .x(300) | 
|  | 1600 | .y(400)) | 
|  | 1601 | .build())); | 
|  | 1602 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1603 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1604 |  | 
|  | 1605 | // Inject a series of mouse events for a mouse click | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1606 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1607 | injectMotionEvent(mDispatcher, | 
|  | 1608 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 1609 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1610 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1611 | .x(300) | 
|  | 1612 | .y(400)) | 
|  | 1613 | .build())); | 
|  | 1614 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1615 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1616 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1617 | injectMotionEvent(mDispatcher, | 
|  | 1618 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 1619 | AINPUT_SOURCE_MOUSE) | 
|  | 1620 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1621 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1622 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1623 | .x(300) | 
|  | 1624 | .y(400)) | 
|  | 1625 | .build())); | 
|  | 1626 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 1627 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1628 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1629 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1630 | injectMotionEvent(mDispatcher, | 
|  | 1631 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 1632 | AINPUT_SOURCE_MOUSE) | 
|  | 1633 | .buttonState(0) | 
|  | 1634 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1635 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1636 | .x(300) | 
|  | 1637 | .y(400)) | 
|  | 1638 | .build())); | 
|  | 1639 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 1640 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1641 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1642 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1643 | injectMotionEvent(mDispatcher, | 
|  | 1644 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) | 
|  | 1645 | .buttonState(0) | 
|  | 1646 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1647 | .x(300) | 
|  | 1648 | .y(400)) | 
|  | 1649 | .build())); | 
|  | 1650 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 1651 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1652 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1653 | injectMotionEvent(mDispatcher, | 
|  | 1654 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 1655 | AINPUT_SOURCE_MOUSE) | 
|  | 1656 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1657 | .x(300) | 
|  | 1658 | .y(400)) | 
|  | 1659 | .build())); | 
|  | 1660 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 1661 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1662 | } | 
|  | 1663 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1664 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1665 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1666 |  | 
|  | 1667 | sp<FakeWindowHandle> windowLeft = | 
|  | 1668 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
|  | 1669 | windowLeft->setFrame(Rect(0, 0, 600, 800)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1670 | windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1671 | sp<FakeWindowHandle> windowRight = | 
|  | 1672 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
|  | 1673 | windowRight->setFrame(Rect(600, 0, 1200, 800)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1674 | windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1675 |  | 
|  | 1676 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 1677 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1678 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1679 |  | 
|  | 1680 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of | 
|  | 1681 | // left window. This event should be dispatched to the left window. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1682 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1683 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1684 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1685 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1686 | windowRight->assertNoEvents(); | 
|  | 1687 | } | 
|  | 1688 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1689 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1690 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1691 | sp<FakeWindowHandle> window = | 
|  | 1692 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1693 | window->setFocusable(true); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1694 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1695 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1696 | setFocusedWindow(window); | 
|  | 1697 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1698 | window->consumeFocusEvent(true); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1699 |  | 
|  | 1700 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 1701 | mDispatcher->notifyKey(&keyArgs); | 
|  | 1702 |  | 
|  | 1703 | // Window should receive key down event. | 
|  | 1704 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 1705 |  | 
|  | 1706 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED | 
|  | 1707 | // on the app side. | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1708 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1709 | mDispatcher->notifyDeviceReset(&args); | 
|  | 1710 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, | 
|  | 1711 | AKEY_EVENT_FLAG_CANCELED); | 
|  | 1712 | } | 
|  | 1713 |  | 
|  | 1714 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1715 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1716 | sp<FakeWindowHandle> window = | 
|  | 1717 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 1718 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1719 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1720 |  | 
|  | 1721 | NotifyMotionArgs motionArgs = | 
|  | 1722 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1723 | ADISPLAY_ID_DEFAULT); | 
|  | 1724 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 1725 |  | 
|  | 1726 | // Window should receive motion down event. | 
|  | 1727 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1728 |  | 
|  | 1729 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL | 
|  | 1730 | // on the app side. | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1731 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1732 | mDispatcher->notifyDeviceReset(&args); | 
|  | 1733 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, | 
|  | 1734 | 0 /*expectedFlags*/); | 
|  | 1735 | } | 
|  | 1736 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1737 | using TransferFunction = | 
|  | 1738 | std::function<bool(sp<InputDispatcher> dispatcher, sp<IBinder>, sp<IBinder>)>; | 
|  | 1739 |  | 
|  | 1740 | class TransferTouchFixture : public InputDispatcherTest, | 
|  | 1741 | public ::testing::WithParamInterface<TransferFunction> {}; | 
|  | 1742 |  | 
|  | 1743 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1744 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1745 |  | 
|  | 1746 | // Create a couple of windows | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1747 | sp<FakeWindowHandle> firstWindow = | 
|  | 1748 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
|  | 1749 | sp<FakeWindowHandle> secondWindow = | 
|  | 1750 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1751 |  | 
|  | 1752 | // Add the windows to the dispatcher | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1753 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1754 |  | 
|  | 1755 | // Send down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1756 | NotifyMotionArgs downMotionArgs = | 
|  | 1757 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1758 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1759 | mDispatcher->notifyMotion(&downMotionArgs); | 
|  | 1760 | // Only the first window should get the down event | 
|  | 1761 | firstWindow->consumeMotionDown(); | 
|  | 1762 | secondWindow->assertNoEvents(); | 
|  | 1763 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1764 | // Transfer touch to the second window | 
|  | 1765 | TransferFunction f = GetParam(); | 
|  | 1766 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); | 
|  | 1767 | ASSERT_TRUE(success); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1768 | // The first window gets cancel and the second gets down | 
|  | 1769 | firstWindow->consumeMotionCancel(); | 
|  | 1770 | secondWindow->consumeMotionDown(); | 
|  | 1771 |  | 
|  | 1772 | // Send up event to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1773 | NotifyMotionArgs upMotionArgs = | 
|  | 1774 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1775 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1776 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 1777 | // The first  window gets no events and the second gets up | 
|  | 1778 | firstWindow->assertNoEvents(); | 
|  | 1779 | secondWindow->consumeMotionUp(); | 
|  | 1780 | } | 
|  | 1781 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1782 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1783 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1784 |  | 
|  | 1785 | PointF touchPoint = {10, 10}; | 
|  | 1786 |  | 
|  | 1787 | // Create a couple of windows | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1788 | sp<FakeWindowHandle> firstWindow = | 
|  | 1789 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
|  | 1790 | sp<FakeWindowHandle> secondWindow = | 
|  | 1791 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1792 |  | 
|  | 1793 | // Add the windows to the dispatcher | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1794 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1795 |  | 
|  | 1796 | // Send down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1797 | NotifyMotionArgs downMotionArgs = | 
|  | 1798 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1799 | ADISPLAY_ID_DEFAULT, {touchPoint}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1800 | mDispatcher->notifyMotion(&downMotionArgs); | 
|  | 1801 | // Only the first window should get the down event | 
|  | 1802 | firstWindow->consumeMotionDown(); | 
|  | 1803 | secondWindow->assertNoEvents(); | 
|  | 1804 |  | 
|  | 1805 | // Send pointer down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1806 | NotifyMotionArgs pointerDownMotionArgs = | 
|  | 1807 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1808 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1809 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1810 | {touchPoint, touchPoint}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1811 | mDispatcher->notifyMotion(&pointerDownMotionArgs); | 
|  | 1812 | // Only the first window should get the pointer down event | 
|  | 1813 | firstWindow->consumeMotionPointerDown(1); | 
|  | 1814 | secondWindow->assertNoEvents(); | 
|  | 1815 |  | 
|  | 1816 | // Transfer touch focus to the second window | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1817 | TransferFunction f = GetParam(); | 
|  | 1818 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); | 
|  | 1819 | ASSERT_TRUE(success); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1820 | // The first window gets cancel and the second gets down and pointer down | 
|  | 1821 | firstWindow->consumeMotionCancel(); | 
|  | 1822 | secondWindow->consumeMotionDown(); | 
|  | 1823 | secondWindow->consumeMotionPointerDown(1); | 
|  | 1824 |  | 
|  | 1825 | // Send pointer up to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1826 | NotifyMotionArgs pointerUpMotionArgs = | 
|  | 1827 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1828 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1829 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1830 | {touchPoint, touchPoint}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1831 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 1832 | // The first window gets nothing and the second gets pointer up | 
|  | 1833 | firstWindow->assertNoEvents(); | 
|  | 1834 | secondWindow->consumeMotionPointerUp(1); | 
|  | 1835 |  | 
|  | 1836 | // Send up event to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1837 | NotifyMotionArgs upMotionArgs = | 
|  | 1838 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1839 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1840 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 1841 | // The first window gets nothing and the second gets up | 
|  | 1842 | firstWindow->assertNoEvents(); | 
|  | 1843 | secondWindow->consumeMotionUp(); | 
|  | 1844 | } | 
|  | 1845 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1846 | // For the cases of single pointer touch and two pointers non-split touch, the api's | 
|  | 1847 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ | 
|  | 1848 | // for the case where there are multiple pointers split across several windows. | 
|  | 1849 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, | 
|  | 1850 | ::testing::Values( | 
|  | 1851 | [&](sp<InputDispatcher> dispatcher, sp<IBinder> /*ignored*/, | 
|  | 1852 | sp<IBinder> destChannelToken) { | 
|  | 1853 | return dispatcher->transferTouch(destChannelToken); | 
|  | 1854 | }, | 
|  | 1855 | [&](sp<InputDispatcher> dispatcher, sp<IBinder> from, | 
|  | 1856 | sp<IBinder> to) { | 
|  | 1857 | return dispatcher->transferTouchFocus(from, to, | 
|  | 1858 | false /*isDragAndDrop*/); | 
|  | 1859 | })); | 
|  | 1860 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1861 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1862 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1863 |  | 
|  | 1864 | // Create a non touch modal window that supports split touch | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1865 | sp<FakeWindowHandle> firstWindow = | 
|  | 1866 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1867 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1868 | firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 1869 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1870 |  | 
|  | 1871 | // Create a non touch modal window that supports split touch | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1872 | sp<FakeWindowHandle> secondWindow = | 
|  | 1873 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1874 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1875 | secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 1876 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1877 |  | 
|  | 1878 | // Add the windows to the dispatcher | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1879 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1880 |  | 
|  | 1881 | PointF pointInFirst = {300, 200}; | 
|  | 1882 | PointF pointInSecond = {300, 600}; | 
|  | 1883 |  | 
|  | 1884 | // Send down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1885 | NotifyMotionArgs firstDownMotionArgs = | 
|  | 1886 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1887 | ADISPLAY_ID_DEFAULT, {pointInFirst}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1888 | mDispatcher->notifyMotion(&firstDownMotionArgs); | 
|  | 1889 | // Only the first window should get the down event | 
|  | 1890 | firstWindow->consumeMotionDown(); | 
|  | 1891 | secondWindow->assertNoEvents(); | 
|  | 1892 |  | 
|  | 1893 | // Send down to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1894 | NotifyMotionArgs secondDownMotionArgs = | 
|  | 1895 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1896 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1897 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1898 | {pointInFirst, pointInSecond}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1899 | mDispatcher->notifyMotion(&secondDownMotionArgs); | 
|  | 1900 | // The first window gets a move and the second a down | 
|  | 1901 | firstWindow->consumeMotionMove(); | 
|  | 1902 | secondWindow->consumeMotionDown(); | 
|  | 1903 |  | 
|  | 1904 | // Transfer touch focus to the second window | 
|  | 1905 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); | 
|  | 1906 | // The first window gets cancel and the new gets pointer down (it already saw down) | 
|  | 1907 | firstWindow->consumeMotionCancel(); | 
|  | 1908 | secondWindow->consumeMotionPointerDown(1); | 
|  | 1909 |  | 
|  | 1910 | // Send pointer up to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1911 | NotifyMotionArgs pointerUpMotionArgs = | 
|  | 1912 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1913 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1914 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1915 | {pointInFirst, pointInSecond}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1916 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 1917 | // The first window gets nothing and the second gets pointer up | 
|  | 1918 | firstWindow->assertNoEvents(); | 
|  | 1919 | secondWindow->consumeMotionPointerUp(1); | 
|  | 1920 |  | 
|  | 1921 | // Send up event to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1922 | NotifyMotionArgs upMotionArgs = | 
|  | 1923 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1924 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1925 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 1926 | // The first window gets nothing and the second gets up | 
|  | 1927 | firstWindow->assertNoEvents(); | 
|  | 1928 | secondWindow->consumeMotionUp(); | 
|  | 1929 | } | 
|  | 1930 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1931 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. | 
|  | 1932 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving | 
|  | 1933 | // touch is not supported, so the touch should continue on those windows and the transferred-to | 
|  | 1934 | // window should get nothing. | 
|  | 1935 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { | 
|  | 1936 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 1937 |  | 
|  | 1938 | // Create a non touch modal window that supports split touch | 
|  | 1939 | sp<FakeWindowHandle> firstWindow = | 
|  | 1940 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
|  | 1941 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
|  | 1942 | firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 1943 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
|  | 1944 |  | 
|  | 1945 | // Create a non touch modal window that supports split touch | 
|  | 1946 | sp<FakeWindowHandle> secondWindow = | 
|  | 1947 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
|  | 1948 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
|  | 1949 | secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 1950 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
|  | 1951 |  | 
|  | 1952 | // Add the windows to the dispatcher | 
|  | 1953 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
|  | 1954 |  | 
|  | 1955 | PointF pointInFirst = {300, 200}; | 
|  | 1956 | PointF pointInSecond = {300, 600}; | 
|  | 1957 |  | 
|  | 1958 | // Send down to the first window | 
|  | 1959 | NotifyMotionArgs firstDownMotionArgs = | 
|  | 1960 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1961 | ADISPLAY_ID_DEFAULT, {pointInFirst}); | 
|  | 1962 | mDispatcher->notifyMotion(&firstDownMotionArgs); | 
|  | 1963 | // Only the first window should get the down event | 
|  | 1964 | firstWindow->consumeMotionDown(); | 
|  | 1965 | secondWindow->assertNoEvents(); | 
|  | 1966 |  | 
|  | 1967 | // Send down to the second window | 
|  | 1968 | NotifyMotionArgs secondDownMotionArgs = | 
|  | 1969 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1970 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1971 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1972 | {pointInFirst, pointInSecond}); | 
|  | 1973 | mDispatcher->notifyMotion(&secondDownMotionArgs); | 
|  | 1974 | // The first window gets a move and the second a down | 
|  | 1975 | firstWindow->consumeMotionMove(); | 
|  | 1976 | secondWindow->consumeMotionDown(); | 
|  | 1977 |  | 
|  | 1978 | // Transfer touch focus to the second window | 
|  | 1979 | const bool transferred = mDispatcher->transferTouch(secondWindow->getToken()); | 
|  | 1980 | // The 'transferTouch' call should not succeed, because there are 2 touched windows | 
|  | 1981 | ASSERT_FALSE(transferred); | 
|  | 1982 | firstWindow->assertNoEvents(); | 
|  | 1983 | secondWindow->assertNoEvents(); | 
|  | 1984 |  | 
|  | 1985 | // The rest of the dispatch should proceed as normal | 
|  | 1986 | // Send pointer up to the second window | 
|  | 1987 | NotifyMotionArgs pointerUpMotionArgs = | 
|  | 1988 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1989 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1990 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1991 | {pointInFirst, pointInSecond}); | 
|  | 1992 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 1993 | // The first window gets MOVE and the second gets pointer up | 
|  | 1994 | firstWindow->consumeMotionMove(); | 
|  | 1995 | secondWindow->consumeMotionUp(); | 
|  | 1996 |  | 
|  | 1997 | // Send up event to the first window | 
|  | 1998 | NotifyMotionArgs upMotionArgs = | 
|  | 1999 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2000 | ADISPLAY_ID_DEFAULT); | 
|  | 2001 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 2002 | // The first window gets nothing and the second gets up | 
|  | 2003 | firstWindow->consumeMotionUp(); | 
|  | 2004 | secondWindow->assertNoEvents(); | 
|  | 2005 | } | 
|  | 2006 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2007 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2008 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2009 | sp<FakeWindowHandle> window = | 
|  | 2010 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2011 |  | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2012 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2013 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2014 | setFocusedWindow(window); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2015 |  | 
|  | 2016 | window->consumeFocusEvent(true); | 
|  | 2017 |  | 
|  | 2018 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 2019 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2020 |  | 
|  | 2021 | // Window should receive key down event. | 
|  | 2022 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 2023 | } | 
|  | 2024 |  | 
|  | 2025 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2026 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2027 | sp<FakeWindowHandle> window = | 
|  | 2028 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2029 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2030 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2031 |  | 
|  | 2032 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 2033 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2034 | mDispatcher->waitForIdle(); | 
|  | 2035 |  | 
|  | 2036 | window->assertNoEvents(); | 
|  | 2037 | } | 
|  | 2038 |  | 
|  | 2039 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys | 
|  | 2040 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2041 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2042 | sp<FakeWindowHandle> window = | 
|  | 2043 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2044 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2045 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2046 |  | 
|  | 2047 | // Send key | 
|  | 2048 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 2049 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2050 | // Send motion | 
|  | 2051 | NotifyMotionArgs motionArgs = | 
|  | 2052 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2053 | ADISPLAY_ID_DEFAULT); | 
|  | 2054 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2055 |  | 
|  | 2056 | // Window should receive only the motion event | 
|  | 2057 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2058 | window->assertNoEvents(); // Key event or focus event will not be received | 
|  | 2059 | } | 
|  | 2060 |  | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2061 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { | 
|  | 2062 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2063 |  | 
|  | 2064 | // Create first non touch modal window that supports split touch | 
|  | 2065 | sp<FakeWindowHandle> firstWindow = | 
|  | 2066 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
|  | 2067 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
|  | 2068 | firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 2069 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
|  | 2070 |  | 
|  | 2071 | // Create second non touch modal window that supports split touch | 
|  | 2072 | sp<FakeWindowHandle> secondWindow = | 
|  | 2073 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
|  | 2074 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
|  | 2075 | secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 2076 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
|  | 2077 |  | 
|  | 2078 | // Add the windows to the dispatcher | 
|  | 2079 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
|  | 2080 |  | 
|  | 2081 | PointF pointInFirst = {300, 200}; | 
|  | 2082 | PointF pointInSecond = {300, 600}; | 
|  | 2083 |  | 
|  | 2084 | // Send down to the first window | 
|  | 2085 | NotifyMotionArgs firstDownMotionArgs = | 
|  | 2086 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2087 | ADISPLAY_ID_DEFAULT, {pointInFirst}); | 
|  | 2088 | mDispatcher->notifyMotion(&firstDownMotionArgs); | 
|  | 2089 | // Only the first window should get the down event | 
|  | 2090 | firstWindow->consumeMotionDown(); | 
|  | 2091 | secondWindow->assertNoEvents(); | 
|  | 2092 |  | 
|  | 2093 | // Send down to the second window | 
|  | 2094 | NotifyMotionArgs secondDownMotionArgs = | 
|  | 2095 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 2096 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 2097 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 2098 | {pointInFirst, pointInSecond}); | 
|  | 2099 | mDispatcher->notifyMotion(&secondDownMotionArgs); | 
|  | 2100 | // The first window gets a move and the second a down | 
|  | 2101 | firstWindow->consumeMotionMove(); | 
|  | 2102 | secondWindow->consumeMotionDown(); | 
|  | 2103 |  | 
|  | 2104 | // Send pointer cancel to the second window | 
|  | 2105 | NotifyMotionArgs pointerUpMotionArgs = | 
|  | 2106 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 2107 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 2108 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 2109 | {pointInFirst, pointInSecond}); | 
|  | 2110 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; | 
|  | 2111 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 2112 | // The first window gets move and the second gets cancel. | 
|  | 2113 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); | 
|  | 2114 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); | 
|  | 2115 |  | 
|  | 2116 | // Send up event. | 
|  | 2117 | NotifyMotionArgs upMotionArgs = | 
|  | 2118 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2119 | ADISPLAY_ID_DEFAULT); | 
|  | 2120 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 2121 | // The first window gets up and the second gets nothing. | 
|  | 2122 | firstWindow->consumeMotionUp(); | 
|  | 2123 | secondWindow->assertNoEvents(); | 
|  | 2124 | } | 
|  | 2125 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 2126 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { | 
|  | 2127 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2128 |  | 
|  | 2129 | sp<FakeWindowHandle> window = | 
|  | 2130 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 2131 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2132 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; | 
|  | 2133 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; | 
|  | 2134 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; | 
|  | 2135 |  | 
|  | 2136 | window->sendTimeline(1 /*inputEventId*/, graphicsTimeline); | 
|  | 2137 | window->assertNoEvents(); | 
|  | 2138 | mDispatcher->waitForIdle(); | 
|  | 2139 | } | 
|  | 2140 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2141 | class FakeMonitorReceiver { | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2142 | public: | 
|  | 2143 | FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name, | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2144 | int32_t displayId, bool isGestureMonitor = false) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 2145 | base::Result<std::unique_ptr<InputChannel>> channel = | 
| Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 2146 | dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 2147 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2148 | } | 
|  | 2149 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2150 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } | 
|  | 2151 |  | 
|  | 2152 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 2153 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, | 
|  | 2154 | expectedDisplayId, expectedFlags); | 
|  | 2155 | } | 
|  | 2156 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2157 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } | 
|  | 2158 |  | 
|  | 2159 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } | 
|  | 2160 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2161 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 2162 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, | 
|  | 2163 | expectedDisplayId, expectedFlags); | 
|  | 2164 | } | 
|  | 2165 |  | 
|  | 2166 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 2167 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, | 
|  | 2168 | expectedDisplayId, expectedFlags); | 
|  | 2169 | } | 
|  | 2170 |  | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2171 | MotionEvent* consumeMotion() { | 
|  | 2172 | InputEvent* event = mInputReceiver->consume(); | 
|  | 2173 | if (!event) { | 
|  | 2174 | ADD_FAILURE() << "No event was produced"; | 
|  | 2175 | return nullptr; | 
|  | 2176 | } | 
|  | 2177 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { | 
|  | 2178 | ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion"; | 
|  | 2179 | return nullptr; | 
|  | 2180 | } | 
|  | 2181 | return static_cast<MotionEvent*>(event); | 
|  | 2182 | } | 
|  | 2183 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2184 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } | 
|  | 2185 |  | 
|  | 2186 | private: | 
|  | 2187 | std::unique_ptr<FakeInputReceiver> mInputReceiver; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2188 | }; | 
|  | 2189 |  | 
|  | 2190 | // Tests for gesture monitors | 
|  | 2191 | TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2192 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2193 | sp<FakeWindowHandle> window = | 
|  | 2194 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2195 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2196 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2197 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2198 | true /*isGestureMonitor*/); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2199 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2200 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2201 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2202 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2203 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2204 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2205 | } | 
|  | 2206 |  | 
|  | 2207 | TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2208 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2209 | sp<FakeWindowHandle> window = | 
|  | 2210 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2211 |  | 
|  | 2212 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2213 | window->setFocusable(true); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2214 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2215 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2216 | setFocusedWindow(window); | 
|  | 2217 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2218 | window->consumeFocusEvent(true); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2219 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2220 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2221 | true /*isGestureMonitor*/); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2222 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2223 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
|  | 2224 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2225 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2226 | monitor.assertNoEvents(); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2227 | } | 
|  | 2228 |  | 
|  | 2229 | TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2230 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2231 | sp<FakeWindowHandle> window = | 
|  | 2232 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2233 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2234 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2235 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2236 | true /*isGestureMonitor*/); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2237 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2238 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2239 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2240 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2241 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2242 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2243 |  | 
|  | 2244 | window->releaseChannel(); | 
|  | 2245 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2246 | mDispatcher->pilferPointers(monitor.getToken()); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2247 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2248 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2249 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2250 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2251 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2252 | } | 
|  | 2253 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2254 | TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) { | 
|  | 2255 | FakeMonitorReceiver monitor = | 
|  | 2256 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, | 
|  | 2257 | true /*isGestureMonitor*/); | 
|  | 2258 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2259 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2260 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); | 
|  | 2261 | std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); | 
|  | 2262 | ASSERT_TRUE(consumeSeq); | 
|  | 2263 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 2264 | mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2265 | monitor.finishEvent(*consumeSeq); | 
|  | 2266 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 2267 | mFakePolicy->assertNotifyMonitorResponsiveWasCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2268 | } | 
|  | 2269 |  | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2270 | // Tests for gesture monitors | 
|  | 2271 | TEST_F(InputDispatcherTest, GestureMonitor_NoWindowTransform) { | 
|  | 2272 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2273 | sp<FakeWindowHandle> window = | 
|  | 2274 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2275 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2276 | window->setWindowOffset(20, 40); | 
|  | 2277 | window->setWindowTransform(0, 1, -1, 0); | 
|  | 2278 |  | 
|  | 2279 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2280 | true /*isGestureMonitor*/); | 
|  | 2281 |  | 
|  | 2282 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2283 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 2284 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 2285 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2286 | MotionEvent* event = monitor.consumeMotion(); | 
|  | 2287 | // Even though window has transform, gesture monitor must not. | 
|  | 2288 | ASSERT_EQ(ui::Transform(), event->getTransform()); | 
|  | 2289 | } | 
|  | 2290 |  | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2291 | TEST_F(InputDispatcherTest, TestMoveEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2292 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2293 | sp<FakeWindowHandle> window = | 
|  | 2294 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2295 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2296 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2297 |  | 
|  | 2298 | NotifyMotionArgs motionArgs = | 
|  | 2299 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2300 | ADISPLAY_ID_DEFAULT); | 
|  | 2301 |  | 
|  | 2302 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2303 | // Window should receive motion down event. | 
|  | 2304 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2305 |  | 
|  | 2306 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2307 | motionArgs.id += 1; | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2308 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 2309 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, | 
|  | 2310 | motionArgs.pointerCoords[0].getX() - 10); | 
|  | 2311 |  | 
|  | 2312 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2313 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, | 
|  | 2314 | 0 /*expectedFlags*/); | 
|  | 2315 | } | 
|  | 2316 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2317 | /** | 
|  | 2318 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to | 
|  | 2319 | * the device default right away. In the test scenario, we check both the default value, | 
|  | 2320 | * and the action of enabling / disabling. | 
|  | 2321 | */ | 
|  | 2322 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2323 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2324 | sp<FakeWindowHandle> window = | 
|  | 2325 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 2326 |  | 
|  | 2327 | // Set focused application. | 
|  | 2328 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2329 | window->setFocusable(true); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2330 |  | 
|  | 2331 | SCOPED_TRACE("Check default value of touch mode"); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2332 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2333 | setFocusedWindow(window); | 
|  | 2334 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2335 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 2336 |  | 
|  | 2337 | SCOPED_TRACE("Remove the window to trigger focus loss"); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2338 | window->setFocusable(false); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2339 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2340 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); | 
|  | 2341 |  | 
|  | 2342 | SCOPED_TRACE("Disable touch mode"); | 
|  | 2343 | mDispatcher->setInTouchMode(false); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2344 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2345 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2346 | setFocusedWindow(window); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2347 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); | 
|  | 2348 |  | 
|  | 2349 | SCOPED_TRACE("Remove the window to trigger focus loss"); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2350 | window->setFocusable(false); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2351 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2352 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); | 
|  | 2353 |  | 
|  | 2354 | SCOPED_TRACE("Enable touch mode again"); | 
|  | 2355 | mDispatcher->setInTouchMode(true); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2356 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2357 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2358 | setFocusedWindow(window); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2359 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 2360 |  | 
|  | 2361 | window->assertNoEvents(); | 
|  | 2362 | } | 
|  | 2363 |  | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2364 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2365 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2366 | sp<FakeWindowHandle> window = | 
|  | 2367 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 2368 |  | 
|  | 2369 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2370 | window->setFocusable(true); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2371 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2372 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2373 | setFocusedWindow(window); | 
|  | 2374 |  | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2375 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 2376 |  | 
|  | 2377 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); | 
|  | 2378 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2379 |  | 
|  | 2380 | InputEvent* event = window->consume(); | 
|  | 2381 | ASSERT_NE(event, nullptr); | 
|  | 2382 |  | 
|  | 2383 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); | 
|  | 2384 | ASSERT_NE(verified, nullptr); | 
|  | 2385 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); | 
|  | 2386 |  | 
|  | 2387 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); | 
|  | 2388 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); | 
|  | 2389 | ASSERT_EQ(keyArgs.source, verified->source); | 
|  | 2390 | ASSERT_EQ(keyArgs.displayId, verified->displayId); | 
|  | 2391 |  | 
|  | 2392 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); | 
|  | 2393 |  | 
|  | 2394 | ASSERT_EQ(keyArgs.action, verifiedKey.action); | 
|  | 2395 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2396 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); | 
|  | 2397 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); | 
|  | 2398 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); | 
|  | 2399 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); | 
|  | 2400 | ASSERT_EQ(0, verifiedKey.repeatCount); | 
|  | 2401 | } | 
|  | 2402 |  | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2403 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2404 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2405 | sp<FakeWindowHandle> window = | 
|  | 2406 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 2407 |  | 
|  | 2408 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2409 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2410 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2411 |  | 
|  | 2412 | NotifyMotionArgs motionArgs = | 
|  | 2413 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2414 | ADISPLAY_ID_DEFAULT); | 
|  | 2415 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2416 |  | 
|  | 2417 | InputEvent* event = window->consume(); | 
|  | 2418 | ASSERT_NE(event, nullptr); | 
|  | 2419 |  | 
|  | 2420 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); | 
|  | 2421 | ASSERT_NE(verified, nullptr); | 
|  | 2422 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); | 
|  | 2423 |  | 
|  | 2424 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); | 
|  | 2425 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); | 
|  | 2426 | EXPECT_EQ(motionArgs.source, verified->source); | 
|  | 2427 | EXPECT_EQ(motionArgs.displayId, verified->displayId); | 
|  | 2428 |  | 
|  | 2429 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); | 
|  | 2430 |  | 
|  | 2431 | EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX); | 
|  | 2432 | EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY); | 
|  | 2433 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); | 
|  | 2434 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); | 
|  | 2435 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); | 
|  | 2436 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); | 
|  | 2437 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); | 
|  | 2438 | } | 
|  | 2439 |  | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2440 | TEST_F(InputDispatcherTest, NonPointerMotionEvent_NotTransformed) { | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2441 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2442 | sp<FakeWindowHandle> window = | 
|  | 2443 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 2444 | const std::string name = window->getName(); | 
|  | 2445 |  | 
|  | 2446 | // Window gets transformed by offset values. | 
|  | 2447 | window->setWindowOffset(500.0f, 500.0f); | 
|  | 2448 |  | 
|  | 2449 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2450 | window->setFocusable(true); | 
|  | 2451 |  | 
|  | 2452 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2453 |  | 
|  | 2454 | // First, we set focused window so that focusedWindowHandle is not null. | 
|  | 2455 | setFocusedWindow(window); | 
|  | 2456 |  | 
|  | 2457 | // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked. | 
|  | 2458 | window->consumeFocusEvent(true); | 
|  | 2459 |  | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2460 | constexpr const std::array nonPointerSources = {AINPUT_SOURCE_TRACKBALL, | 
|  | 2461 | AINPUT_SOURCE_MOUSE_RELATIVE, | 
|  | 2462 | AINPUT_SOURCE_JOYSTICK}; | 
|  | 2463 | for (const int source : nonPointerSources) { | 
|  | 2464 | // Notify motion with a non-pointer source. | 
|  | 2465 | NotifyMotionArgs motionArgs = | 
|  | 2466 | generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, source, ADISPLAY_ID_DEFAULT); | 
|  | 2467 | mDispatcher->notifyMotion(&motionArgs); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2468 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 2469 | MotionEvent* event = window->consumeMotion(); | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2470 | ASSERT_NE(event, nullptr); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2471 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 2472 | const MotionEvent& motionEvent = *event; | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2473 | EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, motionEvent.getAction()); | 
|  | 2474 | EXPECT_EQ(motionArgs.pointerCount, motionEvent.getPointerCount()); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2475 |  | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2476 | float expectedX = motionArgs.pointerCoords[0].getX(); | 
|  | 2477 | float expectedY = motionArgs.pointerCoords[0].getY(); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2478 |  | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2479 | // Ensure the axis values from the final motion event are not transformed. | 
|  | 2480 | EXPECT_EQ(expectedX, motionEvent.getX(0)) | 
|  | 2481 | << "expected " << expectedX << " for x coord of " << name.c_str() << ", got " | 
|  | 2482 | << motionEvent.getX(0); | 
|  | 2483 | EXPECT_EQ(expectedY, motionEvent.getY(0)) | 
|  | 2484 | << "expected " << expectedY << " for y coord of " << name.c_str() << ", got " | 
|  | 2485 | << motionEvent.getY(0); | 
|  | 2486 | // Ensure the raw and transformed axis values for the motion event are the same. | 
|  | 2487 | EXPECT_EQ(motionEvent.getRawX(0), motionEvent.getX(0)) | 
|  | 2488 | << "expected raw and transformed X-axis values to be equal"; | 
|  | 2489 | EXPECT_EQ(motionEvent.getRawY(0), motionEvent.getY(0)) | 
|  | 2490 | << "expected raw and transformed Y-axis values to be equal"; | 
|  | 2491 | } | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2492 | } | 
|  | 2493 |  | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2494 | /** | 
|  | 2495 | * Ensure that separate calls to sign the same data are generating the same key. | 
|  | 2496 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance | 
|  | 2497 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky | 
|  | 2498 | * tests. | 
|  | 2499 | */ | 
|  | 2500 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { | 
|  | 2501 | KeyEvent event = getTestKeyEvent(); | 
|  | 2502 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); | 
|  | 2503 |  | 
|  | 2504 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); | 
|  | 2505 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); | 
|  | 2506 | ASSERT_EQ(hmac1, hmac2); | 
|  | 2507 | } | 
|  | 2508 |  | 
|  | 2509 | /** | 
|  | 2510 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. | 
|  | 2511 | */ | 
|  | 2512 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { | 
|  | 2513 | KeyEvent event = getTestKeyEvent(); | 
|  | 2514 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); | 
|  | 2515 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); | 
|  | 2516 |  | 
|  | 2517 | verifiedEvent.deviceId += 1; | 
|  | 2518 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2519 |  | 
|  | 2520 | verifiedEvent.source += 1; | 
|  | 2521 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2522 |  | 
|  | 2523 | verifiedEvent.eventTimeNanos += 1; | 
|  | 2524 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2525 |  | 
|  | 2526 | verifiedEvent.displayId += 1; | 
|  | 2527 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2528 |  | 
|  | 2529 | verifiedEvent.action += 1; | 
|  | 2530 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2531 |  | 
|  | 2532 | verifiedEvent.downTimeNanos += 1; | 
|  | 2533 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2534 |  | 
|  | 2535 | verifiedEvent.flags += 1; | 
|  | 2536 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2537 |  | 
|  | 2538 | verifiedEvent.keyCode += 1; | 
|  | 2539 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2540 |  | 
|  | 2541 | verifiedEvent.scanCode += 1; | 
|  | 2542 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2543 |  | 
|  | 2544 | verifiedEvent.metaState += 1; | 
|  | 2545 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2546 |  | 
|  | 2547 | verifiedEvent.repeatCount += 1; | 
|  | 2548 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2549 | } | 
|  | 2550 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2551 | TEST_F(InputDispatcherTest, SetFocusedWindow) { | 
|  | 2552 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2553 | sp<FakeWindowHandle> windowTop = | 
|  | 2554 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 2555 | sp<FakeWindowHandle> windowSecond = | 
|  | 2556 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 2557 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2558 |  | 
|  | 2559 | // Top window is also focusable but is not granted focus. | 
|  | 2560 | windowTop->setFocusable(true); | 
|  | 2561 | windowSecond->setFocusable(true); | 
|  | 2562 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
|  | 2563 | setFocusedWindow(windowSecond); | 
|  | 2564 |  | 
|  | 2565 | windowSecond->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2566 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 2567 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2568 |  | 
|  | 2569 | // Focused window should receive event. | 
|  | 2570 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 2571 | windowTop->assertNoEvents(); | 
|  | 2572 | } | 
|  | 2573 |  | 
|  | 2574 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { | 
|  | 2575 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2576 | sp<FakeWindowHandle> window = | 
|  | 2577 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 2578 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2579 |  | 
|  | 2580 | window->setFocusable(true); | 
|  | 2581 | // Release channel for window is no longer valid. | 
|  | 2582 | window->releaseChannel(); | 
|  | 2583 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2584 | setFocusedWindow(window); | 
|  | 2585 |  | 
|  | 2586 | // Test inject a key down, should timeout. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2587 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 2588 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2589 |  | 
|  | 2590 | // window channel is invalid, so it should not receive any input event. | 
|  | 2591 | window->assertNoEvents(); | 
|  | 2592 | } | 
|  | 2593 |  | 
|  | 2594 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { | 
|  | 2595 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2596 | sp<FakeWindowHandle> window = | 
|  | 2597 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 2598 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2599 |  | 
|  | 2600 | // Window is not focusable. | 
|  | 2601 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2602 | setFocusedWindow(window); | 
|  | 2603 |  | 
|  | 2604 | // Test inject a key down, should timeout. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2605 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 2606 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2607 |  | 
|  | 2608 | // window is invalid, so it should not receive any input event. | 
|  | 2609 | window->assertNoEvents(); | 
|  | 2610 | } | 
|  | 2611 |  | 
|  | 2612 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { | 
|  | 2613 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2614 | sp<FakeWindowHandle> windowTop = | 
|  | 2615 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 2616 | sp<FakeWindowHandle> windowSecond = | 
|  | 2617 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 2618 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2619 |  | 
|  | 2620 | windowTop->setFocusable(true); | 
|  | 2621 | windowSecond->setFocusable(true); | 
|  | 2622 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
|  | 2623 | setFocusedWindow(windowTop); | 
|  | 2624 | windowTop->consumeFocusEvent(true); | 
|  | 2625 |  | 
|  | 2626 | setFocusedWindow(windowSecond, windowTop); | 
|  | 2627 | windowSecond->consumeFocusEvent(true); | 
|  | 2628 | windowTop->consumeFocusEvent(false); | 
|  | 2629 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2630 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 2631 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2632 |  | 
|  | 2633 | // Focused window should receive event. | 
|  | 2634 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 2635 | } | 
|  | 2636 |  | 
|  | 2637 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { | 
|  | 2638 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2639 | sp<FakeWindowHandle> windowTop = | 
|  | 2640 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 2641 | sp<FakeWindowHandle> windowSecond = | 
|  | 2642 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 2643 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2644 |  | 
|  | 2645 | windowTop->setFocusable(true); | 
|  | 2646 | windowSecond->setFocusable(true); | 
|  | 2647 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
|  | 2648 | setFocusedWindow(windowSecond, windowTop); | 
|  | 2649 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2650 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 2651 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2652 |  | 
|  | 2653 | // Event should be dropped. | 
|  | 2654 | windowTop->assertNoEvents(); | 
|  | 2655 | windowSecond->assertNoEvents(); | 
|  | 2656 | } | 
|  | 2657 |  | 
|  | 2658 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { | 
|  | 2659 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2660 | sp<FakeWindowHandle> window = | 
|  | 2661 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 2662 | sp<FakeWindowHandle> previousFocusedWindow = | 
|  | 2663 | new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow", | 
|  | 2664 | ADISPLAY_ID_DEFAULT); | 
|  | 2665 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2666 |  | 
|  | 2667 | window->setFocusable(true); | 
|  | 2668 | previousFocusedWindow->setFocusable(true); | 
|  | 2669 | window->setVisible(false); | 
|  | 2670 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); | 
|  | 2671 | setFocusedWindow(previousFocusedWindow); | 
|  | 2672 | previousFocusedWindow->consumeFocusEvent(true); | 
|  | 2673 |  | 
|  | 2674 | // Requesting focus on invisible window takes focus from currently focused window. | 
|  | 2675 | setFocusedWindow(window); | 
|  | 2676 | previousFocusedWindow->consumeFocusEvent(false); | 
|  | 2677 |  | 
|  | 2678 | // Injected key goes to pending queue. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2679 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2680 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2681 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2682 |  | 
|  | 2683 | // Window does not get focus event or key down. | 
|  | 2684 | window->assertNoEvents(); | 
|  | 2685 |  | 
|  | 2686 | // Window becomes visible. | 
|  | 2687 | window->setVisible(true); | 
|  | 2688 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2689 |  | 
|  | 2690 | // Window receives focus event. | 
|  | 2691 | window->consumeFocusEvent(true); | 
|  | 2692 | // Focused window receives key down. | 
|  | 2693 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 2694 | } | 
|  | 2695 |  | 
| Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 2696 | TEST_F(InputDispatcherTest, DisplayRemoved) { | 
|  | 2697 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2698 | sp<FakeWindowHandle> window = | 
|  | 2699 | new FakeWindowHandle(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); | 
|  | 2700 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2701 |  | 
|  | 2702 | // window is granted focus. | 
|  | 2703 | window->setFocusable(true); | 
|  | 2704 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2705 | setFocusedWindow(window); | 
|  | 2706 | window->consumeFocusEvent(true); | 
|  | 2707 |  | 
|  | 2708 | // When a display is removed window loses focus. | 
|  | 2709 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); | 
|  | 2710 | window->consumeFocusEvent(false); | 
|  | 2711 | } | 
|  | 2712 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2713 | /** | 
|  | 2714 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, | 
|  | 2715 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top | 
|  | 2716 | * of the 'slipperyEnterWindow'. | 
|  | 2717 | * | 
|  | 2718 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such | 
|  | 2719 | * a way so that the touched location is no longer covered by the top window. | 
|  | 2720 | * | 
|  | 2721 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now | 
|  | 2722 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had | 
|  | 2723 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive | 
|  | 2724 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting | 
|  | 2725 | * with ACTION_DOWN). | 
|  | 2726 | * Thus, the touch has been transferred from the top window into the bottom window, because the top | 
|  | 2727 | * window moved itself away from the touched location and had Flag::SLIPPERY. | 
|  | 2728 | * | 
|  | 2729 | * Even though the top window moved away from the touched location, it is still obscuring the bottom | 
|  | 2730 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ | 
|  | 2731 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. | 
|  | 2732 | * | 
|  | 2733 | * In this test, we ensure that the event received by the bottom window has | 
|  | 2734 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. | 
|  | 2735 | */ | 
|  | 2736 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { | 
|  | 2737 | constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1; | 
|  | 2738 | constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1; | 
|  | 2739 |  | 
|  | 2740 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2741 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2742 |  | 
|  | 2743 | sp<FakeWindowHandle> slipperyExitWindow = | 
|  | 2744 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 2745 | slipperyExitWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 2746 | InputWindowInfo::Flag::SLIPPERY); | 
|  | 2747 | // Make sure this one overlaps the bottom window | 
|  | 2748 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); | 
|  | 2749 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom | 
|  | 2750 | // one. Windows with the same owner are not considered to be occluding each other. | 
|  | 2751 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); | 
|  | 2752 |  | 
|  | 2753 | sp<FakeWindowHandle> slipperyEnterWindow = | 
|  | 2754 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 2755 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 2756 |  | 
|  | 2757 | mDispatcher->setInputWindows( | 
|  | 2758 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); | 
|  | 2759 |  | 
|  | 2760 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions | 
|  | 2761 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2762 | ADISPLAY_ID_DEFAULT, {{50, 50}}); | 
|  | 2763 | mDispatcher->notifyMotion(&args); | 
|  | 2764 | slipperyExitWindow->consumeMotionDown(); | 
|  | 2765 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); | 
|  | 2766 | mDispatcher->setInputWindows( | 
|  | 2767 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); | 
|  | 2768 |  | 
|  | 2769 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2770 | ADISPLAY_ID_DEFAULT, {{51, 51}}); | 
|  | 2771 | mDispatcher->notifyMotion(&args); | 
|  | 2772 |  | 
|  | 2773 | slipperyExitWindow->consumeMotionCancel(); | 
|  | 2774 |  | 
|  | 2775 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, | 
|  | 2776 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); | 
|  | 2777 | } | 
|  | 2778 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2779 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { | 
|  | 2780 | protected: | 
|  | 2781 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms | 
|  | 2782 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000;   // 40 ms | 
|  | 2783 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2784 | std::shared_ptr<FakeApplicationHandle> mApp; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2785 | sp<FakeWindowHandle> mWindow; | 
|  | 2786 |  | 
|  | 2787 | virtual void SetUp() override { | 
|  | 2788 | mFakePolicy = new FakeInputDispatcherPolicy(); | 
|  | 2789 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); | 
|  | 2790 | mDispatcher = new InputDispatcher(mFakePolicy); | 
|  | 2791 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); | 
|  | 2792 | ASSERT_EQ(OK, mDispatcher->start()); | 
|  | 2793 |  | 
|  | 2794 | setUpWindow(); | 
|  | 2795 | } | 
|  | 2796 |  | 
|  | 2797 | void setUpWindow() { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2798 | mApp = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2799 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2800 |  | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2801 | mWindow->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2802 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2803 | setFocusedWindow(mWindow); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2804 | mWindow->consumeFocusEvent(true); | 
|  | 2805 | } | 
|  | 2806 |  | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2807 | void sendAndConsumeKeyDown(int32_t deviceId) { | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2808 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2809 | keyArgs.deviceId = deviceId; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2810 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event | 
|  | 2811 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2812 |  | 
|  | 2813 | // Window should receive key down event. | 
|  | 2814 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 2815 | } | 
|  | 2816 |  | 
|  | 2817 | void expectKeyRepeatOnce(int32_t repeatCount) { | 
|  | 2818 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); | 
|  | 2819 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 2820 | ASSERT_NE(nullptr, repeatEvent); | 
|  | 2821 |  | 
|  | 2822 | uint32_t eventType = repeatEvent->getType(); | 
|  | 2823 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); | 
|  | 2824 |  | 
|  | 2825 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); | 
|  | 2826 | uint32_t eventAction = repeatKeyEvent->getAction(); | 
|  | 2827 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); | 
|  | 2828 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); | 
|  | 2829 | } | 
|  | 2830 |  | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2831 | void sendAndConsumeKeyUp(int32_t deviceId) { | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2832 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2833 | keyArgs.deviceId = deviceId; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2834 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event | 
|  | 2835 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2836 |  | 
|  | 2837 | // Window should receive key down event. | 
|  | 2838 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, | 
|  | 2839 | 0 /*expectedFlags*/); | 
|  | 2840 | } | 
|  | 2841 | }; | 
|  | 2842 |  | 
|  | 2843 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2844 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 2845 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 2846 | expectKeyRepeatOnce(repeatCount); | 
|  | 2847 | } | 
|  | 2848 | } | 
|  | 2849 |  | 
|  | 2850 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { | 
|  | 2851 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 2852 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 2853 | expectKeyRepeatOnce(repeatCount); | 
|  | 2854 | } | 
|  | 2855 | sendAndConsumeKeyDown(2 /* deviceId */); | 
|  | 2856 | /* repeatCount will start from 1 for deviceId 2 */ | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2857 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 2858 | expectKeyRepeatOnce(repeatCount); | 
|  | 2859 | } | 
|  | 2860 | } | 
|  | 2861 |  | 
|  | 2862 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2863 | sendAndConsumeKeyDown(1 /* deviceId */); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2864 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2865 | sendAndConsumeKeyUp(1 /* deviceId */); | 
|  | 2866 | mWindow->assertNoEvents(); | 
|  | 2867 | } | 
|  | 2868 |  | 
|  | 2869 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { | 
|  | 2870 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 2871 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 2872 | sendAndConsumeKeyDown(2 /* deviceId */); | 
|  | 2873 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 2874 | // Stale key up from device 1. | 
|  | 2875 | sendAndConsumeKeyUp(1 /* deviceId */); | 
|  | 2876 | // Device 2 is still down, keep repeating | 
|  | 2877 | expectKeyRepeatOnce(2 /*repeatCount*/); | 
|  | 2878 | expectKeyRepeatOnce(3 /*repeatCount*/); | 
|  | 2879 | // Device 2 key up | 
|  | 2880 | sendAndConsumeKeyUp(2 /* deviceId */); | 
|  | 2881 | mWindow->assertNoEvents(); | 
|  | 2882 | } | 
|  | 2883 |  | 
|  | 2884 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { | 
|  | 2885 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 2886 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 2887 | sendAndConsumeKeyDown(2 /* deviceId */); | 
|  | 2888 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 2889 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. | 
|  | 2890 | sendAndConsumeKeyUp(2 /* deviceId */); | 
|  | 2891 | // Device 1 still holds key down, but the repeating was already stopped | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2892 | mWindow->assertNoEvents(); | 
|  | 2893 | } | 
|  | 2894 |  | 
| liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 2895 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { | 
|  | 2896 | sendAndConsumeKeyDown(DEVICE_ID); | 
|  | 2897 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 2898 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); | 
|  | 2899 | mDispatcher->notifyDeviceReset(&args); | 
|  | 2900 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, | 
|  | 2901 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); | 
|  | 2902 | mWindow->assertNoEvents(); | 
|  | 2903 | } | 
|  | 2904 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2905 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2906 | sendAndConsumeKeyDown(1 /* deviceId */); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2907 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 2908 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 2909 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; | 
|  | 2910 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, | 
|  | 2911 | IdGenerator::getSource(repeatEvent->getId())); | 
|  | 2912 | } | 
|  | 2913 | } | 
|  | 2914 |  | 
|  | 2915 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2916 | sendAndConsumeKeyDown(1 /* deviceId */); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2917 |  | 
|  | 2918 | std::unordered_set<int32_t> idSet; | 
|  | 2919 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 2920 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 2921 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; | 
|  | 2922 | int32_t id = repeatEvent->getId(); | 
|  | 2923 | EXPECT_EQ(idSet.end(), idSet.find(id)); | 
|  | 2924 | idSet.insert(id); | 
|  | 2925 | } | 
|  | 2926 | } | 
|  | 2927 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2928 | /* Test InputDispatcher for MultiDisplay */ | 
|  | 2929 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { | 
|  | 2930 | public: | 
|  | 2931 | static constexpr int32_t SECOND_DISPLAY_ID = 1; | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2932 | virtual void SetUp() override { | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2933 | InputDispatcherTest::SetUp(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2934 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2935 | application1 = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2936 | windowInPrimary = | 
|  | 2937 | new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2938 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2939 | // Set focus window for primary display, but focused display would be second one. | 
|  | 2940 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2941 | windowInPrimary->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2942 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2943 | setFocusedWindow(windowInPrimary); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2944 | windowInPrimary->consumeFocusEvent(true); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2945 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2946 | application2 = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2947 | windowInSecondary = | 
|  | 2948 | new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2949 | // Set focus to second display window. | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2950 | // Set focus display to second one. | 
|  | 2951 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); | 
|  | 2952 | // Set focus window for second display. | 
|  | 2953 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2954 | windowInSecondary->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2955 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2956 | setFocusedWindow(windowInSecondary); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2957 | windowInSecondary->consumeFocusEvent(true); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2958 | } | 
|  | 2959 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2960 | virtual void TearDown() override { | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2961 | InputDispatcherTest::TearDown(); | 
|  | 2962 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2963 | application1.reset(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2964 | windowInPrimary.clear(); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2965 | application2.reset(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2966 | windowInSecondary.clear(); | 
|  | 2967 | } | 
|  | 2968 |  | 
|  | 2969 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2970 | std::shared_ptr<FakeApplicationHandle> application1; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2971 | sp<FakeWindowHandle> windowInPrimary; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2972 | std::shared_ptr<FakeApplicationHandle> application2; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2973 | sp<FakeWindowHandle> windowInSecondary; | 
|  | 2974 | }; | 
|  | 2975 |  | 
|  | 2976 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { | 
|  | 2977 | // Test touch down on primary display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2978 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2979 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 2980 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2981 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2982 | windowInSecondary->assertNoEvents(); | 
|  | 2983 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2984 | // Test touch down on second display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2985 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2986 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) | 
|  | 2987 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2988 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2989 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2990 | } | 
|  | 2991 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2992 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2993 | // Test inject a key down with display id specified. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 2994 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2995 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2996 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2997 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2998 | windowInSecondary->assertNoEvents(); | 
|  | 2999 |  | 
|  | 3000 | // Test inject a key down without display id specified. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3001 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3002 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3003 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3004 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3005 |  | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3006 | // Remove all windows in secondary display. | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3007 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3008 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3009 | // Old focus should receive a cancel event. | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3010 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, | 
|  | 3011 | AKEY_EVENT_FLAG_CANCELED); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3012 |  | 
|  | 3013 | // Test inject a key down, should timeout because of no target window. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3014 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3015 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3016 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3017 | windowInSecondary->consumeFocusEvent(false); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3018 | windowInSecondary->assertNoEvents(); | 
|  | 3019 | } | 
|  | 3020 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3021 | // Test per-display input monitors for motion event. | 
|  | 3022 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3023 | FakeMonitorReceiver monitorInPrimary = | 
|  | 3024 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
|  | 3025 | FakeMonitorReceiver monitorInSecondary = | 
|  | 3026 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3027 |  | 
|  | 3028 | // Test touch down on primary display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3029 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3030 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 3031 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3032 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3033 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3034 | windowInSecondary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3035 | monitorInSecondary.assertNoEvents(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3036 |  | 
|  | 3037 | // Test touch down on second display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3038 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3039 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) | 
|  | 3040 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3041 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3042 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3043 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3044 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3045 |  | 
|  | 3046 | // Test inject a non-pointer motion event. | 
|  | 3047 | // If specific a display, it will dispatch to the focused window of particular display, | 
|  | 3048 | // or it will dispatch to the focused window of focused display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3049 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3050 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) | 
|  | 3051 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3052 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3053 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3054 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3055 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3056 | } | 
|  | 3057 |  | 
|  | 3058 | // Test per-display input monitors for key event. | 
|  | 3059 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3060 | // Input monitor per display. | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3061 | FakeMonitorReceiver monitorInPrimary = | 
|  | 3062 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
|  | 3063 | FakeMonitorReceiver monitorInSecondary = | 
|  | 3064 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3065 |  | 
|  | 3066 | // Test inject a key down. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3067 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 3068 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3069 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3070 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3071 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3072 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3073 | } | 
|  | 3074 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3075 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { | 
|  | 3076 | sp<FakeWindowHandle> secondWindowInPrimary = | 
|  | 3077 | new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); | 
|  | 3078 | secondWindowInPrimary->setFocusable(true); | 
|  | 3079 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); | 
|  | 3080 | setFocusedWindow(secondWindowInPrimary); | 
|  | 3081 | windowInPrimary->consumeFocusEvent(false); | 
|  | 3082 | secondWindowInPrimary->consumeFocusEvent(true); | 
|  | 3083 |  | 
|  | 3084 | // Test inject a key down. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3085 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
|  | 3086 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3087 | windowInPrimary->assertNoEvents(); | 
|  | 3088 | windowInSecondary->assertNoEvents(); | 
|  | 3089 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 3090 | } | 
|  | 3091 |  | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3092 | class InputFilterTest : public InputDispatcherTest { | 
|  | 3093 | protected: | 
|  | 3094 | static constexpr int32_t SECOND_DISPLAY_ID = 1; | 
|  | 3095 |  | 
|  | 3096 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) { | 
|  | 3097 | NotifyMotionArgs motionArgs; | 
|  | 3098 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3099 | motionArgs = | 
|  | 3100 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3101 | mDispatcher->notifyMotion(&motionArgs); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3102 | motionArgs = | 
|  | 3103 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3104 | mDispatcher->notifyMotion(&motionArgs); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3105 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3106 | if (expectToBeFiltered) { | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 3107 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3108 | } else { | 
|  | 3109 | mFakePolicy->assertFilterInputEventWasNotCalled(); | 
|  | 3110 | } | 
|  | 3111 | } | 
|  | 3112 |  | 
|  | 3113 | void testNotifyKey(bool expectToBeFiltered) { | 
|  | 3114 | NotifyKeyArgs keyArgs; | 
|  | 3115 |  | 
|  | 3116 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); | 
|  | 3117 | mDispatcher->notifyKey(&keyArgs); | 
|  | 3118 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); | 
|  | 3119 | mDispatcher->notifyKey(&keyArgs); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3120 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3121 |  | 
|  | 3122 | if (expectToBeFiltered) { | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 3123 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3124 | } else { | 
|  | 3125 | mFakePolicy->assertFilterInputEventWasNotCalled(); | 
|  | 3126 | } | 
|  | 3127 | } | 
|  | 3128 | }; | 
|  | 3129 |  | 
|  | 3130 | // Test InputFilter for MotionEvent | 
|  | 3131 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { | 
|  | 3132 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. | 
|  | 3133 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); | 
|  | 3134 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); | 
|  | 3135 |  | 
|  | 3136 | // Enable InputFilter | 
|  | 3137 | mDispatcher->setInputFilterEnabled(true); | 
|  | 3138 | // Test touch on both primary and second display, and check if both events are filtered. | 
|  | 3139 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); | 
|  | 3140 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); | 
|  | 3141 |  | 
|  | 3142 | // Disable InputFilter | 
|  | 3143 | mDispatcher->setInputFilterEnabled(false); | 
|  | 3144 | // Test touch on both primary and second display, and check if both events aren't filtered. | 
|  | 3145 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); | 
|  | 3146 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); | 
|  | 3147 | } | 
|  | 3148 |  | 
|  | 3149 | // Test InputFilter for KeyEvent | 
|  | 3150 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { | 
|  | 3151 | // Since the InputFilter is disabled by default, check if key event aren't filtered. | 
|  | 3152 | testNotifyKey(/*expectToBeFiltered*/ false); | 
|  | 3153 |  | 
|  | 3154 | // Enable InputFilter | 
|  | 3155 | mDispatcher->setInputFilterEnabled(true); | 
|  | 3156 | // Send a key event, and check if it is filtered. | 
|  | 3157 | testNotifyKey(/*expectToBeFiltered*/ true); | 
|  | 3158 |  | 
|  | 3159 | // Disable InputFilter | 
|  | 3160 | mDispatcher->setInputFilterEnabled(false); | 
|  | 3161 | // Send a key event, and check if it isn't filtered. | 
|  | 3162 | testNotifyKey(/*expectToBeFiltered*/ false); | 
|  | 3163 | } | 
|  | 3164 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3165 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { | 
|  | 3166 | protected: | 
|  | 3167 | virtual void SetUp() override { | 
|  | 3168 | InputDispatcherTest::SetUp(); | 
|  | 3169 |  | 
|  | 3170 | /** | 
|  | 3171 | * We don't need to enable input filter to test the injected event policy, but we enabled it | 
|  | 3172 | * here to make the tests more realistic, since this policy only matters when inputfilter is | 
|  | 3173 | * on. | 
|  | 3174 | */ | 
|  | 3175 | mDispatcher->setInputFilterEnabled(true); | 
|  | 3176 |  | 
|  | 3177 | std::shared_ptr<InputApplicationHandle> application = | 
|  | 3178 | std::make_shared<FakeApplicationHandle>(); | 
|  | 3179 | mWindow = | 
|  | 3180 | new FakeWindowHandle(application, mDispatcher, "Test Window", ADISPLAY_ID_DEFAULT); | 
|  | 3181 |  | 
|  | 3182 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3183 | mWindow->setFocusable(true); | 
|  | 3184 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3185 | setFocusedWindow(mWindow); | 
|  | 3186 | mWindow->consumeFocusEvent(true); | 
|  | 3187 | } | 
|  | 3188 |  | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3189 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, | 
|  | 3190 | int32_t flags) { | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3191 | KeyEvent event; | 
|  | 3192 |  | 
|  | 3193 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 3194 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, | 
|  | 3195 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, | 
|  | 3196 | KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime); | 
|  | 3197 | const int32_t additionalPolicyFlags = | 
|  | 3198 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; | 
|  | 3199 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3200 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
|  | 3201 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, | 
|  | 3202 | policyFlags | additionalPolicyFlags)); | 
|  | 3203 |  | 
|  | 3204 | InputEvent* received = mWindow->consume(); | 
|  | 3205 | ASSERT_NE(nullptr, received); | 
|  | 3206 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3207 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY); | 
|  | 3208 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); | 
|  | 3209 | ASSERT_EQ(flags, keyEvent.getFlags()); | 
|  | 3210 | } | 
|  | 3211 |  | 
|  | 3212 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, | 
|  | 3213 | int32_t flags) { | 
|  | 3214 | MotionEvent event; | 
|  | 3215 | PointerProperties pointerProperties[1]; | 
|  | 3216 | PointerCoords pointerCoords[1]; | 
|  | 3217 | pointerProperties[0].clear(); | 
|  | 3218 | pointerProperties[0].id = 0; | 
|  | 3219 | pointerCoords[0].clear(); | 
|  | 3220 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); | 
|  | 3221 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); | 
|  | 3222 |  | 
|  | 3223 | ui::Transform identityTransform; | 
|  | 3224 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 3225 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3226 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, | 
|  | 3227 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, | 
|  | 3228 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 3229 | AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 3230 | 0 /*AMOTION_EVENT_INVALID_DISPLAY_SIZE*/, | 
|  | 3231 | 0 /*AMOTION_EVENT_INVALID_DISPLAY_SIZE*/, eventTime, eventTime, | 
|  | 3232 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
|  | 3233 |  | 
|  | 3234 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; | 
|  | 3235 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3236 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
|  | 3237 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, | 
|  | 3238 | policyFlags | additionalPolicyFlags)); | 
|  | 3239 |  | 
|  | 3240 | InputEvent* received = mWindow->consume(); | 
|  | 3241 | ASSERT_NE(nullptr, received); | 
|  | 3242 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); | 
|  | 3243 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION); | 
|  | 3244 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); | 
|  | 3245 | ASSERT_EQ(flags, motionEvent.getFlags()); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3246 | } | 
|  | 3247 |  | 
|  | 3248 | private: | 
|  | 3249 | sp<FakeWindowHandle> mWindow; | 
|  | 3250 | }; | 
|  | 3251 |  | 
|  | 3252 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3253 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input | 
|  | 3254 | // filter. Without it, the event will no different from a regularly injected event, and the | 
|  | 3255 | // injected device id will be overwritten. | 
|  | 3256 | testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, | 
|  | 3257 | 0 /*flags*/); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3258 | } | 
|  | 3259 |  | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3260 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3261 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3262 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, | 
|  | 3263 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); | 
|  | 3264 | } | 
|  | 3265 |  | 
|  | 3266 | TEST_F(InputFilterInjectionPolicyTest, | 
|  | 3267 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { | 
|  | 3268 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, | 
|  | 3269 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, | 
|  | 3270 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3271 | } | 
|  | 3272 |  | 
|  | 3273 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { | 
|  | 3274 | testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3275 | VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3276 | } | 
|  | 3277 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3278 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3279 | virtual void SetUp() override { | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3280 | InputDispatcherTest::SetUp(); | 
|  | 3281 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3282 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 3283 | std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3284 | mUnfocusedWindow = | 
|  | 3285 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3286 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); | 
|  | 3287 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this | 
|  | 3288 | // window. | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3289 | mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3290 |  | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3291 | mFocusedWindow = | 
|  | 3292 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 3293 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3294 | mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3295 |  | 
|  | 3296 | // Set focused application. | 
|  | 3297 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3298 | mFocusedWindow->setFocusable(true); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3299 |  | 
|  | 3300 | // Expect one focus window exist in display. | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3301 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3302 | setFocusedWindow(mFocusedWindow); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3303 | mFocusedWindow->consumeFocusEvent(true); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3304 | } | 
|  | 3305 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3306 | virtual void TearDown() override { | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3307 | InputDispatcherTest::TearDown(); | 
|  | 3308 |  | 
|  | 3309 | mUnfocusedWindow.clear(); | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3310 | mFocusedWindow.clear(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3311 | } | 
|  | 3312 |  | 
|  | 3313 | protected: | 
|  | 3314 | sp<FakeWindowHandle> mUnfocusedWindow; | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3315 | sp<FakeWindowHandle> mFocusedWindow; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3316 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3317 | }; | 
|  | 3318 |  | 
|  | 3319 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action | 
|  | 3320 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received | 
|  | 3321 | // the onPointerDownOutsideFocus callback. | 
|  | 3322 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3323 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3324 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3325 | {20, 20})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3326 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3327 | mUnfocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3328 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3329 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3330 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); | 
|  | 3331 | } | 
|  | 3332 |  | 
|  | 3333 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action | 
|  | 3334 | // DOWN on the window that doesn't have focus. Ensure no window received the | 
|  | 3335 | // onPointerDownOutsideFocus callback. | 
|  | 3336 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3337 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3338 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3339 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3340 | mFocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3341 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3342 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3343 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3344 | } | 
|  | 3345 |  | 
|  | 3346 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't | 
|  | 3347 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. | 
|  | 3348 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3349 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3350 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3351 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3352 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3353 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3354 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3355 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3356 | } | 
|  | 3357 |  | 
|  | 3358 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action | 
|  | 3359 | // DOWN on the window that already has focus. Ensure no window received the | 
|  | 3360 | // onPointerDownOutsideFocus callback. | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3361 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3362 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3363 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3364 | FOCUSED_WINDOW_TOUCH_POINT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3365 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3366 | mFocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3367 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3368 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3369 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3370 | } | 
|  | 3371 |  | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3372 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag | 
|  | 3373 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. | 
|  | 3374 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { | 
|  | 3375 | const MotionEvent event = | 
|  | 3376 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 3377 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 3378 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20)) | 
|  | 3379 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) | 
|  | 3380 | .build(); | 
|  | 3381 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) | 
|  | 3382 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 3383 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); | 
|  | 3384 |  | 
|  | 3385 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3386 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
|  | 3387 | // Ensure that the unfocused window did not receive any FOCUS events. | 
|  | 3388 | mUnfocusedWindow->assertNoEvents(); | 
|  | 3389 | } | 
|  | 3390 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3391 | // These tests ensures we can send touch events to a single client when there are multiple input | 
|  | 3392 | // windows that point to the same client token. | 
|  | 3393 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { | 
|  | 3394 | virtual void SetUp() override { | 
|  | 3395 | InputDispatcherTest::SetUp(); | 
|  | 3396 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3397 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 3398 | std::make_shared<FakeApplicationHandle>(); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3399 | mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1", | 
|  | 3400 | ADISPLAY_ID_DEFAULT); | 
|  | 3401 | // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window. | 
|  | 3402 | // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows. | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3403 | mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 3404 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3405 | mWindow1->setFrame(Rect(0, 0, 100, 100)); | 
|  | 3406 |  | 
|  | 3407 | mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2", | 
|  | 3408 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3409 | mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 3410 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3411 | mWindow2->setFrame(Rect(100, 100, 200, 200)); | 
|  | 3412 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3413 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3414 | } | 
|  | 3415 |  | 
|  | 3416 | protected: | 
|  | 3417 | sp<FakeWindowHandle> mWindow1; | 
|  | 3418 | sp<FakeWindowHandle> mWindow2; | 
|  | 3419 |  | 
|  | 3420 | // Helper function to convert the point from screen coordinates into the window's space | 
|  | 3421 | static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) { | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3422 | vec2 vals = windowInfo->transform.transform(point.x, point.y); | 
|  | 3423 | return {vals.x, vals.y}; | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3424 | } | 
|  | 3425 |  | 
|  | 3426 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, | 
|  | 3427 | const std::vector<PointF>& points) { | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3428 | const std::string name = window->getName(); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3429 | InputEvent* event = window->consume(); | 
|  | 3430 |  | 
|  | 3431 | ASSERT_NE(nullptr, event) << name.c_str() | 
|  | 3432 | << ": consumer should have returned non-NULL event."; | 
|  | 3433 |  | 
|  | 3434 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) | 
|  | 3435 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) | 
|  | 3436 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; | 
|  | 3437 |  | 
|  | 3438 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); | 
|  | 3439 | EXPECT_EQ(expectedAction, motionEvent.getAction()); | 
|  | 3440 |  | 
|  | 3441 | for (size_t i = 0; i < points.size(); i++) { | 
|  | 3442 | float expectedX = points[i].x; | 
|  | 3443 | float expectedY = points[i].y; | 
|  | 3444 |  | 
|  | 3445 | EXPECT_EQ(expectedX, motionEvent.getX(i)) | 
|  | 3446 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() | 
|  | 3447 | << ", got " << motionEvent.getX(i); | 
|  | 3448 | EXPECT_EQ(expectedY, motionEvent.getY(i)) | 
|  | 3449 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() | 
|  | 3450 | << ", got " << motionEvent.getY(i); | 
|  | 3451 | } | 
|  | 3452 | } | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3453 |  | 
|  | 3454 | void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints, | 
|  | 3455 | std::vector<PointF> expectedPoints) { | 
|  | 3456 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3457 | ADISPLAY_ID_DEFAULT, touchedPoints); | 
|  | 3458 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 3459 |  | 
|  | 3460 | // Always consume from window1 since it's the window that has the InputReceiver | 
|  | 3461 | consumeMotionEvent(mWindow1, action, expectedPoints); | 
|  | 3462 | } | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3463 | }; | 
|  | 3464 |  | 
|  | 3465 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { | 
|  | 3466 | // Touch Window 1 | 
|  | 3467 | PointF touchedPoint = {10, 10}; | 
|  | 3468 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3469 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3470 |  | 
|  | 3471 | // Release touch on Window 1 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3472 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3473 |  | 
|  | 3474 | // Touch Window 2 | 
|  | 3475 | touchedPoint = {150, 150}; | 
|  | 3476 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3477 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3478 | } | 
|  | 3479 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3480 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { | 
|  | 3481 | // Set scale value for window2 | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3482 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 3483 |  | 
|  | 3484 | // Touch Window 1 | 
|  | 3485 | PointF touchedPoint = {10, 10}; | 
|  | 3486 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3487 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3488 | // Release touch on Window 1 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3489 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3490 |  | 
|  | 3491 | // Touch Window 2 | 
|  | 3492 | touchedPoint = {150, 150}; | 
|  | 3493 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3494 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
|  | 3495 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3496 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3497 | // Update the transform so rotation is set | 
|  | 3498 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 3499 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
|  | 3500 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3501 | } | 
|  | 3502 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3503 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3504 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 3505 |  | 
|  | 3506 | // Touch Window 1 | 
|  | 3507 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 3508 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3509 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3510 |  | 
|  | 3511 | // Touch Window 2 | 
|  | 3512 | int32_t actionPointerDown = | 
|  | 3513 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3514 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 3515 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
|  | 3516 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3517 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3518 | // Release Window 2 | 
|  | 3519 | int32_t actionPointerUp = | 
|  | 3520 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
|  | 3521 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); | 
|  | 3522 | expectedPoints.pop_back(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3523 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3524 | // Update the transform so rotation is set for Window 2 | 
|  | 3525 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 3526 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
|  | 3527 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3528 | } | 
|  | 3529 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3530 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3531 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 3532 |  | 
|  | 3533 | // Touch Window 1 | 
|  | 3534 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 3535 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3536 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3537 |  | 
|  | 3538 | // Touch Window 2 | 
|  | 3539 | int32_t actionPointerDown = | 
|  | 3540 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3541 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 3542 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3543 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3544 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3545 |  | 
|  | 3546 | // Move both windows | 
|  | 3547 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 3548 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 3549 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 3550 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3551 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3552 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3553 | // Release Window 2 | 
|  | 3554 | int32_t actionPointerUp = | 
|  | 3555 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
|  | 3556 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); | 
|  | 3557 | expectedPoints.pop_back(); | 
|  | 3558 |  | 
|  | 3559 | // Touch Window 2 | 
|  | 3560 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 3561 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
|  | 3562 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
|  | 3563 |  | 
|  | 3564 | // Move both windows | 
|  | 3565 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 3566 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 3567 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 3568 |  | 
|  | 3569 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3570 | } | 
|  | 3571 |  | 
|  | 3572 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { | 
|  | 3573 | mWindow1->setWindowScale(0.5f, 0.5f); | 
|  | 3574 |  | 
|  | 3575 | // Touch Window 1 | 
|  | 3576 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 3577 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3578 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3579 |  | 
|  | 3580 | // Touch Window 2 | 
|  | 3581 | int32_t actionPointerDown = | 
|  | 3582 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3583 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 3584 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3585 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3586 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3587 |  | 
|  | 3588 | // Move both windows | 
|  | 3589 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 3590 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 3591 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 3592 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3593 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3594 | } | 
|  | 3595 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3596 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { | 
|  | 3597 | virtual void SetUp() override { | 
|  | 3598 | InputDispatcherTest::SetUp(); | 
|  | 3599 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3600 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3601 | mApplication->setDispatchingTimeout(20ms); | 
|  | 3602 | mWindow = | 
|  | 3603 | new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 3604 | mWindow->setFrame(Rect(0, 0, 30, 30)); | 
| Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 3605 | mWindow->setDispatchingTimeout(30ms); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3606 | mWindow->setFocusable(true); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3607 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this | 
|  | 3608 | // window. | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3609 | mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3610 |  | 
|  | 3611 | // Set focused application. | 
|  | 3612 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); | 
|  | 3613 |  | 
|  | 3614 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3615 | setFocusedWindow(mWindow); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3616 | mWindow->consumeFocusEvent(true); | 
|  | 3617 | } | 
|  | 3618 |  | 
|  | 3619 | virtual void TearDown() override { | 
|  | 3620 | InputDispatcherTest::TearDown(); | 
|  | 3621 | mWindow.clear(); | 
|  | 3622 | } | 
|  | 3623 |  | 
|  | 3624 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3625 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3626 | sp<FakeWindowHandle> mWindow; | 
|  | 3627 | static constexpr PointF WINDOW_LOCATION = {20, 20}; | 
|  | 3628 |  | 
|  | 3629 | void tapOnWindow() { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3630 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3631 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3632 | WINDOW_LOCATION)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3633 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3634 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3635 | WINDOW_LOCATION)); | 
|  | 3636 | } | 
|  | 3637 | }; | 
|  | 3638 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3639 | // Send a tap and respond, which should not cause an ANR. | 
|  | 3640 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { | 
|  | 3641 | tapOnWindow(); | 
|  | 3642 | mWindow->consumeMotionDown(); | 
|  | 3643 | mWindow->consumeMotionUp(); | 
|  | 3644 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3645 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3646 | } | 
|  | 3647 |  | 
|  | 3648 | // Send a regular key and respond, which should not cause an ANR. | 
|  | 3649 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3650 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3651 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 3652 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3653 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3654 | } | 
|  | 3655 |  | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3656 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { | 
|  | 3657 | mWindow->setFocusable(false); | 
|  | 3658 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3659 | mWindow->consumeFocusEvent(false); | 
|  | 3660 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3661 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3662 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3663 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, | 
|  | 3664 | false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3665 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3666 | // Key will not go to window because we have no focused window. | 
|  | 3667 | // The 'no focused window' ANR timer should start instead. | 
|  | 3668 |  | 
|  | 3669 | // Now, the focused application goes away. | 
|  | 3670 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); | 
|  | 3671 | // The key should get dropped and there should be no ANR. | 
|  | 3672 |  | 
|  | 3673 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3674 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3675 | } | 
|  | 3676 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3677 | // 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] | 3678 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. | 
|  | 3679 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3680 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3681 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3682 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3683 | WINDOW_LOCATION)); | 
|  | 3684 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3685 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN | 
|  | 3686 | ASSERT_TRUE(sequenceNum); | 
|  | 3687 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3688 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3689 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3690 | mWindow->finishEvent(*sequenceNum); | 
|  | 3691 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, | 
|  | 3692 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3693 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3694 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3695 | } | 
|  | 3696 |  | 
|  | 3697 | // Send a key to the app and have the app not respond right away. | 
|  | 3698 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { | 
|  | 3699 | // Inject a key, and don't respond - expect that ANR is called. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3700 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3701 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); | 
|  | 3702 | ASSERT_TRUE(sequenceNum); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3703 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3704 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3705 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3706 | } | 
|  | 3707 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3708 | // We have a focused application, but no focused window | 
|  | 3709 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3710 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3711 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3712 | mWindow->consumeFocusEvent(false); | 
|  | 3713 |  | 
|  | 3714 | // taps on the window work as normal | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3715 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3716 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3717 | WINDOW_LOCATION)); | 
|  | 3718 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); | 
|  | 3719 | mDispatcher->waitForIdle(); | 
|  | 3720 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3721 |  | 
|  | 3722 | // Once a focused event arrives, we get an ANR for this application | 
|  | 3723 | // We specify the injection timeout to be smaller than the application timeout, to ensure that | 
|  | 3724 | // injection times out (instead of failing). | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3725 | const InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3726 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3727 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3728 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3729 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3730 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3731 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3732 | } | 
|  | 3733 |  | 
|  | 3734 | // We have a focused application, but no focused window | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3735 | // Make sure that we don't notify policy twice about the same ANR. | 
|  | 3736 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3737 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3738 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3739 | mWindow->consumeFocusEvent(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3740 |  | 
|  | 3741 | // Once a focused event arrives, we get an ANR for this application | 
|  | 3742 | // We specify the injection timeout to be smaller than the application timeout, to ensure that | 
|  | 3743 | // injection times out (instead of failing). | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3744 | const InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3745 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3746 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3747 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3748 | const std::chrono::duration appTimeout = | 
|  | 3749 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3750 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3751 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3752 | std::this_thread::sleep_for(appTimeout); | 
|  | 3753 | // ANR should not be raised again. It is up to policy to do that if it desires. | 
|  | 3754 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3755 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3756 | // If we now get a focused window, the ANR should stop, but the policy handles that via | 
|  | 3757 | // '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] | 3758 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3759 | } | 
|  | 3760 |  | 
|  | 3761 | // We have a focused application, but no focused window | 
|  | 3762 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3763 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3764 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3765 | mWindow->consumeFocusEvent(false); | 
|  | 3766 |  | 
|  | 3767 | // Once a focused event arrives, we get an ANR for this application | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3768 | const InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3769 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3770 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); | 
|  | 3771 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3772 |  | 
|  | 3773 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3774 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3775 |  | 
|  | 3776 | // Future focused events get dropped right away | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3777 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3778 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3779 | mWindow->assertNoEvents(); | 
|  | 3780 | } | 
|  | 3781 |  | 
|  | 3782 | /** | 
|  | 3783 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the | 
|  | 3784 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. | 
|  | 3785 | * If we process 1 of the events, but ANR on the second event with the same timestamp, | 
|  | 3786 | * the ANR mechanism should still work. | 
|  | 3787 | * | 
|  | 3788 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the | 
|  | 3789 | * DOWN event, while not responding on the second one. | 
|  | 3790 | */ | 
|  | 3791 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { | 
|  | 3792 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 3793 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3794 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, | 
|  | 3795 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 3796 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3797 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3798 |  | 
|  | 3799 | // Now send ACTION_UP, with identical timestamp | 
|  | 3800 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3801 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, | 
|  | 3802 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 3803 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3804 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3805 |  | 
|  | 3806 | // We have now sent down and up. Let's consume first event and then ANR on the second. | 
|  | 3807 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3808 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3809 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3810 | } | 
|  | 3811 |  | 
|  | 3812 | // If an app is not responding to a key event, gesture monitors should continue to receive | 
|  | 3813 | // new motion events | 
|  | 3814 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) { | 
|  | 3815 | FakeMonitorReceiver monitor = | 
|  | 3816 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, | 
|  | 3817 | true /*isGestureMonitor*/); | 
|  | 3818 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3819 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3820 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3821 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3822 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3823 |  | 
|  | 3824 | // Stuck on the ACTION_UP | 
|  | 3825 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3826 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3827 |  | 
|  | 3828 | // New tap will go to the gesture monitor, but not to the window | 
|  | 3829 | tapOnWindow(); | 
|  | 3830 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3831 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 3832 |  | 
|  | 3833 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion | 
|  | 3834 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3835 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3836 | mWindow->assertNoEvents(); | 
|  | 3837 | monitor.assertNoEvents(); | 
|  | 3838 | } | 
|  | 3839 |  | 
|  | 3840 | // If an app is not responding to a motion event, gesture monitors should continue to receive | 
|  | 3841 | // new motion events | 
|  | 3842 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) { | 
|  | 3843 | FakeMonitorReceiver monitor = | 
|  | 3844 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, | 
|  | 3845 | true /*isGestureMonitor*/); | 
|  | 3846 |  | 
|  | 3847 | tapOnWindow(); | 
|  | 3848 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3849 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 3850 |  | 
|  | 3851 | mWindow->consumeMotionDown(); | 
|  | 3852 | // Stuck on the ACTION_UP | 
|  | 3853 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3854 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3855 |  | 
|  | 3856 | // New tap will go to the gesture monitor, but not to the window | 
|  | 3857 | tapOnWindow(); | 
|  | 3858 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3859 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 3860 |  | 
|  | 3861 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion | 
|  | 3862 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3863 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3864 | mWindow->assertNoEvents(); | 
|  | 3865 | monitor.assertNoEvents(); | 
|  | 3866 | } | 
|  | 3867 |  | 
|  | 3868 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to | 
|  | 3869 | // process events, you don't get an anr. When the window later becomes unresponsive again, you | 
|  | 3870 | // get an ANR again. | 
|  | 3871 | // 1. tap -> block on ACTION_UP -> receive ANR | 
|  | 3872 | // 2. consume all pending events (= queue becomes healthy again) | 
|  | 3873 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time | 
|  | 3874 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { | 
|  | 3875 | tapOnWindow(); | 
|  | 3876 |  | 
|  | 3877 | mWindow->consumeMotionDown(); | 
|  | 3878 | // Block on ACTION_UP | 
|  | 3879 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3880 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3881 | mWindow->consumeMotionUp(); // Now the connection should be healthy again | 
|  | 3882 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3883 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3884 | mWindow->assertNoEvents(); | 
|  | 3885 |  | 
|  | 3886 | tapOnWindow(); | 
|  | 3887 | mWindow->consumeMotionDown(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3888 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3889 | mWindow->consumeMotionUp(); | 
|  | 3890 |  | 
|  | 3891 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3892 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3893 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3894 | mWindow->assertNoEvents(); | 
|  | 3895 | } | 
|  | 3896 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3897 | // If a connection remains unresponsive for a while, make sure policy is only notified once about | 
|  | 3898 | // it. | 
|  | 3899 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3900 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3901 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3902 | WINDOW_LOCATION)); | 
|  | 3903 |  | 
|  | 3904 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3905 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3906 | std::this_thread::sleep_for(windowTimeout); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3907 | // 'notifyConnectionUnresponsive' should only be called once per connection | 
|  | 3908 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3909 | // 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] | 3910 | mWindow->consumeMotionDown(); | 
|  | 3911 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, | 
|  | 3912 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 3913 | mWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3914 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3915 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3916 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3917 | } | 
|  | 3918 |  | 
|  | 3919 | /** | 
|  | 3920 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 3921 | * not to to the focused window until the motion is processed. | 
|  | 3922 | * | 
|  | 3923 | * Warning!!! | 
|  | 3924 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT | 
|  | 3925 | * and the injection timeout that we specify when injecting the key. | 
|  | 3926 | * We must have the injection timeout (10ms) be smaller than | 
|  | 3927 | *  KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). | 
|  | 3928 | * | 
|  | 3929 | * If that value changes, this test should also change. | 
|  | 3930 | */ | 
|  | 3931 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { | 
|  | 3932 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering | 
|  | 3933 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3934 |  | 
|  | 3935 | tapOnWindow(); | 
|  | 3936 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); | 
|  | 3937 | ASSERT_TRUE(downSequenceNum); | 
|  | 3938 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); | 
|  | 3939 | ASSERT_TRUE(upSequenceNum); | 
|  | 3940 | // Don't finish the events yet, and send a key | 
|  | 3941 | // Injection will "succeed" because we will eventually give up and send the key to the focused | 
|  | 3942 | // window even if motions are still being processed. But because the injection timeout is short, | 
|  | 3943 | // we will receive INJECTION_TIMED_OUT as the result. | 
|  | 3944 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3945 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3946 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3947 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); | 
|  | 3948 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3949 | // Key will not be sent to the window, yet, because the window is still processing events | 
|  | 3950 | // and the key remains pending, waiting for the touch events to be processed | 
|  | 3951 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); | 
|  | 3952 | ASSERT_FALSE(keySequenceNum); | 
|  | 3953 |  | 
|  | 3954 | std::this_thread::sleep_for(500ms); | 
|  | 3955 | // if we wait long enough though, dispatcher will give up, and still send the key | 
|  | 3956 | // to the focused window, even though we have not yet finished the motion event | 
|  | 3957 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 3958 | mWindow->finishEvent(*downSequenceNum); | 
|  | 3959 | mWindow->finishEvent(*upSequenceNum); | 
|  | 3960 | } | 
|  | 3961 |  | 
|  | 3962 | /** | 
|  | 3963 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 3964 | * not go to the focused window until the motion is processed. | 
|  | 3965 | * If then a new motion comes in, then the pending key event should be going to the currently | 
|  | 3966 | * focused window right away. | 
|  | 3967 | */ | 
|  | 3968 | TEST_F(InputDispatcherSingleWindowAnr, | 
|  | 3969 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { | 
|  | 3970 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering | 
|  | 3971 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3972 |  | 
|  | 3973 | tapOnWindow(); | 
|  | 3974 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); | 
|  | 3975 | ASSERT_TRUE(downSequenceNum); | 
|  | 3976 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); | 
|  | 3977 | ASSERT_TRUE(upSequenceNum); | 
|  | 3978 | // Don't finish the events yet, and send a key | 
|  | 3979 | // Injection is async, so it will succeed | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3980 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3981 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3982 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3983 | // At this point, key is still pending, and should not be sent to the application yet. | 
|  | 3984 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); | 
|  | 3985 | ASSERT_FALSE(keySequenceNum); | 
|  | 3986 |  | 
|  | 3987 | // Now tap down again. It should cause the pending key to go to the focused window right away. | 
|  | 3988 | tapOnWindow(); | 
|  | 3989 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd | 
|  | 3990 | // the other events yet. We can finish events in any order. | 
|  | 3991 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN | 
|  | 3992 | mWindow->finishEvent(*upSequenceNum);   // first tap's ACTION_UP | 
|  | 3993 | mWindow->consumeMotionDown(); | 
|  | 3994 | mWindow->consumeMotionUp(); | 
|  | 3995 | mWindow->assertNoEvents(); | 
|  | 3996 | } | 
|  | 3997 |  | 
|  | 3998 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { | 
|  | 3999 | virtual void SetUp() override { | 
|  | 4000 | InputDispatcherTest::SetUp(); | 
|  | 4001 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4002 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4003 | mApplication->setDispatchingTimeout(10ms); | 
|  | 4004 | mUnfocusedWindow = | 
|  | 4005 | new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT); | 
|  | 4006 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); | 
|  | 4007 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this | 
|  | 4008 | // window. | 
|  | 4009 | // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4010 | mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 4011 | InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH | | 
|  | 4012 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4013 |  | 
|  | 4014 | mFocusedWindow = | 
|  | 4015 | new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 4016 | mFocusedWindow->setDispatchingTimeout(30ms); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4017 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4018 | mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 4019 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4020 |  | 
|  | 4021 | // Set focused application. | 
|  | 4022 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4023 | mFocusedWindow->setFocusable(true); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4024 |  | 
|  | 4025 | // Expect one focus window exist in display. | 
|  | 4026 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4027 | setFocusedWindow(mFocusedWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4028 | mFocusedWindow->consumeFocusEvent(true); | 
|  | 4029 | } | 
|  | 4030 |  | 
|  | 4031 | virtual void TearDown() override { | 
|  | 4032 | InputDispatcherTest::TearDown(); | 
|  | 4033 |  | 
|  | 4034 | mUnfocusedWindow.clear(); | 
|  | 4035 | mFocusedWindow.clear(); | 
|  | 4036 | } | 
|  | 4037 |  | 
|  | 4038 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4039 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4040 | sp<FakeWindowHandle> mUnfocusedWindow; | 
|  | 4041 | sp<FakeWindowHandle> mFocusedWindow; | 
|  | 4042 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; | 
|  | 4043 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; | 
|  | 4044 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; | 
|  | 4045 |  | 
|  | 4046 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } | 
|  | 4047 |  | 
|  | 4048 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } | 
|  | 4049 |  | 
|  | 4050 | private: | 
|  | 4051 | void tap(const PointF& location) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4052 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4053 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4054 | location)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4055 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4056 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4057 | location)); | 
|  | 4058 | } | 
|  | 4059 | }; | 
|  | 4060 |  | 
|  | 4061 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout | 
|  | 4062 | // should be ANR'd first. | 
|  | 4063 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4064 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4065 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4066 | FOCUSED_WINDOW_LOCATION)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4067 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4068 | mFocusedWindow->consumeMotionDown(); | 
|  | 4069 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
|  | 4070 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 4071 | // We consumed all events, so no ANR | 
|  | 4072 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4073 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4074 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4075 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4076 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4077 | FOCUSED_WINDOW_LOCATION)); | 
|  | 4078 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 4079 | ASSERT_TRUE(unfocusedSequenceNum); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4080 |  | 
|  | 4081 | const std::chrono::duration timeout = | 
|  | 4082 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4083 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4084 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event | 
|  | 4085 | // sequence to make it consistent | 
|  | 4086 | mFocusedWindow->consumeMotionCancel(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4087 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4088 | mFocusedWindow->consumeMotionDown(); | 
|  | 4089 | // This cancel is generated because the connection was unresponsive | 
|  | 4090 | mFocusedWindow->consumeMotionCancel(); | 
|  | 4091 | mFocusedWindow->assertNoEvents(); | 
|  | 4092 | mUnfocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4093 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4094 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4095 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4096 | } | 
|  | 4097 |  | 
|  | 4098 | // If we have 2 windows with identical timeouts that are both unresponsive, | 
|  | 4099 | // it doesn't matter which order they should have ANR. | 
|  | 4100 | // But we should receive ANR for both. | 
|  | 4101 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { | 
|  | 4102 | // Set the timeout for unfocused window to match the focused window | 
|  | 4103 | mUnfocusedWindow->setDispatchingTimeout(10ms); | 
|  | 4104 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
|  | 4105 |  | 
|  | 4106 | tapOnFocusedWindow(); | 
|  | 4107 | // 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] | 4108 | sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms); | 
|  | 4109 | sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4110 |  | 
|  | 4111 | // 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] | 4112 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || | 
|  | 4113 | mFocusedWindow->getToken() == anrConnectionToken2); | 
|  | 4114 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || | 
|  | 4115 | mUnfocusedWindow->getToken() == anrConnectionToken2); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4116 |  | 
|  | 4117 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4118 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4119 |  | 
|  | 4120 | mFocusedWindow->consumeMotionDown(); | 
|  | 4121 | mFocusedWindow->consumeMotionUp(); | 
|  | 4122 | mUnfocusedWindow->consumeMotionOutside(); | 
|  | 4123 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4124 | sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken(); | 
|  | 4125 | sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4126 |  | 
|  | 4127 | // Both applications should be marked as responsive, in any order | 
|  | 4128 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || | 
|  | 4129 | mFocusedWindow->getToken() == responsiveToken2); | 
|  | 4130 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || | 
|  | 4131 | mUnfocusedWindow->getToken() == responsiveToken2); | 
|  | 4132 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4133 | } | 
|  | 4134 |  | 
|  | 4135 | // If a window is already not responding, the second tap on the same window should be ignored. | 
|  | 4136 | // We should also log an error to account for the dropped event (not tested here). | 
|  | 4137 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. | 
|  | 4138 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { | 
|  | 4139 | tapOnFocusedWindow(); | 
|  | 4140 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
|  | 4141 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 4142 | // Receive the events, but don't respond | 
|  | 4143 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN | 
|  | 4144 | ASSERT_TRUE(downEventSequenceNum); | 
|  | 4145 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP | 
|  | 4146 | ASSERT_TRUE(upEventSequenceNum); | 
|  | 4147 | const std::chrono::duration timeout = | 
|  | 4148 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4149 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4150 |  | 
|  | 4151 | // Tap once again | 
|  | 4152 | // 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] | 4153 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4154 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4155 | FOCUSED_WINDOW_LOCATION)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4156 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4157 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4158 | FOCUSED_WINDOW_LOCATION)); | 
|  | 4159 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a | 
|  | 4160 | // valid touch target | 
|  | 4161 | mUnfocusedWindow->assertNoEvents(); | 
|  | 4162 |  | 
|  | 4163 | // Consume the first tap | 
|  | 4164 | mFocusedWindow->finishEvent(*downEventSequenceNum); | 
|  | 4165 | mFocusedWindow->finishEvent(*upEventSequenceNum); | 
|  | 4166 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4167 | // The second tap did not go to the focused window | 
|  | 4168 | mFocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4169 | // Since all events are finished, connection should be deemed healthy again | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4170 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4171 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4172 | } | 
|  | 4173 |  | 
|  | 4174 | // If you tap outside of all windows, there will not be ANR | 
|  | 4175 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4176 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4177 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4178 | LOCATION_OUTSIDE_ALL_WINDOWS)); | 
|  | 4179 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4180 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4181 | } | 
|  | 4182 |  | 
|  | 4183 | // Since the focused window is paused, tapping on it should not produce any events | 
|  | 4184 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { | 
|  | 4185 | mFocusedWindow->setPaused(true); | 
|  | 4186 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
|  | 4187 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4188 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4189 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4190 | FOCUSED_WINDOW_LOCATION)); | 
|  | 4191 |  | 
|  | 4192 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); | 
|  | 4193 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4194 | // Should not ANR because the window is paused, and touches shouldn't go to it | 
|  | 4195 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4196 |  | 
|  | 4197 | mFocusedWindow->assertNoEvents(); | 
|  | 4198 | mUnfocusedWindow->assertNoEvents(); | 
|  | 4199 | } | 
|  | 4200 |  | 
|  | 4201 | /** | 
|  | 4202 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 4203 | * not to to the focused window until the motion is processed. | 
|  | 4204 | * If a different window becomes focused at this time, the key should go to that window instead. | 
|  | 4205 | * | 
|  | 4206 | * Warning!!! | 
|  | 4207 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT | 
|  | 4208 | * and the injection timeout that we specify when injecting the key. | 
|  | 4209 | * We must have the injection timeout (10ms) be smaller than | 
|  | 4210 | *  KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). | 
|  | 4211 | * | 
|  | 4212 | * If that value changes, this test should also change. | 
|  | 4213 | */ | 
|  | 4214 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { | 
|  | 4215 | // Set a long ANR timeout to prevent it from triggering | 
|  | 4216 | mFocusedWindow->setDispatchingTimeout(2s); | 
|  | 4217 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 4218 |  | 
|  | 4219 | tapOnUnfocusedWindow(); | 
|  | 4220 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 4221 | ASSERT_TRUE(downSequenceNum); | 
|  | 4222 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 4223 | ASSERT_TRUE(upSequenceNum); | 
|  | 4224 | // Don't finish the events yet, and send a key | 
|  | 4225 | // Injection will succeed because we will eventually give up and send the key to the focused | 
|  | 4226 | // window even if motions are still being processed. | 
|  | 4227 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4228 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4229 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4230 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); | 
|  | 4231 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4232 | // Key will not be sent to the window, yet, because the window is still processing events | 
|  | 4233 | // and the key remains pending, waiting for the touch events to be processed | 
|  | 4234 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); | 
|  | 4235 | ASSERT_FALSE(keySequenceNum); | 
|  | 4236 |  | 
|  | 4237 | // 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] | 4238 | mFocusedWindow->setFocusable(false); | 
|  | 4239 | mUnfocusedWindow->setFocusable(true); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4240 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4241 | setFocusedWindow(mUnfocusedWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4242 |  | 
|  | 4243 | // Focus events should precede the key events | 
|  | 4244 | mUnfocusedWindow->consumeFocusEvent(true); | 
|  | 4245 | mFocusedWindow->consumeFocusEvent(false); | 
|  | 4246 |  | 
|  | 4247 | // Finish the tap events, which should unblock dispatcher | 
|  | 4248 | mUnfocusedWindow->finishEvent(*downSequenceNum); | 
|  | 4249 | mUnfocusedWindow->finishEvent(*upSequenceNum); | 
|  | 4250 |  | 
|  | 4251 | // Now that all queues are cleared and no backlog in the connections, the key event | 
|  | 4252 | // can finally go to the newly focused "mUnfocusedWindow". | 
|  | 4253 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 4254 | mFocusedWindow->assertNoEvents(); | 
|  | 4255 | mUnfocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4256 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4257 | } | 
|  | 4258 |  | 
|  | 4259 | // When the touch stream is split across 2 windows, and one of them does not respond, | 
|  | 4260 | // then ANR should be raised and the touch should be canceled for the unresponsive window. | 
|  | 4261 | // The other window should not be affected by that. | 
|  | 4262 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { | 
|  | 4263 | // Touch Window 1 | 
|  | 4264 | NotifyMotionArgs motionArgs = | 
|  | 4265 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4266 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); | 
|  | 4267 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4268 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
|  | 4269 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 4270 |  | 
|  | 4271 | // Touch Window 2 | 
|  | 4272 | int32_t actionPointerDown = | 
|  | 4273 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
|  | 4274 |  | 
|  | 4275 | motionArgs = | 
|  | 4276 | generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4277 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); | 
|  | 4278 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4279 |  | 
|  | 4280 | const std::chrono::duration timeout = | 
|  | 4281 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4282 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4283 |  | 
|  | 4284 | mUnfocusedWindow->consumeMotionDown(); | 
|  | 4285 | mFocusedWindow->consumeMotionDown(); | 
|  | 4286 | // Focused window may or may not receive ACTION_MOVE | 
|  | 4287 | // But it should definitely receive ACTION_CANCEL due to the ANR | 
|  | 4288 | InputEvent* event; | 
|  | 4289 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); | 
|  | 4290 | ASSERT_TRUE(moveOrCancelSequenceNum); | 
|  | 4291 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); | 
|  | 4292 | ASSERT_NE(nullptr, event); | 
|  | 4293 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); | 
|  | 4294 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 4295 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { | 
|  | 4296 | mFocusedWindow->consumeMotionCancel(); | 
|  | 4297 | } else { | 
|  | 4298 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); | 
|  | 4299 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4300 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4301 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4302 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4303 | mUnfocusedWindow->assertNoEvents(); | 
|  | 4304 | mFocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4305 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4306 | } | 
|  | 4307 |  | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 4308 | /** | 
|  | 4309 | * If we have no focused window, and a key comes in, we start the ANR timer. | 
|  | 4310 | * The focused application should add a focused window before the timer runs out to prevent ANR. | 
|  | 4311 | * | 
|  | 4312 | * If the user touches another application during this time, the key should be dropped. | 
|  | 4313 | * Next, if a new focused window comes in, without toggling the focused application, | 
|  | 4314 | * then no ANR should occur. | 
|  | 4315 | * | 
|  | 4316 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', | 
|  | 4317 | * but in some cases the policy may not update the focused application. | 
|  | 4318 | */ | 
|  | 4319 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { | 
|  | 4320 | std::shared_ptr<FakeApplicationHandle> focusedApplication = | 
|  | 4321 | std::make_shared<FakeApplicationHandle>(); | 
|  | 4322 | focusedApplication->setDispatchingTimeout(60ms); | 
|  | 4323 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); | 
|  | 4324 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. | 
|  | 4325 | mFocusedWindow->setFocusable(false); | 
|  | 4326 |  | 
|  | 4327 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 4328 | mFocusedWindow->consumeFocusEvent(false); | 
|  | 4329 |  | 
|  | 4330 | // Send a key. The ANR timer should start because there is no focused window. | 
|  | 4331 | // 'focusedApplication' will get blamed if this timer completes. | 
|  | 4332 | // 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] | 4333 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 4334 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4335 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, | 
|  | 4336 | false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4337 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 4338 |  | 
|  | 4339 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, | 
|  | 4340 | // then the injected touches won't cause the focused event to get dropped. | 
|  | 4341 | // The dispatcher only checks for whether the queue should be pruned upon queueing. | 
|  | 4342 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would | 
|  | 4343 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. | 
|  | 4344 | // For this test, it means that the key would get delivered to the window once it becomes | 
|  | 4345 | // focused. | 
|  | 4346 | std::this_thread::sleep_for(10ms); | 
|  | 4347 |  | 
|  | 4348 | // Touch unfocused window. This should force the pending key to get dropped. | 
|  | 4349 | NotifyMotionArgs motionArgs = | 
|  | 4350 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4351 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); | 
|  | 4352 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4353 |  | 
|  | 4354 | // We do not consume the motion right away, because that would require dispatcher to first | 
|  | 4355 | // process (== drop) the key event, and by that time, ANR will be raised. | 
|  | 4356 | // Set the focused window first. | 
|  | 4357 | mFocusedWindow->setFocusable(true); | 
|  | 4358 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 4359 | setFocusedWindow(mFocusedWindow); | 
|  | 4360 | mFocusedWindow->consumeFocusEvent(true); | 
|  | 4361 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs | 
|  | 4362 | // to another application. This could be a bug / behaviour in the policy. | 
|  | 4363 |  | 
|  | 4364 | mUnfocusedWindow->consumeMotionDown(); | 
|  | 4365 |  | 
|  | 4366 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4367 | // Should not ANR because we actually have a focused window. It was just added too slowly. | 
|  | 4368 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); | 
|  | 4369 | } | 
|  | 4370 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4371 | // These tests ensure we cannot send touch events to a window that's positioned behind a window | 
|  | 4372 | // that has feature NO_INPUT_CHANNEL. | 
|  | 4373 | // Layout: | 
|  | 4374 | //   Top (closest to user) | 
|  | 4375 | //       mNoInputWindow (above all windows) | 
|  | 4376 | //       mBottomWindow | 
|  | 4377 | //   Bottom (furthest from user) | 
|  | 4378 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { | 
|  | 4379 | virtual void SetUp() override { | 
|  | 4380 | InputDispatcherTest::SetUp(); | 
|  | 4381 |  | 
|  | 4382 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
|  | 4383 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, | 
|  | 4384 | "Window without input channel", ADISPLAY_ID_DEFAULT, | 
|  | 4385 | std::make_optional<sp<IBinder>>(nullptr) /*token*/); | 
|  | 4386 |  | 
|  | 4387 | mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL); | 
|  | 4388 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 4389 | // It's perfectly valid for this window to not have an associated input channel | 
|  | 4390 |  | 
|  | 4391 | mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window", | 
|  | 4392 | ADISPLAY_ID_DEFAULT); | 
|  | 4393 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 4394 |  | 
|  | 4395 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); | 
|  | 4396 | } | 
|  | 4397 |  | 
|  | 4398 | protected: | 
|  | 4399 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
|  | 4400 | sp<FakeWindowHandle> mNoInputWindow; | 
|  | 4401 | sp<FakeWindowHandle> mBottomWindow; | 
|  | 4402 | }; | 
|  | 4403 |  | 
|  | 4404 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { | 
|  | 4405 | PointF touchedPoint = {10, 10}; | 
|  | 4406 |  | 
|  | 4407 | NotifyMotionArgs motionArgs = | 
|  | 4408 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4409 | ADISPLAY_ID_DEFAULT, {touchedPoint}); | 
|  | 4410 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4411 |  | 
|  | 4412 | mNoInputWindow->assertNoEvents(); | 
|  | 4413 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have | 
|  | 4414 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, | 
|  | 4415 | // and therefore should prevent mBottomWindow from receiving touches | 
|  | 4416 | mBottomWindow->assertNoEvents(); | 
|  | 4417 | } | 
|  | 4418 |  | 
|  | 4419 | /** | 
|  | 4420 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, | 
|  | 4421 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. | 
|  | 4422 | */ | 
|  | 4423 | TEST_F(InputDispatcherMultiWindowOcclusionTests, | 
|  | 4424 | NoInputChannelFeature_DropsTouchesWithValidChannel) { | 
|  | 4425 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, | 
|  | 4426 | "Window with input channel and NO_INPUT_CHANNEL", | 
|  | 4427 | ADISPLAY_ID_DEFAULT); | 
|  | 4428 |  | 
|  | 4429 | mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL); | 
|  | 4430 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 4431 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); | 
|  | 4432 |  | 
|  | 4433 | PointF touchedPoint = {10, 10}; | 
|  | 4434 |  | 
|  | 4435 | NotifyMotionArgs motionArgs = | 
|  | 4436 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4437 | ADISPLAY_ID_DEFAULT, {touchedPoint}); | 
|  | 4438 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4439 |  | 
|  | 4440 | mNoInputWindow->assertNoEvents(); | 
|  | 4441 | mBottomWindow->assertNoEvents(); | 
|  | 4442 | } | 
|  | 4443 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4444 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { | 
|  | 4445 | protected: | 
|  | 4446 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 4447 | sp<FakeWindowHandle> mWindow; | 
|  | 4448 | sp<FakeWindowHandle> mMirror; | 
|  | 4449 |  | 
|  | 4450 | virtual void SetUp() override { | 
|  | 4451 | InputDispatcherTest::SetUp(); | 
|  | 4452 | mApp = std::make_shared<FakeApplicationHandle>(); | 
|  | 4453 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 4454 | mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT, | 
|  | 4455 | mWindow->getToken()); | 
|  | 4456 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
|  | 4457 | mWindow->setFocusable(true); | 
|  | 4458 | mMirror->setFocusable(true); | 
|  | 4459 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4460 | } | 
|  | 4461 | }; | 
|  | 4462 |  | 
|  | 4463 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { | 
|  | 4464 | // Request focus on a mirrored window | 
|  | 4465 | setFocusedWindow(mMirror); | 
|  | 4466 |  | 
|  | 4467 | // window gets focused | 
|  | 4468 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4469 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4470 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4471 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 4472 | } | 
|  | 4473 |  | 
|  | 4474 | // A focused & mirrored window remains focused only if the window and its mirror are both | 
|  | 4475 | // focusable. | 
|  | 4476 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { | 
|  | 4477 | setFocusedWindow(mMirror); | 
|  | 4478 |  | 
|  | 4479 | // window gets focused | 
|  | 4480 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4481 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4482 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4483 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4484 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4485 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4486 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4487 |  | 
|  | 4488 | mMirror->setFocusable(false); | 
|  | 4489 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4490 |  | 
|  | 4491 | // window loses focus since one of the windows associated with the token in not focusable | 
|  | 4492 | mWindow->consumeFocusEvent(false); | 
|  | 4493 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4494 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 4495 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4496 | mWindow->assertNoEvents(); | 
|  | 4497 | } | 
|  | 4498 |  | 
|  | 4499 | // A focused & mirrored window remains focused until the window and its mirror both become | 
|  | 4500 | // invisible. | 
|  | 4501 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { | 
|  | 4502 | setFocusedWindow(mMirror); | 
|  | 4503 |  | 
|  | 4504 | // window gets focused | 
|  | 4505 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4506 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4507 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4508 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4509 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4510 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4511 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4512 |  | 
|  | 4513 | mMirror->setVisible(false); | 
|  | 4514 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4515 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4516 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4517 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4518 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4519 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4520 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4521 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4522 |  | 
|  | 4523 | mWindow->setVisible(false); | 
|  | 4524 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4525 |  | 
|  | 4526 | // window loses focus only after all windows associated with the token become invisible. | 
|  | 4527 | mWindow->consumeFocusEvent(false); | 
|  | 4528 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4529 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 4530 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4531 | mWindow->assertNoEvents(); | 
|  | 4532 | } | 
|  | 4533 |  | 
|  | 4534 | // A focused & mirrored window remains focused until both windows are removed. | 
|  | 4535 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { | 
|  | 4536 | setFocusedWindow(mMirror); | 
|  | 4537 |  | 
|  | 4538 | // window gets focused | 
|  | 4539 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4540 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4541 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4542 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4543 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4544 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4545 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4546 |  | 
|  | 4547 | // single window is removed but the window token remains focused | 
|  | 4548 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); | 
|  | 4549 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4550 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4551 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4552 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4553 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4554 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4555 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4556 |  | 
|  | 4557 | // Both windows are removed | 
|  | 4558 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); | 
|  | 4559 | mWindow->consumeFocusEvent(false); | 
|  | 4560 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4561 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 4562 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4563 | mWindow->assertNoEvents(); | 
|  | 4564 | } | 
|  | 4565 |  | 
|  | 4566 | // Focus request can be pending until one window becomes visible. | 
|  | 4567 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { | 
|  | 4568 | // Request focus on an invisible mirror. | 
|  | 4569 | mWindow->setVisible(false); | 
|  | 4570 | mMirror->setVisible(false); | 
|  | 4571 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4572 | setFocusedWindow(mMirror); | 
|  | 4573 |  | 
|  | 4574 | // Injected key goes to pending queue. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4575 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4576 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4577 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4578 |  | 
|  | 4579 | mMirror->setVisible(true); | 
|  | 4580 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4581 |  | 
|  | 4582 | // window gets focused | 
|  | 4583 | mWindow->consumeFocusEvent(true); | 
|  | 4584 | // window gets the pending key event | 
|  | 4585 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 4586 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4587 |  | 
|  | 4588 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { | 
|  | 4589 | protected: | 
|  | 4590 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 4591 | sp<FakeWindowHandle> mWindow; | 
|  | 4592 | sp<FakeWindowHandle> mSecondWindow; | 
|  | 4593 |  | 
|  | 4594 | void SetUp() override { | 
|  | 4595 | InputDispatcherTest::SetUp(); | 
|  | 4596 | mApp = std::make_shared<FakeApplicationHandle>(); | 
|  | 4597 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 4598 | mWindow->setFocusable(true); | 
|  | 4599 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); | 
|  | 4600 | mSecondWindow->setFocusable(true); | 
|  | 4601 |  | 
|  | 4602 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
|  | 4603 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); | 
|  | 4604 |  | 
|  | 4605 | setFocusedWindow(mWindow); | 
|  | 4606 | mWindow->consumeFocusEvent(true); | 
|  | 4607 | } | 
|  | 4608 |  | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4609 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { | 
|  | 4610 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4611 | mDispatcher->notifyPointerCaptureChanged(&args); | 
|  | 4612 | } | 
|  | 4613 |  | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4614 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, | 
|  | 4615 | bool enabled) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4616 | mDispatcher->requestPointerCapture(window->getToken(), enabled); | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4617 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); | 
|  | 4618 | notifyPointerCaptureChanged(request); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4619 | window->consumeCaptureEvent(enabled); | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4620 | return request; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4621 | } | 
|  | 4622 | }; | 
|  | 4623 |  | 
|  | 4624 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { | 
|  | 4625 | // Ensure that capture cannot be obtained for unfocused windows. | 
|  | 4626 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); | 
|  | 4627 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 4628 | mSecondWindow->assertNoEvents(); | 
|  | 4629 |  | 
|  | 4630 | // Ensure that capture can be enabled from the focus window. | 
|  | 4631 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 4632 |  | 
|  | 4633 | // Ensure that capture cannot be disabled from a window that does not have capture. | 
|  | 4634 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); | 
|  | 4635 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 4636 |  | 
|  | 4637 | // Ensure that capture can be disabled from the window with capture. | 
|  | 4638 | requestAndVerifyPointerCapture(mWindow, false); | 
|  | 4639 | } | 
|  | 4640 |  | 
|  | 4641 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4642 | auto request = requestAndVerifyPointerCapture(mWindow, true); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4643 |  | 
|  | 4644 | setFocusedWindow(mSecondWindow); | 
|  | 4645 |  | 
|  | 4646 | // Ensure that the capture disabled event was sent first. | 
|  | 4647 | mWindow->consumeCaptureEvent(false); | 
|  | 4648 | mWindow->consumeFocusEvent(false); | 
|  | 4649 | mSecondWindow->consumeFocusEvent(true); | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4650 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4651 |  | 
|  | 4652 | // Ensure that additional state changes from InputReader are not sent to the window. | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4653 | notifyPointerCaptureChanged({}); | 
|  | 4654 | notifyPointerCaptureChanged(request); | 
|  | 4655 | notifyPointerCaptureChanged({}); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4656 | mWindow->assertNoEvents(); | 
|  | 4657 | mSecondWindow->assertNoEvents(); | 
|  | 4658 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 4659 | } | 
|  | 4660 |  | 
|  | 4661 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4662 | auto request = requestAndVerifyPointerCapture(mWindow, true); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4663 |  | 
|  | 4664 | // InputReader unexpectedly disables and enables pointer capture. | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4665 | notifyPointerCaptureChanged({}); | 
|  | 4666 | notifyPointerCaptureChanged(request); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4667 |  | 
|  | 4668 | // Ensure that Pointer Capture is disabled. | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4669 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4670 | mWindow->consumeCaptureEvent(false); | 
|  | 4671 | mWindow->assertNoEvents(); | 
|  | 4672 | } | 
|  | 4673 |  | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 4674 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { | 
|  | 4675 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 4676 |  | 
|  | 4677 | // The first window loses focus. | 
|  | 4678 | setFocusedWindow(mSecondWindow); | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4679 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 4680 | mWindow->consumeCaptureEvent(false); | 
|  | 4681 |  | 
|  | 4682 | // Request Pointer Capture from the second window before the notification from InputReader | 
|  | 4683 | // arrives. | 
|  | 4684 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4685 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 4686 |  | 
|  | 4687 | // InputReader notifies Pointer Capture was disabled (because of the focus change). | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4688 | notifyPointerCaptureChanged({}); | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 4689 |  | 
|  | 4690 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4691 | notifyPointerCaptureChanged(request); | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 4692 |  | 
|  | 4693 | mSecondWindow->consumeFocusEvent(true); | 
|  | 4694 | mSecondWindow->consumeCaptureEvent(true); | 
|  | 4695 | } | 
|  | 4696 |  | 
| Prabir Pradhan | f192a10 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4697 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { | 
|  | 4698 | // App repeatedly enables and disables capture. | 
|  | 4699 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); | 
|  | 4700 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); | 
|  | 4701 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); | 
|  | 4702 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
|  | 4703 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); | 
|  | 4704 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); | 
|  | 4705 |  | 
|  | 4706 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the | 
|  | 4707 | // first request is now stale, this should do nothing. | 
|  | 4708 | notifyPointerCaptureChanged(firstRequest); | 
|  | 4709 | mWindow->assertNoEvents(); | 
|  | 4710 |  | 
|  | 4711 | // InputReader notifies that the second request was enabled. | 
|  | 4712 | notifyPointerCaptureChanged(secondRequest); | 
|  | 4713 | mWindow->consumeCaptureEvent(true); | 
|  | 4714 | } | 
|  | 4715 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4716 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { | 
|  | 4717 | protected: | 
|  | 4718 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4719 |  | 
|  | 4720 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; | 
|  | 4721 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); | 
|  | 4722 |  | 
|  | 4723 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; | 
|  | 4724 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); | 
|  | 4725 |  | 
|  | 4726 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold | 
|  | 4727 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; | 
|  | 4728 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); | 
|  | 4729 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < | 
|  | 4730 | MAXIMUM_OBSCURING_OPACITY); | 
|  | 4731 |  | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4732 | static const int32_t TOUCHED_APP_UID = 10001; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4733 | static const int32_t APP_B_UID = 10002; | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4734 | static const int32_t APP_C_UID = 10003; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4735 |  | 
|  | 4736 | sp<FakeWindowHandle> mTouchWindow; | 
|  | 4737 |  | 
|  | 4738 | virtual void SetUp() override { | 
|  | 4739 | InputDispatcherTest::SetUp(); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4740 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4741 | mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK); | 
|  | 4742 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); | 
|  | 4743 | } | 
|  | 4744 |  | 
|  | 4745 | virtual void TearDown() override { | 
|  | 4746 | InputDispatcherTest::TearDown(); | 
|  | 4747 | mTouchWindow.clear(); | 
|  | 4748 | } | 
|  | 4749 |  | 
|  | 4750 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4751 | os::TouchOcclusionMode mode, float alpha = 1.0f) { | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4752 | sp<FakeWindowHandle> window = getWindow(uid, name); | 
|  | 4753 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCHABLE); | 
|  | 4754 | window->setTouchOcclusionMode(mode); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4755 | window->setAlpha(alpha); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4756 | return window; | 
|  | 4757 | } | 
|  | 4758 |  | 
|  | 4759 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { | 
|  | 4760 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); | 
|  | 4761 | sp<FakeWindowHandle> window = | 
|  | 4762 | new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); | 
|  | 4763 | // Generate an arbitrary PID based on the UID | 
|  | 4764 | window->setOwnerInfo(1777 + (uid % 10000), uid); | 
|  | 4765 | return window; | 
|  | 4766 | } | 
|  | 4767 |  | 
|  | 4768 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { | 
|  | 4769 | NotifyMotionArgs args = | 
|  | 4770 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4771 | ADISPLAY_ID_DEFAULT, points); | 
|  | 4772 | mDispatcher->notifyMotion(&args); | 
|  | 4773 | } | 
|  | 4774 | }; | 
|  | 4775 |  | 
|  | 4776 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4777 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4778 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4779 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4780 |  | 
|  | 4781 | touch(); | 
|  | 4782 |  | 
|  | 4783 | mTouchWindow->assertNoEvents(); | 
|  | 4784 | } | 
|  | 4785 |  | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 4786 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4787 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { | 
|  | 4788 | const sp<FakeWindowHandle>& w = | 
|  | 4789 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); | 
|  | 4790 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4791 |  | 
|  | 4792 | touch(); | 
|  | 4793 |  | 
|  | 4794 | mTouchWindow->assertNoEvents(); | 
|  | 4795 | } | 
|  | 4796 |  | 
|  | 4797 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 4798 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { | 
|  | 4799 | const sp<FakeWindowHandle>& w = | 
|  | 4800 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 4801 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4802 |  | 
|  | 4803 | touch(); | 
|  | 4804 |  | 
|  | 4805 | w->assertNoEvents(); | 
|  | 4806 | } | 
|  | 4807 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4808 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4809 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); | 
|  | 4810 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4811 |  | 
|  | 4812 | touch(); | 
|  | 4813 |  | 
|  | 4814 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4815 | } | 
|  | 4816 |  | 
|  | 4817 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4818 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4819 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4820 | w->setFrame(Rect(0, 0, 50, 50)); | 
|  | 4821 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4822 |  | 
|  | 4823 | touch({PointF{100, 100}}); | 
|  | 4824 |  | 
|  | 4825 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4826 | } | 
|  | 4827 |  | 
|  | 4828 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4829 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4830 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4831 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4832 |  | 
|  | 4833 | touch(); | 
|  | 4834 |  | 
|  | 4835 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4836 | } | 
|  | 4837 |  | 
|  | 4838 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { | 
|  | 4839 | const sp<FakeWindowHandle>& w = | 
|  | 4840 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
|  | 4841 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4842 |  | 
|  | 4843 | touch(); | 
|  | 4844 |  | 
|  | 4845 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4846 | } | 
|  | 4847 |  | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 4848 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { | 
|  | 4849 | const sp<FakeWindowHandle>& w = | 
|  | 4850 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
|  | 4851 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4852 |  | 
|  | 4853 | touch(); | 
|  | 4854 |  | 
|  | 4855 | w->assertNoEvents(); | 
|  | 4856 | } | 
|  | 4857 |  | 
|  | 4858 | /** | 
|  | 4859 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs | 
|  | 4860 | * inside) while letting them pass-through. Note that even though touch passes through the occluding | 
|  | 4861 | * window, the occluding window will still receive ACTION_OUTSIDE event. | 
|  | 4862 | */ | 
|  | 4863 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 4864 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { | 
|  | 4865 | const sp<FakeWindowHandle>& w = | 
|  | 4866 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
|  | 4867 | w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH); | 
|  | 4868 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4869 |  | 
|  | 4870 | touch(); | 
|  | 4871 |  | 
|  | 4872 | w->consumeMotionOutside(); | 
|  | 4873 | } | 
|  | 4874 |  | 
|  | 4875 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { | 
|  | 4876 | const sp<FakeWindowHandle>& w = | 
|  | 4877 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
|  | 4878 | w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH); | 
|  | 4879 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4880 |  | 
|  | 4881 | touch(); | 
|  | 4882 |  | 
|  | 4883 | InputEvent* event = w->consume(); | 
|  | 4884 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()); | 
|  | 4885 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 4886 | EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX()); | 
|  | 4887 | EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY()); | 
|  | 4888 | } | 
|  | 4889 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4890 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4891 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4892 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4893 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4894 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4895 |  | 
|  | 4896 | touch(); | 
|  | 4897 |  | 
|  | 4898 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4899 | } | 
|  | 4900 |  | 
|  | 4901 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { | 
|  | 4902 | const sp<FakeWindowHandle>& w = | 
|  | 4903 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4904 | MAXIMUM_OBSCURING_OPACITY); | 
|  | 4905 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4906 |  | 
|  | 4907 | touch(); | 
|  | 4908 |  | 
|  | 4909 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4910 | } | 
|  | 4911 |  | 
|  | 4912 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4913 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4914 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4915 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4916 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4917 |  | 
|  | 4918 | touch(); | 
|  | 4919 |  | 
|  | 4920 | mTouchWindow->assertNoEvents(); | 
|  | 4921 | } | 
|  | 4922 |  | 
|  | 4923 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { | 
|  | 4924 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 | 
|  | 4925 | const sp<FakeWindowHandle>& w1 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4926 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, | 
|  | 4927 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4928 | const sp<FakeWindowHandle>& w2 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4929 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, | 
|  | 4930 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4931 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 4932 |  | 
|  | 4933 | touch(); | 
|  | 4934 |  | 
|  | 4935 | mTouchWindow->assertNoEvents(); | 
|  | 4936 | } | 
|  | 4937 |  | 
|  | 4938 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { | 
|  | 4939 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 | 
|  | 4940 | const sp<FakeWindowHandle>& w1 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4941 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, | 
|  | 4942 | OPACITY_FAR_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4943 | const sp<FakeWindowHandle>& w2 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4944 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, | 
|  | 4945 | OPACITY_FAR_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4946 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 4947 |  | 
|  | 4948 | touch(); | 
|  | 4949 |  | 
|  | 4950 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4951 | } | 
|  | 4952 |  | 
|  | 4953 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 4954 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { | 
|  | 4955 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4956 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4957 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4958 | const sp<FakeWindowHandle>& wC = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4959 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 4960 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4961 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
|  | 4962 |  | 
|  | 4963 | touch(); | 
|  | 4964 |  | 
|  | 4965 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4966 | } | 
|  | 4967 |  | 
|  | 4968 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { | 
|  | 4969 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4970 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4971 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4972 | const sp<FakeWindowHandle>& wC = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4973 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 4974 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4975 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4976 |  | 
|  | 4977 | touch(); | 
|  | 4978 |  | 
|  | 4979 | mTouchWindow->assertNoEvents(); | 
|  | 4980 | } | 
|  | 4981 |  | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4982 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 4983 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { | 
|  | 4984 | const sp<FakeWindowHandle>& wA = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4985 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 4986 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4987 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4988 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4989 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4990 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); | 
|  | 4991 |  | 
|  | 4992 | touch(); | 
|  | 4993 |  | 
|  | 4994 | mTouchWindow->assertNoEvents(); | 
|  | 4995 | } | 
|  | 4996 |  | 
|  | 4997 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 4998 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { | 
|  | 4999 | const sp<FakeWindowHandle>& wA = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5000 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 5001 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5002 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5003 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 5004 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5005 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); | 
|  | 5006 |  | 
|  | 5007 | touch(); | 
|  | 5008 |  | 
|  | 5009 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5010 | } | 
|  | 5011 |  | 
|  | 5012 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { | 
|  | 5013 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5014 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 5015 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5016 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5017 |  | 
|  | 5018 | touch(); | 
|  | 5019 |  | 
|  | 5020 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5021 | } | 
|  | 5022 |  | 
|  | 5023 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { | 
|  | 5024 | const sp<FakeWindowHandle>& w = | 
|  | 5025 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 5026 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5027 |  | 
|  | 5028 | touch(); | 
|  | 5029 |  | 
|  | 5030 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5031 | } | 
|  | 5032 |  | 
| Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 5033 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5034 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { | 
|  | 5035 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); | 
|  | 5036 | const sp<FakeWindowHandle>& w = | 
|  | 5037 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); | 
|  | 5038 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5039 |  | 
|  | 5040 | touch(); | 
|  | 5041 |  | 
|  | 5042 | mTouchWindow->assertNoEvents(); | 
|  | 5043 | } | 
|  | 5044 |  | 
|  | 5045 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { | 
|  | 5046 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); | 
|  | 5047 | const sp<FakeWindowHandle>& w = | 
|  | 5048 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); | 
|  | 5049 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5050 |  | 
|  | 5051 | touch(); | 
|  | 5052 |  | 
|  | 5053 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5054 | } | 
|  | 5055 |  | 
|  | 5056 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5057 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { | 
|  | 5058 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); | 
|  | 5059 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5060 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 5061 | OPACITY_ABOVE_THRESHOLD); | 
|  | 5062 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5063 |  | 
|  | 5064 | touch(); | 
|  | 5065 |  | 
|  | 5066 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5067 | } | 
|  | 5068 |  | 
|  | 5069 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5070 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { | 
|  | 5071 | const sp<FakeWindowHandle>& w1 = | 
|  | 5072 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, | 
|  | 5073 | OPACITY_BELOW_THRESHOLD); | 
|  | 5074 | const sp<FakeWindowHandle>& w2 = | 
|  | 5075 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 5076 | OPACITY_BELOW_THRESHOLD); | 
|  | 5077 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 5078 |  | 
|  | 5079 | touch(); | 
|  | 5080 |  | 
|  | 5081 | mTouchWindow->assertNoEvents(); | 
|  | 5082 | } | 
|  | 5083 |  | 
|  | 5084 | /** | 
|  | 5085 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the | 
|  | 5086 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold | 
|  | 5087 | * (which alone would result in allowing touches) does not affect the blocking behavior. | 
|  | 5088 | */ | 
|  | 5089 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5090 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { | 
|  | 5091 | const sp<FakeWindowHandle>& wB = | 
|  | 5092 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, | 
|  | 5093 | OPACITY_BELOW_THRESHOLD); | 
|  | 5094 | const sp<FakeWindowHandle>& wC = | 
|  | 5095 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 5096 | OPACITY_BELOW_THRESHOLD); | 
|  | 5097 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
|  | 5098 |  | 
|  | 5099 | touch(); | 
|  | 5100 |  | 
|  | 5101 | mTouchWindow->assertNoEvents(); | 
|  | 5102 | } | 
|  | 5103 |  | 
|  | 5104 | /** | 
|  | 5105 | * This test is testing that a window from a different UID but with same application token doesn't | 
|  | 5106 | * block the touch. Apps can share the application token for close UI collaboration for example. | 
|  | 5107 | */ | 
|  | 5108 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5109 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { | 
|  | 5110 | const sp<FakeWindowHandle>& w = | 
|  | 5111 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 5112 | w->setApplicationToken(mTouchWindow->getApplicationToken()); | 
| Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 5113 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5114 |  | 
|  | 5115 | touch(); | 
|  | 5116 |  | 
|  | 5117 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5118 | } | 
|  | 5119 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5120 | class InputDispatcherDragTests : public InputDispatcherTest { | 
|  | 5121 | protected: | 
|  | 5122 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 5123 | sp<FakeWindowHandle> mWindow; | 
|  | 5124 | sp<FakeWindowHandle> mSecondWindow; | 
|  | 5125 | sp<FakeWindowHandle> mDragWindow; | 
|  | 5126 |  | 
|  | 5127 | void SetUp() override { | 
|  | 5128 | InputDispatcherTest::SetUp(); | 
|  | 5129 | mApp = std::make_shared<FakeApplicationHandle>(); | 
|  | 5130 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 5131 | mWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 5132 | mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
|  | 5133 |  | 
|  | 5134 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); | 
|  | 5135 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); | 
|  | 5136 | mSecondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
|  | 5137 |  | 
|  | 5138 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
|  | 5139 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); | 
|  | 5140 | } | 
|  | 5141 |  | 
|  | 5142 | // Start performing drag, we will create a drag window and transfer touch to it. | 
|  | 5143 | void performDrag() { | 
|  | 5144 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5145 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5146 | {50, 50})) | 
|  | 5147 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5148 |  | 
|  | 5149 | // Window should receive motion event. | 
|  | 5150 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5151 |  | 
|  | 5152 | // The drag window covers the entire display | 
|  | 5153 | mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); | 
|  | 5154 | mDispatcher->setInputWindows( | 
|  | 5155 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); | 
|  | 5156 |  | 
|  | 5157 | // Transfer touch focus to the drag window | 
|  | 5158 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), | 
|  | 5159 | true /* isDragDrop */); | 
|  | 5160 | mWindow->consumeMotionCancel(); | 
|  | 5161 | mDragWindow->consumeMotionDown(); | 
|  | 5162 | } | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5163 |  | 
|  | 5164 | // Start performing drag, we will create a drag window and transfer touch to it. | 
|  | 5165 | void performStylusDrag() { | 
|  | 5166 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5167 | injectMotionEvent(mDispatcher, | 
|  | 5168 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, | 
|  | 5169 | AINPUT_SOURCE_STYLUS) | 
|  | 5170 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) | 
|  | 5171 | .pointer(PointerBuilder(0, | 
|  | 5172 | AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 5173 | .x(50) | 
|  | 5174 | .y(50)) | 
|  | 5175 | .build())); | 
|  | 5176 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5177 |  | 
|  | 5178 | // The drag window covers the entire display | 
|  | 5179 | mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); | 
|  | 5180 | mDispatcher->setInputWindows( | 
|  | 5181 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); | 
|  | 5182 |  | 
|  | 5183 | // Transfer touch focus to the drag window | 
|  | 5184 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), | 
|  | 5185 | true /* isDragDrop */); | 
|  | 5186 | mWindow->consumeMotionCancel(); | 
|  | 5187 | mDragWindow->consumeMotionDown(); | 
|  | 5188 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5189 | }; | 
|  | 5190 |  | 
|  | 5191 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { | 
|  | 5192 | performDrag(); | 
|  | 5193 |  | 
|  | 5194 | // Move on window. | 
|  | 5195 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5196 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5197 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5198 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5199 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5200 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5201 | mSecondWindow->assertNoEvents(); | 
|  | 5202 |  | 
|  | 5203 | // Move to another window. | 
|  | 5204 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5205 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5206 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 5207 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5208 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5209 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 5210 | mSecondWindow->consumeDragEvent(false, 50, 50); | 
|  | 5211 |  | 
|  | 5212 | // Move back to original window. | 
|  | 5213 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5214 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5215 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5216 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5217 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5218 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5219 | mSecondWindow->consumeDragEvent(true, -50, 50); | 
|  | 5220 |  | 
|  | 5221 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5222 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5223 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5224 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 5225 | mWindow->assertNoEvents(); | 
|  | 5226 | mSecondWindow->assertNoEvents(); | 
|  | 5227 | } | 
|  | 5228 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5229 | TEST_F(InputDispatcherDragTests, DragAndDrop) { | 
|  | 5230 | performDrag(); | 
|  | 5231 |  | 
|  | 5232 | // Move on window. | 
|  | 5233 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5234 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5235 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5236 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5237 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5238 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5239 | mSecondWindow->assertNoEvents(); | 
|  | 5240 |  | 
|  | 5241 | // Move to another window. | 
|  | 5242 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5243 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5244 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 5245 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5246 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5247 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 5248 | mSecondWindow->consumeDragEvent(false, 50, 50); | 
|  | 5249 |  | 
|  | 5250 | // drop to another window. | 
|  | 5251 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5252 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5253 | {150, 50})) | 
|  | 5254 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5255 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 5256 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); | 
|  | 5257 | mWindow->assertNoEvents(); | 
|  | 5258 | mSecondWindow->assertNoEvents(); | 
|  | 5259 | } | 
|  | 5260 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5261 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { | 
|  | 5262 | performStylusDrag(); | 
|  | 5263 |  | 
|  | 5264 | // Move on window and keep button pressed. | 
|  | 5265 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5266 | injectMotionEvent(mDispatcher, | 
|  | 5267 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) | 
|  | 5268 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) | 
|  | 5269 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 5270 | .x(50) | 
|  | 5271 | .y(50)) | 
|  | 5272 | .build())) | 
|  | 5273 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5274 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5275 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5276 | mSecondWindow->assertNoEvents(); | 
|  | 5277 |  | 
|  | 5278 | // Move to another window and release button, expect to drop item. | 
|  | 5279 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5280 | injectMotionEvent(mDispatcher, | 
|  | 5281 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) | 
|  | 5282 | .buttonState(0) | 
|  | 5283 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 5284 | .x(150) | 
|  | 5285 | .y(50)) | 
|  | 5286 | .build())) | 
|  | 5287 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5288 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5289 | mWindow->assertNoEvents(); | 
|  | 5290 | mSecondWindow->assertNoEvents(); | 
|  | 5291 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); | 
|  | 5292 |  | 
|  | 5293 | // nothing to the window. | 
|  | 5294 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5295 | injectMotionEvent(mDispatcher, | 
|  | 5296 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) | 
|  | 5297 | .buttonState(0) | 
|  | 5298 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 5299 | .x(150) | 
|  | 5300 | .y(50)) | 
|  | 5301 | .build())) | 
|  | 5302 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5303 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 5304 | mWindow->assertNoEvents(); | 
|  | 5305 | mSecondWindow->assertNoEvents(); | 
|  | 5306 | } | 
|  | 5307 |  | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 5308 | TEST_F(InputDispatcherDragTests, DragAndDrop_InvalidWindow) { | 
|  | 5309 | performDrag(); | 
|  | 5310 |  | 
|  | 5311 | // Set second window invisible. | 
|  | 5312 | mSecondWindow->setVisible(false); | 
|  | 5313 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); | 
|  | 5314 |  | 
|  | 5315 | // Move on window. | 
|  | 5316 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5317 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5318 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5319 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5320 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5321 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5322 | mSecondWindow->assertNoEvents(); | 
|  | 5323 |  | 
|  | 5324 | // Move to another window. | 
|  | 5325 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5326 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5327 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 5328 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5329 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5330 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 5331 | mSecondWindow->assertNoEvents(); | 
|  | 5332 |  | 
|  | 5333 | // drop to another window. | 
|  | 5334 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5335 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5336 | {150, 50})) | 
|  | 5337 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5338 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 5339 | mFakePolicy->assertDropTargetEquals(nullptr); | 
|  | 5340 | mWindow->assertNoEvents(); | 
|  | 5341 | mSecondWindow->assertNoEvents(); | 
|  | 5342 | } | 
|  | 5343 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5344 | } // namespace android::inputdispatcher |