| 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 | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 242 | void waitForSetPointerCapture(bool enabled) { | 
|  | 243 | std::unique_lock lock(mLock); | 
|  | 244 | base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 245 |  | 
|  | 246 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, | 
|  | 247 | [this, enabled]() REQUIRES(mLock) { | 
|  | 248 | return mPointerCaptureEnabled && | 
|  | 249 | *mPointerCaptureEnabled == | 
|  | 250 | enabled; | 
|  | 251 | })) { | 
|  | 252 | FAIL() << "Timed out waiting for setPointerCapture(" << enabled << ") to be called."; | 
|  | 253 | } | 
|  | 254 | mPointerCaptureEnabled.reset(); | 
|  | 255 | } | 
|  | 256 |  | 
|  | 257 | void assertSetPointerCaptureNotCalled() { | 
|  | 258 | std::unique_lock lock(mLock); | 
|  | 259 | base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 260 |  | 
|  | 261 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { | 
|  | 262 | FAIL() << "Expected setPointerCapture(enabled) to not be called, but was called. " | 
|  | 263 | "enabled = " | 
|  | 264 | << *mPointerCaptureEnabled; | 
|  | 265 | } | 
|  | 266 | mPointerCaptureEnabled.reset(); | 
|  | 267 | } | 
|  | 268 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 269 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { | 
|  | 270 | std::scoped_lock lock(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 271 | ASSERT_TRUE(mNotifyDropWindowWasCalled); | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 272 | ASSERT_EQ(targetToken, mDropTargetWindowToken); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 273 | mNotifyDropWindowWasCalled = false; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 274 | } | 
|  | 275 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 276 | private: | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 277 | std::mutex mLock; | 
|  | 278 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); | 
|  | 279 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); | 
|  | 280 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); | 
|  | 281 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 282 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 283 | std::condition_variable mPointerCaptureChangedCondition; | 
|  | 284 | std::optional<bool> mPointerCaptureEnabled GUARDED_BY(mLock); | 
|  | 285 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 286 | // ANR handling | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 287 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 288 | std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock); | 
|  | 289 | std::queue<sp<IBinder>> mResponsiveWindowTokens GUARDED_BY(mLock); | 
|  | 290 | std::queue<int32_t> mAnrMonitorPids GUARDED_BY(mLock); | 
|  | 291 | std::queue<int32_t> mResponsiveMonitorPids GUARDED_BY(mLock); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 292 | std::condition_variable mNotifyAnr; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 293 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 294 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 295 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 296 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 297 | void notifyConfigurationChanged(nsecs_t when) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 298 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 299 | mConfigurationChangedTime = when; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 300 | } | 
|  | 301 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 302 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, const std::string&) override { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 303 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 304 | mAnrWindowTokens.push(connectionToken); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 305 | mNotifyAnr.notify_all(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 306 | } | 
|  | 307 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 308 | void notifyMonitorUnresponsive(int32_t pid, const std::string&) override { | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 309 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 310 | mAnrMonitorPids.push(pid); | 
|  | 311 | mNotifyAnr.notify_all(); | 
|  | 312 | } | 
|  | 313 |  | 
|  | 314 | void notifyWindowResponsive(const sp<IBinder>& connectionToken) override { | 
|  | 315 | std::scoped_lock lock(mLock); | 
|  | 316 | mResponsiveWindowTokens.push(connectionToken); | 
|  | 317 | mNotifyAnr.notify_all(); | 
|  | 318 | } | 
|  | 319 |  | 
|  | 320 | void notifyMonitorResponsive(int32_t pid) override { | 
|  | 321 | std::scoped_lock lock(mLock); | 
|  | 322 | mResponsiveMonitorPids.push(pid); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 323 | mNotifyAnr.notify_all(); | 
|  | 324 | } | 
|  | 325 |  | 
|  | 326 | void notifyNoFocusedWindowAnr( | 
|  | 327 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { | 
|  | 328 | std::scoped_lock lock(mLock); | 
|  | 329 | mAnrApplications.push(applicationHandle); | 
|  | 330 | mNotifyAnr.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 331 | } | 
|  | 332 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 333 | void notifyInputChannelBroken(const sp<IBinder>&) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 334 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 335 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} | 
| Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 336 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 337 | void notifyUntrustedTouch(const std::string& obscuringPackage) override {} | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 338 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, | 
|  | 339 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, | 
|  | 340 | const std::vector<float>& values) override {} | 
|  | 341 |  | 
|  | 342 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, | 
|  | 343 | InputDeviceSensorAccuracy accuracy) override {} | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 344 |  | 
| Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 345 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} | 
|  | 346 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 347 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 348 | *outConfig = mConfig; | 
|  | 349 | } | 
|  | 350 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 351 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 352 | std::scoped_lock lock(mLock); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 353 | switch (inputEvent->getType()) { | 
|  | 354 | case AINPUT_EVENT_TYPE_KEY: { | 
|  | 355 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 356 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 357 | break; | 
|  | 358 | } | 
|  | 359 |  | 
|  | 360 | case AINPUT_EVENT_TYPE_MOTION: { | 
|  | 361 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 362 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 363 | break; | 
|  | 364 | } | 
|  | 365 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 366 | return true; | 
|  | 367 | } | 
|  | 368 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 369 | void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 370 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 371 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 372 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 373 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 374 | return 0; | 
|  | 375 | } | 
|  | 376 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 377 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 378 | return false; | 
|  | 379 | } | 
|  | 380 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 381 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, | 
|  | 382 | uint32_t policyFlags) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 383 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 384 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is | 
|  | 385 | * essentially a passthrough for notifySwitch. | 
|  | 386 | */ | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 387 | mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 388 | } | 
|  | 389 |  | 
| Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 390 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 391 |  | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 392 | bool checkInjectEventsPermissionNonReentrant(int32_t pid, int32_t uid) override { | 
|  | 393 | return pid == INJECTOR_PID && uid == INJECTOR_UID; | 
|  | 394 | } | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 395 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 396 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 397 | std::scoped_lock lock(mLock); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 398 | mOnPointerDownToken = newToken; | 
|  | 399 | } | 
|  | 400 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 401 | void setPointerCapture(bool enabled) override { | 
|  | 402 | std::scoped_lock lock(mLock); | 
|  | 403 | mPointerCaptureEnabled = {enabled}; | 
|  | 404 | mPointerCaptureChangedCondition.notify_all(); | 
|  | 405 | } | 
|  | 406 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 407 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { | 
|  | 408 | std::scoped_lock lock(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 409 | mNotifyDropWindowWasCalled = true; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 410 | mDropTargetWindowToken = token; | 
|  | 411 | } | 
|  | 412 |  | 
| Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 413 | void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action, | 
|  | 414 | int32_t displayId) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 415 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 416 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; | 
|  | 417 | ASSERT_EQ(mFilteredEvent->getType(), type); | 
|  | 418 |  | 
|  | 419 | if (type == AINPUT_EVENT_TYPE_KEY) { | 
|  | 420 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent); | 
|  | 421 | EXPECT_EQ(keyEvent.getEventTime(), eventTime); | 
|  | 422 | EXPECT_EQ(keyEvent.getAction(), action); | 
|  | 423 | EXPECT_EQ(keyEvent.getDisplayId(), displayId); | 
|  | 424 | } else if (type == AINPUT_EVENT_TYPE_MOTION) { | 
|  | 425 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent); | 
|  | 426 | EXPECT_EQ(motionEvent.getEventTime(), eventTime); | 
|  | 427 | EXPECT_EQ(motionEvent.getAction(), action); | 
|  | 428 | EXPECT_EQ(motionEvent.getDisplayId(), displayId); | 
|  | 429 | } else { | 
|  | 430 | FAIL() << "Unknown type: " << type; | 
|  | 431 | } | 
|  | 432 |  | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 433 | mFilteredEvent = nullptr; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 434 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 435 | }; | 
|  | 436 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 437 | // --- InputDispatcherTest --- | 
|  | 438 |  | 
|  | 439 | class InputDispatcherTest : public testing::Test { | 
|  | 440 | protected: | 
|  | 441 | sp<FakeInputDispatcherPolicy> mFakePolicy; | 
|  | 442 | sp<InputDispatcher> mDispatcher; | 
|  | 443 |  | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 444 | void SetUp() override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 445 | mFakePolicy = new FakeInputDispatcherPolicy(); | 
|  | 446 | mDispatcher = new InputDispatcher(mFakePolicy); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 447 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 448 | // Start InputDispatcher thread | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 449 | ASSERT_EQ(OK, mDispatcher->start()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 450 | } | 
|  | 451 |  | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 452 | void TearDown() override { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 453 | ASSERT_EQ(OK, mDispatcher->stop()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 454 | mFakePolicy.clear(); | 
|  | 455 | mDispatcher.clear(); | 
|  | 456 | } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 457 |  | 
|  | 458 | /** | 
|  | 459 | * Used for debugging when writing the test | 
|  | 460 | */ | 
|  | 461 | void dumpDispatcherState() { | 
|  | 462 | std::string dump; | 
|  | 463 | mDispatcher->dump(dump); | 
|  | 464 | std::stringstream ss(dump); | 
|  | 465 | std::string to; | 
|  | 466 |  | 
|  | 467 | while (std::getline(ss, to, '\n')) { | 
|  | 468 | ALOGE("%s", to.c_str()); | 
|  | 469 | } | 
|  | 470 | } | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 471 |  | 
|  | 472 | void setFocusedWindow(const sp<InputWindowHandle>& window, | 
|  | 473 | const sp<InputWindowHandle>& focusedWindow = nullptr) { | 
|  | 474 | FocusRequest request; | 
|  | 475 | request.token = window->getToken(); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 476 | request.windowName = window->getName(); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 477 | if (focusedWindow) { | 
|  | 478 | request.focusedToken = focusedWindow->getToken(); | 
|  | 479 | } | 
|  | 480 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 481 | request.displayId = window->getInfo()->displayId; | 
|  | 482 | mDispatcher->setFocusedWindow(request); | 
|  | 483 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 484 | }; | 
|  | 485 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 486 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { | 
|  | 487 | KeyEvent event; | 
|  | 488 |  | 
|  | 489 | // Rejects undefined key actions. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 490 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 491 | INVALID_HMAC, | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 492 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, | 
|  | 493 | ARBITRARY_TIME); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 494 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 495 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 496 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 497 | << "Should reject key events with undefined action."; | 
|  | 498 |  | 
|  | 499 | // 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] | 500 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 501 | 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] | 502 | ARBITRARY_TIME, ARBITRARY_TIME); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 503 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 504 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 505 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 506 | << "Should reject key events with ACTION_MULTIPLE."; | 
|  | 507 | } | 
|  | 508 |  | 
|  | 509 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { | 
|  | 510 | MotionEvent event; | 
|  | 511 | PointerProperties pointerProperties[MAX_POINTERS + 1]; | 
|  | 512 | PointerCoords pointerCoords[MAX_POINTERS + 1]; | 
|  | 513 | for (int i = 0; i <= MAX_POINTERS; i++) { | 
|  | 514 | pointerProperties[i].clear(); | 
|  | 515 | pointerProperties[i].id = i; | 
|  | 516 | pointerCoords[i].clear(); | 
|  | 517 | } | 
|  | 518 |  | 
| Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 519 | // Some constants commonly used below | 
|  | 520 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; | 
|  | 521 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; | 
|  | 522 | constexpr int32_t metaState = AMETA_NONE; | 
|  | 523 | constexpr MotionClassification classification = MotionClassification::NONE; | 
|  | 524 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 525 | ui::Transform identityTransform; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 526 | // Rejects undefined motion actions. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 527 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 528 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, | 
|  | 529 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 530 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
|  | 531 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 532 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 533 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 534 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 535 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 536 | << "Should reject motion events with undefined action."; | 
|  | 537 |  | 
|  | 538 | // Rejects pointer down with invalid index. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 539 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 540 | AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 541 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 542 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 543 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 544 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 545 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, | 
|  | 546 | pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 547 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 548 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 549 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 550 | << "Should reject motion events with pointer down index too large."; | 
|  | 551 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 552 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 553 | AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 554 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 555 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 556 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 557 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 558 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, | 
|  | 559 | pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 560 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 561 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 562 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 563 | << "Should reject motion events with pointer down index too small."; | 
|  | 564 |  | 
|  | 565 | // Rejects pointer up with invalid index. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 566 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 567 | AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 568 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 569 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 570 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 571 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 572 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, | 
|  | 573 | pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 574 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 575 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 576 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 577 | << "Should reject motion events with pointer up index too large."; | 
|  | 578 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 579 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 580 | AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 581 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 582 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 583 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 584 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 585 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, | 
|  | 586 | pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 587 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 588 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 589 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 590 | << "Should reject motion events with pointer up index too small."; | 
|  | 591 |  | 
|  | 592 | // Rejects motion events with invalid number of pointers. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 593 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 594 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 595 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 596 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
|  | 597 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 598 | /*pointerCount*/ 0, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 599 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 600 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 601 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 602 | << "Should reject motion events with 0 pointers."; | 
|  | 603 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 604 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 605 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 606 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 607 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
|  | 608 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 609 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 610 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 611 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 612 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 613 | << "Should reject motion events with more than MAX_POINTERS pointers."; | 
|  | 614 |  | 
|  | 615 | // Rejects motion events with invalid pointer ids. | 
|  | 616 | pointerProperties[0].id = -1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 617 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 618 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 619 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 620 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
|  | 621 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 622 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 623 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 624 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 625 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 626 | << "Should reject motion events with pointer ids less than 0."; | 
|  | 627 |  | 
|  | 628 | pointerProperties[0].id = MAX_POINTER_ID + 1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 629 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 630 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 631 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 632 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
|  | 633 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 634 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 635 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 636 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 637 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 638 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; | 
|  | 639 |  | 
|  | 640 | // Rejects motion events with duplicate pointer ids. | 
|  | 641 | pointerProperties[0].id = 1; | 
|  | 642 | pointerProperties[1].id = 1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 643 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 644 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 645 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 646 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
|  | 647 | AMOTION_EVENT_INVALID_DISPLAY_SIZE, ARBITRARY_TIME, ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 648 | /*pointerCount*/ 2, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 649 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 650 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 651 | InputEventInjectionSync::NONE, 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 652 | << "Should reject motion events with duplicate pointer ids."; | 
|  | 653 | } | 
|  | 654 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 655 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ | 
|  | 656 |  | 
|  | 657 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { | 
|  | 658 | constexpr nsecs_t eventTime = 20; | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 659 | NotifyConfigurationChangedArgs args(10 /*id*/, eventTime); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 660 | mDispatcher->notifyConfigurationChanged(&args); | 
|  | 661 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 662 |  | 
|  | 663 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); | 
|  | 664 | } | 
|  | 665 |  | 
|  | 666 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 667 | NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/, | 
|  | 668 | 2 /*switchMask*/); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 669 | mDispatcher->notifySwitch(&args); | 
|  | 670 |  | 
|  | 671 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener | 
|  | 672 | args.policyFlags |= POLICY_FLAG_TRUSTED; | 
|  | 673 | mFakePolicy->assertNotifySwitchWasCalled(args); | 
|  | 674 | } | 
|  | 675 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 676 | // --- InputDispatcherTest SetInputWindowTest --- | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 677 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 678 | static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 679 |  | 
|  | 680 | class FakeApplicationHandle : public InputApplicationHandle { | 
|  | 681 | public: | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 682 | FakeApplicationHandle() { | 
|  | 683 | mInfo.name = "Fake Application"; | 
|  | 684 | mInfo.token = new BBinder(); | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 685 | mInfo.dispatchingTimeoutMillis = | 
|  | 686 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 687 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 688 | virtual ~FakeApplicationHandle() {} | 
|  | 689 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 690 | virtual bool updateInfo() override { return true; } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 691 |  | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 692 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { | 
|  | 693 | mInfo.dispatchingTimeoutMillis = timeout.count(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 694 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 695 | }; | 
|  | 696 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 697 | class FakeInputReceiver { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 698 | public: | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 699 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 700 | : mName(name) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 701 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 702 | } | 
|  | 703 |  | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 704 | InputEvent* consume() { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 705 | InputEvent* event; | 
|  | 706 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); | 
|  | 707 | if (!consumeSeq) { | 
|  | 708 | return nullptr; | 
|  | 709 | } | 
|  | 710 | finishEvent(*consumeSeq); | 
|  | 711 | return event; | 
|  | 712 | } | 
|  | 713 |  | 
|  | 714 | /** | 
|  | 715 | * Receive an event without acknowledging it. | 
|  | 716 | * Return the sequence number that could later be used to send finished signal. | 
|  | 717 | */ | 
|  | 718 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 719 | uint32_t consumeSeq; | 
|  | 720 | InputEvent* event; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 721 |  | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 722 | std::chrono::time_point start = std::chrono::steady_clock::now(); | 
|  | 723 | status_t status = WOULD_BLOCK; | 
|  | 724 | while (status == WOULD_BLOCK) { | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 725 | status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 726 | &event); | 
|  | 727 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; | 
|  | 728 | if (elapsed > 100ms) { | 
|  | 729 | break; | 
|  | 730 | } | 
|  | 731 | } | 
|  | 732 |  | 
|  | 733 | if (status == WOULD_BLOCK) { | 
|  | 734 | // Just means there's no event available. | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 735 | return std::nullopt; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 736 | } | 
|  | 737 |  | 
|  | 738 | if (status != OK) { | 
|  | 739 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 740 | return std::nullopt; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 741 | } | 
|  | 742 | if (event == nullptr) { | 
|  | 743 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; | 
| 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 | } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 746 | if (outEvent != nullptr) { | 
|  | 747 | *outEvent = event; | 
|  | 748 | } | 
|  | 749 | return consumeSeq; | 
|  | 750 | } | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 751 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 752 | /** | 
|  | 753 | * To be used together with "receiveEvent" to complete the consumption of an event. | 
|  | 754 | */ | 
|  | 755 | void finishEvent(uint32_t consumeSeq) { | 
|  | 756 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); | 
|  | 757 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 758 | } | 
|  | 759 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 760 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { | 
|  | 761 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); | 
|  | 762 | ASSERT_EQ(OK, status); | 
|  | 763 | } | 
|  | 764 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 765 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, | 
|  | 766 | std::optional<int32_t> expectedDisplayId, | 
|  | 767 | std::optional<int32_t> expectedFlags) { | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 768 | InputEvent* event = consume(); | 
|  | 769 |  | 
|  | 770 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 771 | << ": consumer should have returned non-NULL event."; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 772 | ASSERT_EQ(expectedEventType, event->getType()) | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 773 | << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType) | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 774 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 775 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 776 | if (expectedDisplayId.has_value()) { | 
|  | 777 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); | 
|  | 778 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 779 |  | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 780 | switch (expectedEventType) { | 
|  | 781 | case AINPUT_EVENT_TYPE_KEY: { | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 782 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); | 
|  | 783 | EXPECT_EQ(expectedAction, keyEvent.getAction()); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 784 | if (expectedFlags.has_value()) { | 
|  | 785 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); | 
|  | 786 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 787 | break; | 
|  | 788 | } | 
|  | 789 | case AINPUT_EVENT_TYPE_MOTION: { | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 790 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); | 
|  | 791 | EXPECT_EQ(expectedAction, motionEvent.getAction()); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 792 | if (expectedFlags.has_value()) { | 
|  | 793 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); | 
|  | 794 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 795 | break; | 
|  | 796 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 797 | case AINPUT_EVENT_TYPE_FOCUS: { | 
|  | 798 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; | 
|  | 799 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 800 | case AINPUT_EVENT_TYPE_CAPTURE: { | 
|  | 801 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; | 
|  | 802 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 803 | case AINPUT_EVENT_TYPE_DRAG: { | 
|  | 804 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; | 
|  | 805 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 806 | default: { | 
|  | 807 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; | 
|  | 808 | } | 
|  | 809 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 810 | } | 
|  | 811 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 812 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { | 
|  | 813 | InputEvent* event = consume(); | 
|  | 814 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 815 | << ": consumer should have returned non-NULL event."; | 
|  | 816 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType()) | 
|  | 817 | << "Got " << inputEventTypeToString(event->getType()) | 
|  | 818 | << " event instead of FOCUS event"; | 
|  | 819 |  | 
|  | 820 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 821 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 822 |  | 
|  | 823 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); | 
|  | 824 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); | 
|  | 825 | EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode()); | 
|  | 826 | } | 
|  | 827 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 828 | void consumeCaptureEvent(bool hasCapture) { | 
|  | 829 | const InputEvent* event = consume(); | 
|  | 830 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 831 | << ": consumer should have returned non-NULL event."; | 
|  | 832 | ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType()) | 
|  | 833 | << "Got " << inputEventTypeToString(event->getType()) | 
|  | 834 | << " event instead of CAPTURE event"; | 
|  | 835 |  | 
|  | 836 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 837 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 838 |  | 
|  | 839 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); | 
|  | 840 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); | 
|  | 841 | } | 
|  | 842 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 843 | void consumeDragEvent(bool isExiting, float x, float y) { | 
|  | 844 | const InputEvent* event = consume(); | 
|  | 845 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 846 | << ": consumer should have returned non-NULL event."; | 
|  | 847 | ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType()) | 
|  | 848 | << "Got " << inputEventTypeToString(event->getType()) | 
|  | 849 | << " event instead of DRAG event"; | 
|  | 850 |  | 
|  | 851 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 852 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 853 |  | 
|  | 854 | const auto& dragEvent = static_cast<const DragEvent&>(*event); | 
|  | 855 | EXPECT_EQ(isExiting, dragEvent.isExiting()); | 
|  | 856 | EXPECT_EQ(x, dragEvent.getX()); | 
|  | 857 | EXPECT_EQ(y, dragEvent.getY()); | 
|  | 858 | } | 
|  | 859 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 860 | void assertNoEvents() { | 
|  | 861 | InputEvent* event = consume(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 862 | if (event == nullptr) { | 
|  | 863 | return; | 
|  | 864 | } | 
|  | 865 | if (event->getType() == AINPUT_EVENT_TYPE_KEY) { | 
|  | 866 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); | 
|  | 867 | ADD_FAILURE() << "Received key event " | 
|  | 868 | << KeyEvent::actionToString(keyEvent.getAction()); | 
|  | 869 | } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) { | 
|  | 870 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 871 | ADD_FAILURE() << "Received motion event " | 
|  | 872 | << MotionEvent::actionToString(motionEvent.getAction()); | 
|  | 873 | } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) { | 
|  | 874 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); | 
|  | 875 | ADD_FAILURE() << "Received focus event, hasFocus = " | 
|  | 876 | << (focusEvent.getHasFocus() ? "true" : "false"); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 877 | } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) { | 
|  | 878 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); | 
|  | 879 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " | 
|  | 880 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 881 | } | 
|  | 882 | FAIL() << mName.c_str() | 
|  | 883 | << ": should not have received any events, so consume() should return NULL"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 884 | } | 
|  | 885 |  | 
|  | 886 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } | 
|  | 887 |  | 
|  | 888 | protected: | 
|  | 889 | std::unique_ptr<InputConsumer> mConsumer; | 
|  | 890 | PreallocatedInputEventFactory mEventFactory; | 
|  | 891 |  | 
|  | 892 | std::string mName; | 
|  | 893 | }; | 
|  | 894 |  | 
|  | 895 | class FakeWindowHandle : public InputWindowHandle { | 
|  | 896 | public: | 
|  | 897 | static const int32_t WIDTH = 600; | 
|  | 898 | static const int32_t HEIGHT = 800; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 899 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 900 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 901 | const sp<InputDispatcher>& dispatcher, const std::string name, | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 902 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 903 | : mName(name) { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 904 | if (token == std::nullopt) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 905 | base::Result<std::unique_ptr<InputChannel>> channel = | 
|  | 906 | dispatcher->createInputChannel(name); | 
|  | 907 | token = (*channel)->getConnectionToken(); | 
|  | 908 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 909 | } | 
|  | 910 |  | 
|  | 911 | inputApplicationHandle->updateInfo(); | 
|  | 912 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); | 
|  | 913 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 914 | mInfo.token = *token; | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 915 | mInfo.id = sId++; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 916 | mInfo.name = name; | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 917 | mInfo.type = InputWindowInfo::Type::APPLICATION; | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 918 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 919 | mInfo.alpha = 1.0; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 920 | mInfo.frameLeft = 0; | 
|  | 921 | mInfo.frameTop = 0; | 
|  | 922 | mInfo.frameRight = WIDTH; | 
|  | 923 | mInfo.frameBottom = HEIGHT; | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 924 | mInfo.transform.set(0, 0); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 925 | mInfo.globalScaleFactor = 1.0; | 
|  | 926 | mInfo.touchableRegion.clear(); | 
|  | 927 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); | 
|  | 928 | mInfo.visible = true; | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 929 | mInfo.focusable = false; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 930 | mInfo.hasWallpaper = false; | 
|  | 931 | mInfo.paused = false; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 932 | mInfo.ownerPid = INJECTOR_PID; | 
|  | 933 | mInfo.ownerUid = INJECTOR_UID; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 934 | mInfo.displayId = displayId; | 
|  | 935 | } | 
|  | 936 |  | 
|  | 937 | virtual bool updateInfo() { return true; } | 
|  | 938 |  | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 939 | void setFocusable(bool focusable) { mInfo.focusable = focusable; } | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 940 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 941 | void setVisible(bool visible) { mInfo.visible = visible; } | 
|  | 942 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 943 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 944 | mInfo.dispatchingTimeout = timeout; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 945 | } | 
|  | 946 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 947 | void setPaused(bool paused) { mInfo.paused = paused; } | 
|  | 948 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 949 | void setAlpha(float alpha) { mInfo.alpha = alpha; } | 
|  | 950 |  | 
|  | 951 | void setTouchOcclusionMode(android::os::TouchOcclusionMode mode) { | 
|  | 952 | mInfo.touchOcclusionMode = mode; | 
|  | 953 | } | 
|  | 954 |  | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 955 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } | 
|  | 956 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 957 | void setFrame(const Rect& frame) { | 
|  | 958 | mInfo.frameLeft = frame.left; | 
|  | 959 | mInfo.frameTop = frame.top; | 
|  | 960 | mInfo.frameRight = frame.right; | 
|  | 961 | mInfo.frameBottom = frame.bottom; | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 962 | mInfo.transform.set(-frame.left, -frame.top); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 963 | mInfo.touchableRegion.clear(); | 
|  | 964 | mInfo.addTouchableRegion(frame); | 
|  | 965 | } | 
|  | 966 |  | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 967 | void addFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags |= flags; } | 
|  | 968 |  | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 969 | void setFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags = flags; } | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 970 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 971 | void setInputFeatures(InputWindowInfo::Feature features) { mInfo.inputFeatures = features; } | 
|  | 972 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 973 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { | 
|  | 974 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); | 
|  | 975 | } | 
|  | 976 |  | 
|  | 977 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 978 |  | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 979 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } | 
|  | 980 |  | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 981 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 982 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, | 
|  | 983 | expectedFlags); | 
|  | 984 | } | 
|  | 985 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 986 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 987 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); | 
|  | 988 | } | 
|  | 989 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 990 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 991 | int32_t expectedFlags = 0) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 992 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId, | 
|  | 993 | expectedFlags); | 
|  | 994 | } | 
|  | 995 |  | 
|  | 996 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 997 | int32_t expectedFlags = 0) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 998 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId, | 
|  | 999 | expectedFlags); | 
|  | 1000 | } | 
|  | 1001 |  | 
|  | 1002 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1003 | int32_t expectedFlags = 0) { | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1004 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); | 
|  | 1005 | } | 
|  | 1006 |  | 
|  | 1007 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, | 
|  | 1008 | std::optional<int32_t> expectedFlags = std::nullopt) { | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1009 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, | 
|  | 1010 | expectedFlags); | 
|  | 1011 | } | 
|  | 1012 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1013 | void consumeMotionPointerDown(int32_t pointerIdx, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1014 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
|  | 1015 | int32_t expectedFlags = 0) { | 
|  | 1016 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1017 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1018 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); | 
|  | 1019 | } | 
|  | 1020 |  | 
|  | 1021 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1022 | int32_t expectedFlags = 0) { | 
|  | 1023 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1024 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1025 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); | 
|  | 1026 | } | 
|  | 1027 |  | 
|  | 1028 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1029 | int32_t expectedFlags = 0) { | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1030 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, | 
|  | 1031 | expectedFlags); | 
|  | 1032 | } | 
|  | 1033 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1034 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
|  | 1035 | int32_t expectedFlags = 0) { | 
|  | 1036 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, | 
|  | 1037 | expectedFlags); | 
|  | 1038 | } | 
|  | 1039 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1040 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { | 
|  | 1041 | ASSERT_NE(mInputReceiver, nullptr) | 
|  | 1042 | << "Cannot consume events from a window with no receiver"; | 
|  | 1043 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); | 
|  | 1044 | } | 
|  | 1045 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1046 | void consumeCaptureEvent(bool hasCapture) { | 
|  | 1047 | ASSERT_NE(mInputReceiver, nullptr) | 
|  | 1048 | << "Cannot consume events from a window with no receiver"; | 
|  | 1049 | mInputReceiver->consumeCaptureEvent(hasCapture); | 
|  | 1050 | } | 
|  | 1051 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1052 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, | 
|  | 1053 | std::optional<int32_t> expectedDisplayId, | 
|  | 1054 | std::optional<int32_t> expectedFlags) { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1055 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; | 
|  | 1056 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, | 
|  | 1057 | expectedFlags); | 
|  | 1058 | } | 
|  | 1059 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1060 | void consumeDragEvent(bool isExiting, float x, float y) { | 
|  | 1061 | mInputReceiver->consumeDragEvent(isExiting, x, y); | 
|  | 1062 | } | 
|  | 1063 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1064 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1065 | if (mInputReceiver == nullptr) { | 
|  | 1066 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; | 
|  | 1067 | return std::nullopt; | 
|  | 1068 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1069 | return mInputReceiver->receiveEvent(outEvent); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1070 | } | 
|  | 1071 |  | 
|  | 1072 | void finishEvent(uint32_t sequenceNum) { | 
|  | 1073 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; | 
|  | 1074 | mInputReceiver->finishEvent(sequenceNum); | 
|  | 1075 | } | 
|  | 1076 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1077 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { | 
|  | 1078 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; | 
|  | 1079 | mInputReceiver->sendTimeline(inputEventId, timeline); | 
|  | 1080 | } | 
|  | 1081 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1082 | InputEvent* consume() { | 
|  | 1083 | if (mInputReceiver == nullptr) { | 
|  | 1084 | return nullptr; | 
|  | 1085 | } | 
|  | 1086 | return mInputReceiver->consume(); | 
|  | 1087 | } | 
|  | 1088 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1089 | MotionEvent* consumeMotion() { | 
|  | 1090 | InputEvent* event = consume(); | 
|  | 1091 | if (event == nullptr) { | 
|  | 1092 | ADD_FAILURE() << "Consume failed : no event"; | 
|  | 1093 | return nullptr; | 
|  | 1094 | } | 
|  | 1095 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { | 
|  | 1096 | ADD_FAILURE() << "Instead of motion event, got " | 
|  | 1097 | << inputEventTypeToString(event->getType()); | 
|  | 1098 | return nullptr; | 
|  | 1099 | } | 
|  | 1100 | return static_cast<MotionEvent*>(event); | 
|  | 1101 | } | 
|  | 1102 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1103 | void assertNoEvents() { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1104 | if (mInputReceiver == nullptr && | 
|  | 1105 | mInfo.inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL)) { | 
|  | 1106 | return; // Can't receive events if the window does not have input channel | 
|  | 1107 | } | 
|  | 1108 | ASSERT_NE(nullptr, mInputReceiver) | 
|  | 1109 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1110 | mInputReceiver->assertNoEvents(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1111 | } | 
|  | 1112 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1113 | sp<IBinder> getToken() { return mInfo.token; } | 
|  | 1114 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1115 | const std::string& getName() { return mName; } | 
|  | 1116 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1117 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { | 
|  | 1118 | mInfo.ownerPid = ownerPid; | 
|  | 1119 | mInfo.ownerUid = ownerUid; | 
|  | 1120 | } | 
|  | 1121 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1122 | private: | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1123 | const std::string mName; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1124 | std::unique_ptr<FakeInputReceiver> mInputReceiver; | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1125 | 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] | 1126 | }; | 
|  | 1127 |  | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1128 | std::atomic<int32_t> FakeWindowHandle::sId{1}; | 
|  | 1129 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1130 | static InputEventInjectionResult injectKey( | 
|  | 1131 | const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, | 
|  | 1132 | int32_t displayId = ADISPLAY_ID_NONE, | 
|  | 1133 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1134 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
|  | 1135 | bool allowKeyRepeat = true) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1136 | KeyEvent event; | 
|  | 1137 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1138 |  | 
|  | 1139 | // Define a valid key down event. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1140 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1141 | INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE, | 
|  | 1142 | repeatCount, currentTime, currentTime); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1143 |  | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1144 | int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; | 
|  | 1145 | if (!allowKeyRepeat) { | 
|  | 1146 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; | 
|  | 1147 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1148 | // Inject event until dispatch out. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1149 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1150 | injectionTimeout, policyFlags); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1151 | } | 
|  | 1152 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1153 | static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher, | 
|  | 1154 | int32_t displayId = ADISPLAY_ID_NONE) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1155 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId); | 
|  | 1156 | } | 
|  | 1157 |  | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1158 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without | 
|  | 1159 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it | 
|  | 1160 | // has to be woken up to process the repeating key. | 
|  | 1161 | static InputEventInjectionResult injectKeyDownNoRepeat(const sp<InputDispatcher>& dispatcher, | 
|  | 1162 | int32_t displayId = ADISPLAY_ID_NONE) { | 
|  | 1163 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId, | 
|  | 1164 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, | 
|  | 1165 | /* allowKeyRepeat */ false); | 
|  | 1166 | } | 
|  | 1167 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1168 | static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher, | 
|  | 1169 | int32_t displayId = ADISPLAY_ID_NONE) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1170 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId); | 
|  | 1171 | } | 
|  | 1172 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1173 | class PointerBuilder { | 
|  | 1174 | public: | 
|  | 1175 | PointerBuilder(int32_t id, int32_t toolType) { | 
|  | 1176 | mProperties.clear(); | 
|  | 1177 | mProperties.id = id; | 
|  | 1178 | mProperties.toolType = toolType; | 
|  | 1179 | mCoords.clear(); | 
|  | 1180 | } | 
|  | 1181 |  | 
|  | 1182 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } | 
|  | 1183 |  | 
|  | 1184 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } | 
|  | 1185 |  | 
|  | 1186 | PointerBuilder& axis(int32_t axis, float value) { | 
|  | 1187 | mCoords.setAxisValue(axis, value); | 
|  | 1188 | return *this; | 
|  | 1189 | } | 
|  | 1190 |  | 
|  | 1191 | PointerProperties buildProperties() const { return mProperties; } | 
|  | 1192 |  | 
|  | 1193 | PointerCoords buildCoords() const { return mCoords; } | 
|  | 1194 |  | 
|  | 1195 | private: | 
|  | 1196 | PointerProperties mProperties; | 
|  | 1197 | PointerCoords mCoords; | 
|  | 1198 | }; | 
|  | 1199 |  | 
|  | 1200 | class MotionEventBuilder { | 
|  | 1201 | public: | 
|  | 1202 | MotionEventBuilder(int32_t action, int32_t source) { | 
|  | 1203 | mAction = action; | 
|  | 1204 | mSource = source; | 
|  | 1205 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1206 | } | 
|  | 1207 |  | 
|  | 1208 | MotionEventBuilder& eventTime(nsecs_t eventTime) { | 
|  | 1209 | mEventTime = eventTime; | 
|  | 1210 | return *this; | 
|  | 1211 | } | 
|  | 1212 |  | 
|  | 1213 | MotionEventBuilder& displayId(int32_t displayId) { | 
|  | 1214 | mDisplayId = displayId; | 
|  | 1215 | return *this; | 
|  | 1216 | } | 
|  | 1217 |  | 
|  | 1218 | MotionEventBuilder& actionButton(int32_t actionButton) { | 
|  | 1219 | mActionButton = actionButton; | 
|  | 1220 | return *this; | 
|  | 1221 | } | 
|  | 1222 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1223 | MotionEventBuilder& buttonState(int32_t buttonState) { | 
|  | 1224 | mButtonState = buttonState; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1225 | return *this; | 
|  | 1226 | } | 
|  | 1227 |  | 
|  | 1228 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { | 
|  | 1229 | mRawXCursorPosition = rawXCursorPosition; | 
|  | 1230 | return *this; | 
|  | 1231 | } | 
|  | 1232 |  | 
|  | 1233 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { | 
|  | 1234 | mRawYCursorPosition = rawYCursorPosition; | 
|  | 1235 | return *this; | 
|  | 1236 | } | 
|  | 1237 |  | 
|  | 1238 | MotionEventBuilder& pointer(PointerBuilder pointer) { | 
|  | 1239 | mPointers.push_back(pointer); | 
|  | 1240 | return *this; | 
|  | 1241 | } | 
|  | 1242 |  | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1243 | MotionEventBuilder& addFlag(uint32_t flags) { | 
|  | 1244 | mFlags |= flags; | 
|  | 1245 | return *this; | 
|  | 1246 | } | 
|  | 1247 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1248 | MotionEvent build() { | 
|  | 1249 | std::vector<PointerProperties> pointerProperties; | 
|  | 1250 | std::vector<PointerCoords> pointerCoords; | 
|  | 1251 | for (const PointerBuilder& pointer : mPointers) { | 
|  | 1252 | pointerProperties.push_back(pointer.buildProperties()); | 
|  | 1253 | pointerCoords.push_back(pointer.buildCoords()); | 
|  | 1254 | } | 
|  | 1255 |  | 
|  | 1256 | // Set mouse cursor position for the most common cases to avoid boilerplate. | 
|  | 1257 | if (mSource == AINPUT_SOURCE_MOUSE && | 
|  | 1258 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && | 
|  | 1259 | mPointers.size() == 1) { | 
|  | 1260 | mRawXCursorPosition = pointerCoords[0].getX(); | 
|  | 1261 | mRawYCursorPosition = pointerCoords[0].getY(); | 
|  | 1262 | } | 
|  | 1263 |  | 
|  | 1264 | MotionEvent event; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1265 | ui::Transform identityTransform; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1266 | event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC, | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1267 | mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1268 | mButtonState, MotionClassification::NONE, identityTransform, | 
|  | 1269 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 1270 | mRawYCursorPosition, mDisplayWidth, mDisplayHeight, mEventTime, mEventTime, | 
|  | 1271 | mPointers.size(), pointerProperties.data(), pointerCoords.data()); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1272 |  | 
|  | 1273 | return event; | 
|  | 1274 | } | 
|  | 1275 |  | 
|  | 1276 | private: | 
|  | 1277 | int32_t mAction; | 
|  | 1278 | int32_t mSource; | 
|  | 1279 | nsecs_t mEventTime; | 
|  | 1280 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; | 
|  | 1281 | int32_t mActionButton{0}; | 
|  | 1282 | int32_t mButtonState{0}; | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1283 | int32_t mFlags{0}; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1284 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; | 
|  | 1285 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 1286 | int32_t mDisplayWidth{AMOTION_EVENT_INVALID_DISPLAY_SIZE}; | 
|  | 1287 | int32_t mDisplayHeight{AMOTION_EVENT_INVALID_DISPLAY_SIZE}; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1288 |  | 
|  | 1289 | std::vector<PointerBuilder> mPointers; | 
|  | 1290 | }; | 
|  | 1291 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1292 | static InputEventInjectionResult injectMotionEvent( | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1293 | const sp<InputDispatcher>& dispatcher, const MotionEvent& event, | 
|  | 1294 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1295 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) { | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1296 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode, | 
|  | 1297 | injectionTimeout, | 
|  | 1298 | POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); | 
|  | 1299 | } | 
|  | 1300 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1301 | static InputEventInjectionResult injectMotionEvent( | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1302 | const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId, | 
|  | 1303 | const PointF& position, | 
|  | 1304 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1305 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
|  | 1306 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1307 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1308 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) { | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1309 | MotionEvent event = MotionEventBuilder(action, source) | 
|  | 1310 | .displayId(displayId) | 
|  | 1311 | .eventTime(eventTime) | 
|  | 1312 | .rawXCursorPosition(cursorPosition.x) | 
|  | 1313 | .rawYCursorPosition(cursorPosition.y) | 
|  | 1314 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 1315 | .x(position.x) | 
|  | 1316 | .y(position.y)) | 
|  | 1317 | .build(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1318 |  | 
|  | 1319 | // Inject event until dispatch out. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1320 | return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1321 | } | 
|  | 1322 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1323 | static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher, | 
|  | 1324 | int32_t source, int32_t displayId, | 
|  | 1325 | const PointF& location = {100, 200}) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1326 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1327 | } | 
|  | 1328 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1329 | static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher, | 
|  | 1330 | int32_t source, int32_t displayId, | 
|  | 1331 | const PointF& location = {100, 200}) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1332 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1333 | } | 
|  | 1334 |  | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1335 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { | 
|  | 1336 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1337 | // Define a valid key event. | 
| Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1338 | NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, | 
|  | 1339 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, | 
|  | 1340 | KEY_A, AMETA_NONE, currentTime); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1341 |  | 
|  | 1342 | return args; | 
|  | 1343 | } | 
|  | 1344 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1345 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, | 
|  | 1346 | const std::vector<PointF>& points) { | 
|  | 1347 | size_t pointerCount = points.size(); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1348 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { | 
|  | 1349 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; | 
|  | 1350 | } | 
|  | 1351 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1352 | PointerProperties pointerProperties[pointerCount]; | 
|  | 1353 | PointerCoords pointerCoords[pointerCount]; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1354 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1355 | for (size_t i = 0; i < pointerCount; i++) { | 
|  | 1356 | pointerProperties[i].clear(); | 
|  | 1357 | pointerProperties[i].id = i; | 
|  | 1358 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1359 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1360 | pointerCoords[i].clear(); | 
|  | 1361 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); | 
|  | 1362 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); | 
|  | 1363 | } | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1364 |  | 
|  | 1365 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1366 | // Define a valid motion event. | 
| Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1367 | NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1368 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, | 
|  | 1369 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1370 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, | 
|  | 1371 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1372 | AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 1373 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1374 |  | 
|  | 1375 | return args; | 
|  | 1376 | } | 
|  | 1377 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1378 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { | 
|  | 1379 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); | 
|  | 1380 | } | 
|  | 1381 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1382 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(bool enabled) { | 
|  | 1383 | return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), enabled); | 
|  | 1384 | } | 
|  | 1385 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1386 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1387 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1388 | sp<FakeWindowHandle> window = | 
|  | 1389 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1390 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1391 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1392 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1393 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 1394 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1395 |  | 
|  | 1396 | // Window should receive motion event. | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1397 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1398 | } | 
|  | 1399 |  | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1400 | /** | 
|  | 1401 | * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues. | 
|  | 1402 | * To ensure that window receives only events that were directly inside of it, add | 
|  | 1403 | * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input | 
|  | 1404 | * when finding touched windows. | 
|  | 1405 | * This test serves as a sanity check for the next test, where setInputWindows is | 
|  | 1406 | * called twice. | 
|  | 1407 | */ | 
|  | 1408 | TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1409 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1410 | sp<FakeWindowHandle> window = | 
|  | 1411 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 1412 | window->setFrame(Rect(0, 0, 100, 100)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1413 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1414 |  | 
|  | 1415 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1416 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1417 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1418 | {50, 50})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1419 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1420 |  | 
|  | 1421 | // Window should receive motion event. | 
|  | 1422 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1423 | } | 
|  | 1424 |  | 
|  | 1425 | /** | 
|  | 1426 | * Calling setInputWindows twice, with the same info, should not cause any issues. | 
|  | 1427 | * To ensure that window receives only events that were directly inside of it, add | 
|  | 1428 | * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input | 
|  | 1429 | * when finding touched windows. | 
|  | 1430 | */ | 
|  | 1431 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1432 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1433 | sp<FakeWindowHandle> window = | 
|  | 1434 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 1435 | window->setFrame(Rect(0, 0, 100, 100)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1436 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1437 |  | 
|  | 1438 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 1439 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1440 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1441 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1442 | {50, 50})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1443 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1444 |  | 
|  | 1445 | // Window should receive motion event. | 
|  | 1446 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1447 | } | 
|  | 1448 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1449 | // The foreground window should receive the first touch down event. | 
|  | 1450 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1451 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1452 | sp<FakeWindowHandle> windowTop = | 
|  | 1453 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 1454 | sp<FakeWindowHandle> windowSecond = | 
|  | 1455 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1456 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1457 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1458 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1459 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 1460 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1461 |  | 
|  | 1462 | // 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] | 1463 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1464 | windowSecond->assertNoEvents(); | 
|  | 1465 | } | 
|  | 1466 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1467 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1468 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1469 | sp<FakeWindowHandle> windowLeft = | 
|  | 1470 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
|  | 1471 | windowLeft->setFrame(Rect(0, 0, 600, 800)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1472 | windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1473 | sp<FakeWindowHandle> windowRight = | 
|  | 1474 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
|  | 1475 | windowRight->setFrame(Rect(600, 0, 1200, 800)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1476 | windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1477 |  | 
|  | 1478 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 1479 |  | 
|  | 1480 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); | 
|  | 1481 |  | 
|  | 1482 | // 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] | 1483 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1484 | injectMotionEvent(mDispatcher, | 
|  | 1485 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1486 | AINPUT_SOURCE_MOUSE) | 
|  | 1487 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1488 | .x(900) | 
|  | 1489 | .y(400)) | 
|  | 1490 | .build())); | 
|  | 1491 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1492 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1493 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1494 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1495 |  | 
|  | 1496 | // Move cursor into left window | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1497 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1498 | injectMotionEvent(mDispatcher, | 
|  | 1499 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1500 | AINPUT_SOURCE_MOUSE) | 
|  | 1501 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1502 | .x(300) | 
|  | 1503 | .y(400)) | 
|  | 1504 | .build())); | 
|  | 1505 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 1506 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1507 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1508 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1509 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1510 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1511 |  | 
|  | 1512 | // Inject a series of mouse events for a mouse click | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1513 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1514 | injectMotionEvent(mDispatcher, | 
|  | 1515 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 1516 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1517 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1518 | .x(300) | 
|  | 1519 | .y(400)) | 
|  | 1520 | .build())); | 
|  | 1521 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1522 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1523 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1524 | injectMotionEvent(mDispatcher, | 
|  | 1525 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 1526 | AINPUT_SOURCE_MOUSE) | 
|  | 1527 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1528 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1529 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1530 | .x(300) | 
|  | 1531 | .y(400)) | 
|  | 1532 | .build())); | 
|  | 1533 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 1534 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1535 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1536 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1537 | injectMotionEvent(mDispatcher, | 
|  | 1538 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 1539 | AINPUT_SOURCE_MOUSE) | 
|  | 1540 | .buttonState(0) | 
|  | 1541 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1542 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1543 | .x(300) | 
|  | 1544 | .y(400)) | 
|  | 1545 | .build())); | 
|  | 1546 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 1547 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1548 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1549 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1550 | injectMotionEvent(mDispatcher, | 
|  | 1551 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) | 
|  | 1552 | .buttonState(0) | 
|  | 1553 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1554 | .x(300) | 
|  | 1555 | .y(400)) | 
|  | 1556 | .build())); | 
|  | 1557 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 1558 |  | 
|  | 1559 | // Move mouse cursor back to right window | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1560 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1561 | injectMotionEvent(mDispatcher, | 
|  | 1562 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1563 | AINPUT_SOURCE_MOUSE) | 
|  | 1564 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1565 | .x(900) | 
|  | 1566 | .y(400)) | 
|  | 1567 | .build())); | 
|  | 1568 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 1569 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1570 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1571 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1572 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 1573 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1574 | } | 
|  | 1575 |  | 
|  | 1576 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected | 
|  | 1577 | // directly in this test. | 
|  | 1578 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1579 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1580 | sp<FakeWindowHandle> window = | 
|  | 1581 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 1582 | window->setFrame(Rect(0, 0, 1200, 800)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1583 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1584 |  | 
|  | 1585 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 1586 |  | 
|  | 1587 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 1588 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1589 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1590 | injectMotionEvent(mDispatcher, | 
|  | 1591 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1592 | AINPUT_SOURCE_MOUSE) | 
|  | 1593 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1594 | .x(300) | 
|  | 1595 | .y(400)) | 
|  | 1596 | .build())); | 
|  | 1597 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 1598 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1599 |  | 
|  | 1600 | // Inject a series of mouse events for a mouse click | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1601 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1602 | injectMotionEvent(mDispatcher, | 
|  | 1603 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 1604 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1605 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1606 | .x(300) | 
|  | 1607 | .y(400)) | 
|  | 1608 | .build())); | 
|  | 1609 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1610 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1611 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1612 | injectMotionEvent(mDispatcher, | 
|  | 1613 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 1614 | AINPUT_SOURCE_MOUSE) | 
|  | 1615 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1616 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1617 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1618 | .x(300) | 
|  | 1619 | .y(400)) | 
|  | 1620 | .build())); | 
|  | 1621 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 1622 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1623 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1624 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1625 | injectMotionEvent(mDispatcher, | 
|  | 1626 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 1627 | AINPUT_SOURCE_MOUSE) | 
|  | 1628 | .buttonState(0) | 
|  | 1629 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 1630 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1631 | .x(300) | 
|  | 1632 | .y(400)) | 
|  | 1633 | .build())); | 
|  | 1634 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 1635 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1636 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1637 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1638 | injectMotionEvent(mDispatcher, | 
|  | 1639 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) | 
|  | 1640 | .buttonState(0) | 
|  | 1641 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1642 | .x(300) | 
|  | 1643 | .y(400)) | 
|  | 1644 | .build())); | 
|  | 1645 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 1646 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1647 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1648 | injectMotionEvent(mDispatcher, | 
|  | 1649 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 1650 | AINPUT_SOURCE_MOUSE) | 
|  | 1651 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 1652 | .x(300) | 
|  | 1653 | .y(400)) | 
|  | 1654 | .build())); | 
|  | 1655 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 1656 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); | 
|  | 1657 | } | 
|  | 1658 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1659 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1660 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1661 |  | 
|  | 1662 | sp<FakeWindowHandle> windowLeft = | 
|  | 1663 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
|  | 1664 | windowLeft->setFrame(Rect(0, 0, 600, 800)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1665 | windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1666 | sp<FakeWindowHandle> windowRight = | 
|  | 1667 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
|  | 1668 | windowRight->setFrame(Rect(600, 0, 1200, 800)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1669 | windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1670 |  | 
|  | 1671 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 1672 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1673 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1674 |  | 
|  | 1675 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of | 
|  | 1676 | // left window. This event should be dispatched to the left window. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1677 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1678 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1679 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1680 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1681 | windowRight->assertNoEvents(); | 
|  | 1682 | } | 
|  | 1683 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1684 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1685 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1686 | sp<FakeWindowHandle> window = | 
|  | 1687 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1688 | window->setFocusable(true); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1689 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1690 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1691 | setFocusedWindow(window); | 
|  | 1692 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1693 | window->consumeFocusEvent(true); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1694 |  | 
|  | 1695 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 1696 | mDispatcher->notifyKey(&keyArgs); | 
|  | 1697 |  | 
|  | 1698 | // Window should receive key down event. | 
|  | 1699 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 1700 |  | 
|  | 1701 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED | 
|  | 1702 | // on the app side. | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1703 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1704 | mDispatcher->notifyDeviceReset(&args); | 
|  | 1705 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, | 
|  | 1706 | AKEY_EVENT_FLAG_CANCELED); | 
|  | 1707 | } | 
|  | 1708 |  | 
|  | 1709 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1710 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1711 | sp<FakeWindowHandle> window = | 
|  | 1712 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 1713 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1714 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1715 |  | 
|  | 1716 | NotifyMotionArgs motionArgs = | 
|  | 1717 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1718 | ADISPLAY_ID_DEFAULT); | 
|  | 1719 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 1720 |  | 
|  | 1721 | // Window should receive motion down event. | 
|  | 1722 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1723 |  | 
|  | 1724 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL | 
|  | 1725 | // on the app side. | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1726 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1727 | mDispatcher->notifyDeviceReset(&args); | 
|  | 1728 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, | 
|  | 1729 | 0 /*expectedFlags*/); | 
|  | 1730 | } | 
|  | 1731 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1732 | using TransferFunction = | 
|  | 1733 | std::function<bool(sp<InputDispatcher> dispatcher, sp<IBinder>, sp<IBinder>)>; | 
|  | 1734 |  | 
|  | 1735 | class TransferTouchFixture : public InputDispatcherTest, | 
|  | 1736 | public ::testing::WithParamInterface<TransferFunction> {}; | 
|  | 1737 |  | 
|  | 1738 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1739 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1740 |  | 
|  | 1741 | // Create a couple of windows | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1742 | sp<FakeWindowHandle> firstWindow = | 
|  | 1743 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
|  | 1744 | sp<FakeWindowHandle> secondWindow = | 
|  | 1745 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1746 |  | 
|  | 1747 | // Add the windows to the dispatcher | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1748 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1749 |  | 
|  | 1750 | // Send down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1751 | NotifyMotionArgs downMotionArgs = | 
|  | 1752 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1753 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1754 | mDispatcher->notifyMotion(&downMotionArgs); | 
|  | 1755 | // Only the first window should get the down event | 
|  | 1756 | firstWindow->consumeMotionDown(); | 
|  | 1757 | secondWindow->assertNoEvents(); | 
|  | 1758 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1759 | // Transfer touch to the second window | 
|  | 1760 | TransferFunction f = GetParam(); | 
|  | 1761 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); | 
|  | 1762 | ASSERT_TRUE(success); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1763 | // The first window gets cancel and the second gets down | 
|  | 1764 | firstWindow->consumeMotionCancel(); | 
|  | 1765 | secondWindow->consumeMotionDown(); | 
|  | 1766 |  | 
|  | 1767 | // Send up event to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1768 | NotifyMotionArgs upMotionArgs = | 
|  | 1769 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1770 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1771 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 1772 | // The first  window gets no events and the second gets up | 
|  | 1773 | firstWindow->assertNoEvents(); | 
|  | 1774 | secondWindow->consumeMotionUp(); | 
|  | 1775 | } | 
|  | 1776 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1777 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1778 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1779 |  | 
|  | 1780 | PointF touchPoint = {10, 10}; | 
|  | 1781 |  | 
|  | 1782 | // Create a couple of windows | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1783 | sp<FakeWindowHandle> firstWindow = | 
|  | 1784 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
|  | 1785 | sp<FakeWindowHandle> secondWindow = | 
|  | 1786 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1787 |  | 
|  | 1788 | // Add the windows to the dispatcher | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1789 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1790 |  | 
|  | 1791 | // Send down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1792 | NotifyMotionArgs downMotionArgs = | 
|  | 1793 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1794 | ADISPLAY_ID_DEFAULT, {touchPoint}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1795 | mDispatcher->notifyMotion(&downMotionArgs); | 
|  | 1796 | // Only the first window should get the down event | 
|  | 1797 | firstWindow->consumeMotionDown(); | 
|  | 1798 | secondWindow->assertNoEvents(); | 
|  | 1799 |  | 
|  | 1800 | // Send pointer down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1801 | NotifyMotionArgs pointerDownMotionArgs = | 
|  | 1802 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1803 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1804 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1805 | {touchPoint, touchPoint}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1806 | mDispatcher->notifyMotion(&pointerDownMotionArgs); | 
|  | 1807 | // Only the first window should get the pointer down event | 
|  | 1808 | firstWindow->consumeMotionPointerDown(1); | 
|  | 1809 | secondWindow->assertNoEvents(); | 
|  | 1810 |  | 
|  | 1811 | // Transfer touch focus to the second window | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1812 | TransferFunction f = GetParam(); | 
|  | 1813 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); | 
|  | 1814 | ASSERT_TRUE(success); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1815 | // The first window gets cancel and the second gets down and pointer down | 
|  | 1816 | firstWindow->consumeMotionCancel(); | 
|  | 1817 | secondWindow->consumeMotionDown(); | 
|  | 1818 | secondWindow->consumeMotionPointerDown(1); | 
|  | 1819 |  | 
|  | 1820 | // Send pointer up to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1821 | NotifyMotionArgs pointerUpMotionArgs = | 
|  | 1822 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1823 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1824 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1825 | {touchPoint, touchPoint}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1826 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 1827 | // The first window gets nothing and the second gets pointer up | 
|  | 1828 | firstWindow->assertNoEvents(); | 
|  | 1829 | secondWindow->consumeMotionPointerUp(1); | 
|  | 1830 |  | 
|  | 1831 | // Send up event to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1832 | NotifyMotionArgs upMotionArgs = | 
|  | 1833 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1834 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1835 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 1836 | // The first window gets nothing and the second gets up | 
|  | 1837 | firstWindow->assertNoEvents(); | 
|  | 1838 | secondWindow->consumeMotionUp(); | 
|  | 1839 | } | 
|  | 1840 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1841 | // For the cases of single pointer touch and two pointers non-split touch, the api's | 
|  | 1842 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ | 
|  | 1843 | // for the case where there are multiple pointers split across several windows. | 
|  | 1844 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, | 
|  | 1845 | ::testing::Values( | 
|  | 1846 | [&](sp<InputDispatcher> dispatcher, sp<IBinder> /*ignored*/, | 
|  | 1847 | sp<IBinder> destChannelToken) { | 
|  | 1848 | return dispatcher->transferTouch(destChannelToken); | 
|  | 1849 | }, | 
|  | 1850 | [&](sp<InputDispatcher> dispatcher, sp<IBinder> from, | 
|  | 1851 | sp<IBinder> to) { | 
|  | 1852 | return dispatcher->transferTouchFocus(from, to, | 
|  | 1853 | false /*isDragAndDrop*/); | 
|  | 1854 | })); | 
|  | 1855 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1856 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1857 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1858 |  | 
|  | 1859 | // Create a non touch modal window that supports split touch | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1860 | sp<FakeWindowHandle> firstWindow = | 
|  | 1861 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1862 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1863 | firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 1864 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1865 |  | 
|  | 1866 | // Create a non touch modal window that supports split touch | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1867 | sp<FakeWindowHandle> secondWindow = | 
|  | 1868 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1869 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1870 | secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 1871 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1872 |  | 
|  | 1873 | // Add the windows to the dispatcher | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1874 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1875 |  | 
|  | 1876 | PointF pointInFirst = {300, 200}; | 
|  | 1877 | PointF pointInSecond = {300, 600}; | 
|  | 1878 |  | 
|  | 1879 | // Send down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1880 | NotifyMotionArgs firstDownMotionArgs = | 
|  | 1881 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1882 | ADISPLAY_ID_DEFAULT, {pointInFirst}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1883 | mDispatcher->notifyMotion(&firstDownMotionArgs); | 
|  | 1884 | // Only the first window should get the down event | 
|  | 1885 | firstWindow->consumeMotionDown(); | 
|  | 1886 | secondWindow->assertNoEvents(); | 
|  | 1887 |  | 
|  | 1888 | // Send down to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1889 | NotifyMotionArgs secondDownMotionArgs = | 
|  | 1890 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1891 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1892 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1893 | {pointInFirst, pointInSecond}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1894 | mDispatcher->notifyMotion(&secondDownMotionArgs); | 
|  | 1895 | // The first window gets a move and the second a down | 
|  | 1896 | firstWindow->consumeMotionMove(); | 
|  | 1897 | secondWindow->consumeMotionDown(); | 
|  | 1898 |  | 
|  | 1899 | // Transfer touch focus to the second window | 
|  | 1900 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); | 
|  | 1901 | // The first window gets cancel and the new gets pointer down (it already saw down) | 
|  | 1902 | firstWindow->consumeMotionCancel(); | 
|  | 1903 | secondWindow->consumeMotionPointerDown(1); | 
|  | 1904 |  | 
|  | 1905 | // Send pointer up to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1906 | NotifyMotionArgs pointerUpMotionArgs = | 
|  | 1907 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1908 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1909 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1910 | {pointInFirst, pointInSecond}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1911 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 1912 | // The first window gets nothing and the second gets pointer up | 
|  | 1913 | firstWindow->assertNoEvents(); | 
|  | 1914 | secondWindow->consumeMotionPointerUp(1); | 
|  | 1915 |  | 
|  | 1916 | // Send up event to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1917 | NotifyMotionArgs upMotionArgs = | 
|  | 1918 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1919 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1920 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 1921 | // The first window gets nothing and the second gets up | 
|  | 1922 | firstWindow->assertNoEvents(); | 
|  | 1923 | secondWindow->consumeMotionUp(); | 
|  | 1924 | } | 
|  | 1925 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 1926 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. | 
|  | 1927 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving | 
|  | 1928 | // touch is not supported, so the touch should continue on those windows and the transferred-to | 
|  | 1929 | // window should get nothing. | 
|  | 1930 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { | 
|  | 1931 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 1932 |  | 
|  | 1933 | // Create a non touch modal window that supports split touch | 
|  | 1934 | sp<FakeWindowHandle> firstWindow = | 
|  | 1935 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
|  | 1936 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
|  | 1937 | firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 1938 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
|  | 1939 |  | 
|  | 1940 | // Create a non touch modal window that supports split touch | 
|  | 1941 | sp<FakeWindowHandle> secondWindow = | 
|  | 1942 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
|  | 1943 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
|  | 1944 | secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 1945 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
|  | 1946 |  | 
|  | 1947 | // Add the windows to the dispatcher | 
|  | 1948 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
|  | 1949 |  | 
|  | 1950 | PointF pointInFirst = {300, 200}; | 
|  | 1951 | PointF pointInSecond = {300, 600}; | 
|  | 1952 |  | 
|  | 1953 | // Send down to the first window | 
|  | 1954 | NotifyMotionArgs firstDownMotionArgs = | 
|  | 1955 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1956 | ADISPLAY_ID_DEFAULT, {pointInFirst}); | 
|  | 1957 | mDispatcher->notifyMotion(&firstDownMotionArgs); | 
|  | 1958 | // Only the first window should get the down event | 
|  | 1959 | firstWindow->consumeMotionDown(); | 
|  | 1960 | secondWindow->assertNoEvents(); | 
|  | 1961 |  | 
|  | 1962 | // Send down to the second window | 
|  | 1963 | NotifyMotionArgs secondDownMotionArgs = | 
|  | 1964 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1965 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1966 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1967 | {pointInFirst, pointInSecond}); | 
|  | 1968 | mDispatcher->notifyMotion(&secondDownMotionArgs); | 
|  | 1969 | // The first window gets a move and the second a down | 
|  | 1970 | firstWindow->consumeMotionMove(); | 
|  | 1971 | secondWindow->consumeMotionDown(); | 
|  | 1972 |  | 
|  | 1973 | // Transfer touch focus to the second window | 
|  | 1974 | const bool transferred = mDispatcher->transferTouch(secondWindow->getToken()); | 
|  | 1975 | // The 'transferTouch' call should not succeed, because there are 2 touched windows | 
|  | 1976 | ASSERT_FALSE(transferred); | 
|  | 1977 | firstWindow->assertNoEvents(); | 
|  | 1978 | secondWindow->assertNoEvents(); | 
|  | 1979 |  | 
|  | 1980 | // The rest of the dispatch should proceed as normal | 
|  | 1981 | // Send pointer up to the second window | 
|  | 1982 | NotifyMotionArgs pointerUpMotionArgs = | 
|  | 1983 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1984 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 1985 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1986 | {pointInFirst, pointInSecond}); | 
|  | 1987 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 1988 | // The first window gets MOVE and the second gets pointer up | 
|  | 1989 | firstWindow->consumeMotionMove(); | 
|  | 1990 | secondWindow->consumeMotionUp(); | 
|  | 1991 |  | 
|  | 1992 | // Send up event to the first window | 
|  | 1993 | NotifyMotionArgs upMotionArgs = | 
|  | 1994 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1995 | ADISPLAY_ID_DEFAULT); | 
|  | 1996 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 1997 | // The first window gets nothing and the second gets up | 
|  | 1998 | firstWindow->consumeMotionUp(); | 
|  | 1999 | secondWindow->assertNoEvents(); | 
|  | 2000 | } | 
|  | 2001 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2002 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2003 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2004 | sp<FakeWindowHandle> window = | 
|  | 2005 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2006 |  | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2007 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2008 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2009 | setFocusedWindow(window); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2010 |  | 
|  | 2011 | window->consumeFocusEvent(true); | 
|  | 2012 |  | 
|  | 2013 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 2014 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2015 |  | 
|  | 2016 | // Window should receive key down event. | 
|  | 2017 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 2018 | } | 
|  | 2019 |  | 
|  | 2020 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2021 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2022 | sp<FakeWindowHandle> window = | 
|  | 2023 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2024 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2025 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2026 |  | 
|  | 2027 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 2028 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2029 | mDispatcher->waitForIdle(); | 
|  | 2030 |  | 
|  | 2031 | window->assertNoEvents(); | 
|  | 2032 | } | 
|  | 2033 |  | 
|  | 2034 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys | 
|  | 2035 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2036 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2037 | sp<FakeWindowHandle> window = | 
|  | 2038 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2039 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2040 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2041 |  | 
|  | 2042 | // Send key | 
|  | 2043 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 2044 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2045 | // Send motion | 
|  | 2046 | NotifyMotionArgs motionArgs = | 
|  | 2047 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2048 | ADISPLAY_ID_DEFAULT); | 
|  | 2049 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2050 |  | 
|  | 2051 | // Window should receive only the motion event | 
|  | 2052 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2053 | window->assertNoEvents(); // Key event or focus event will not be received | 
|  | 2054 | } | 
|  | 2055 |  | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2056 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { | 
|  | 2057 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2058 |  | 
|  | 2059 | // Create first non touch modal window that supports split touch | 
|  | 2060 | sp<FakeWindowHandle> firstWindow = | 
|  | 2061 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); | 
|  | 2062 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
|  | 2063 | firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 2064 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
|  | 2065 |  | 
|  | 2066 | // Create second non touch modal window that supports split touch | 
|  | 2067 | sp<FakeWindowHandle> secondWindow = | 
|  | 2068 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); | 
|  | 2069 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
|  | 2070 | secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 2071 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
|  | 2072 |  | 
|  | 2073 | // Add the windows to the dispatcher | 
|  | 2074 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
|  | 2075 |  | 
|  | 2076 | PointF pointInFirst = {300, 200}; | 
|  | 2077 | PointF pointInSecond = {300, 600}; | 
|  | 2078 |  | 
|  | 2079 | // Send down to the first window | 
|  | 2080 | NotifyMotionArgs firstDownMotionArgs = | 
|  | 2081 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2082 | ADISPLAY_ID_DEFAULT, {pointInFirst}); | 
|  | 2083 | mDispatcher->notifyMotion(&firstDownMotionArgs); | 
|  | 2084 | // Only the first window should get the down event | 
|  | 2085 | firstWindow->consumeMotionDown(); | 
|  | 2086 | secondWindow->assertNoEvents(); | 
|  | 2087 |  | 
|  | 2088 | // Send down to the second window | 
|  | 2089 | NotifyMotionArgs secondDownMotionArgs = | 
|  | 2090 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 2091 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 2092 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 2093 | {pointInFirst, pointInSecond}); | 
|  | 2094 | mDispatcher->notifyMotion(&secondDownMotionArgs); | 
|  | 2095 | // The first window gets a move and the second a down | 
|  | 2096 | firstWindow->consumeMotionMove(); | 
|  | 2097 | secondWindow->consumeMotionDown(); | 
|  | 2098 |  | 
|  | 2099 | // Send pointer cancel to the second window | 
|  | 2100 | NotifyMotionArgs pointerUpMotionArgs = | 
|  | 2101 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 2102 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
|  | 2103 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 2104 | {pointInFirst, pointInSecond}); | 
|  | 2105 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; | 
|  | 2106 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 2107 | // The first window gets move and the second gets cancel. | 
|  | 2108 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); | 
|  | 2109 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); | 
|  | 2110 |  | 
|  | 2111 | // Send up event. | 
|  | 2112 | NotifyMotionArgs upMotionArgs = | 
|  | 2113 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2114 | ADISPLAY_ID_DEFAULT); | 
|  | 2115 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 2116 | // The first window gets up and the second gets nothing. | 
|  | 2117 | firstWindow->consumeMotionUp(); | 
|  | 2118 | secondWindow->assertNoEvents(); | 
|  | 2119 | } | 
|  | 2120 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 2121 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { | 
|  | 2122 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2123 |  | 
|  | 2124 | sp<FakeWindowHandle> window = | 
|  | 2125 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 2126 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2127 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; | 
|  | 2128 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; | 
|  | 2129 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; | 
|  | 2130 |  | 
|  | 2131 | window->sendTimeline(1 /*inputEventId*/, graphicsTimeline); | 
|  | 2132 | window->assertNoEvents(); | 
|  | 2133 | mDispatcher->waitForIdle(); | 
|  | 2134 | } | 
|  | 2135 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2136 | class FakeMonitorReceiver { | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2137 | public: | 
|  | 2138 | FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name, | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2139 | int32_t displayId, bool isGestureMonitor = false) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 2140 | base::Result<std::unique_ptr<InputChannel>> channel = | 
| Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 2141 | dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 2142 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2143 | } | 
|  | 2144 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2145 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } | 
|  | 2146 |  | 
|  | 2147 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 2148 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, | 
|  | 2149 | expectedDisplayId, expectedFlags); | 
|  | 2150 | } | 
|  | 2151 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2152 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } | 
|  | 2153 |  | 
|  | 2154 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } | 
|  | 2155 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2156 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 2157 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, | 
|  | 2158 | expectedDisplayId, expectedFlags); | 
|  | 2159 | } | 
|  | 2160 |  | 
|  | 2161 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 2162 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, | 
|  | 2163 | expectedDisplayId, expectedFlags); | 
|  | 2164 | } | 
|  | 2165 |  | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2166 | MotionEvent* consumeMotion() { | 
|  | 2167 | InputEvent* event = mInputReceiver->consume(); | 
|  | 2168 | if (!event) { | 
|  | 2169 | ADD_FAILURE() << "No event was produced"; | 
|  | 2170 | return nullptr; | 
|  | 2171 | } | 
|  | 2172 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { | 
|  | 2173 | ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion"; | 
|  | 2174 | return nullptr; | 
|  | 2175 | } | 
|  | 2176 | return static_cast<MotionEvent*>(event); | 
|  | 2177 | } | 
|  | 2178 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2179 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } | 
|  | 2180 |  | 
|  | 2181 | private: | 
|  | 2182 | std::unique_ptr<FakeInputReceiver> mInputReceiver; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2183 | }; | 
|  | 2184 |  | 
|  | 2185 | // Tests for gesture monitors | 
|  | 2186 | TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2187 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2188 | sp<FakeWindowHandle> window = | 
|  | 2189 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2190 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2191 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2192 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2193 | true /*isGestureMonitor*/); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2194 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2195 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2196 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2197 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2198 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2199 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2200 | } | 
|  | 2201 |  | 
|  | 2202 | TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2203 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2204 | sp<FakeWindowHandle> window = | 
|  | 2205 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2206 |  | 
|  | 2207 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2208 | window->setFocusable(true); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2209 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2210 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2211 | setFocusedWindow(window); | 
|  | 2212 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2213 | window->consumeFocusEvent(true); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2214 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2215 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2216 | true /*isGestureMonitor*/); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2217 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2218 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
|  | 2219 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2220 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2221 | monitor.assertNoEvents(); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2222 | } | 
|  | 2223 |  | 
|  | 2224 | TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2225 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2226 | sp<FakeWindowHandle> window = | 
|  | 2227 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2228 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2229 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2230 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2231 | true /*isGestureMonitor*/); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2232 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2233 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2234 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2235 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2236 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2237 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2238 |  | 
|  | 2239 | window->releaseChannel(); | 
|  | 2240 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2241 | mDispatcher->pilferPointers(monitor.getToken()); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2242 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2243 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2244 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2245 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2246 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2247 | } | 
|  | 2248 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2249 | TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) { | 
|  | 2250 | FakeMonitorReceiver monitor = | 
|  | 2251 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, | 
|  | 2252 | true /*isGestureMonitor*/); | 
|  | 2253 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2254 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2255 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); | 
|  | 2256 | std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); | 
|  | 2257 | ASSERT_TRUE(consumeSeq); | 
|  | 2258 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 2259 | mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2260 | monitor.finishEvent(*consumeSeq); | 
|  | 2261 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 2262 | mFakePolicy->assertNotifyMonitorResponsiveWasCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2263 | } | 
|  | 2264 |  | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2265 | // Tests for gesture monitors | 
|  | 2266 | TEST_F(InputDispatcherTest, GestureMonitor_NoWindowTransform) { | 
|  | 2267 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2268 | sp<FakeWindowHandle> window = | 
|  | 2269 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2270 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2271 | window->setWindowOffset(20, 40); | 
|  | 2272 | window->setWindowTransform(0, 1, -1, 0); | 
|  | 2273 |  | 
|  | 2274 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, | 
|  | 2275 | true /*isGestureMonitor*/); | 
|  | 2276 |  | 
|  | 2277 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2278 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 2279 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 2280 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2281 | MotionEvent* event = monitor.consumeMotion(); | 
|  | 2282 | // Even though window has transform, gesture monitor must not. | 
|  | 2283 | ASSERT_EQ(ui::Transform(), event->getTransform()); | 
|  | 2284 | } | 
|  | 2285 |  | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2286 | TEST_F(InputDispatcherTest, TestMoveEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2287 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2288 | sp<FakeWindowHandle> window = | 
|  | 2289 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2290 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2291 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2292 |  | 
|  | 2293 | NotifyMotionArgs motionArgs = | 
|  | 2294 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2295 | ADISPLAY_ID_DEFAULT); | 
|  | 2296 |  | 
|  | 2297 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2298 | // Window should receive motion down event. | 
|  | 2299 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2300 |  | 
|  | 2301 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2302 | motionArgs.id += 1; | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2303 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 2304 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, | 
|  | 2305 | motionArgs.pointerCoords[0].getX() - 10); | 
|  | 2306 |  | 
|  | 2307 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2308 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, | 
|  | 2309 | 0 /*expectedFlags*/); | 
|  | 2310 | } | 
|  | 2311 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2312 | /** | 
|  | 2313 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to | 
|  | 2314 | * the device default right away. In the test scenario, we check both the default value, | 
|  | 2315 | * and the action of enabling / disabling. | 
|  | 2316 | */ | 
|  | 2317 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2318 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2319 | sp<FakeWindowHandle> window = | 
|  | 2320 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 2321 |  | 
|  | 2322 | // Set focused application. | 
|  | 2323 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2324 | window->setFocusable(true); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2325 |  | 
|  | 2326 | SCOPED_TRACE("Check default value of touch mode"); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2327 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2328 | setFocusedWindow(window); | 
|  | 2329 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2330 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 2331 |  | 
|  | 2332 | SCOPED_TRACE("Remove the window to trigger focus loss"); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2333 | window->setFocusable(false); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2334 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2335 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); | 
|  | 2336 |  | 
|  | 2337 | SCOPED_TRACE("Disable touch mode"); | 
|  | 2338 | mDispatcher->setInTouchMode(false); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2339 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2340 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2341 | setFocusedWindow(window); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2342 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); | 
|  | 2343 |  | 
|  | 2344 | SCOPED_TRACE("Remove the window to trigger focus loss"); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2345 | window->setFocusable(false); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2346 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2347 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); | 
|  | 2348 |  | 
|  | 2349 | SCOPED_TRACE("Enable touch mode again"); | 
|  | 2350 | mDispatcher->setInTouchMode(true); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2351 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2352 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2353 | setFocusedWindow(window); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2354 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 2355 |  | 
|  | 2356 | window->assertNoEvents(); | 
|  | 2357 | } | 
|  | 2358 |  | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2359 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2360 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2361 | sp<FakeWindowHandle> window = | 
|  | 2362 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 2363 |  | 
|  | 2364 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2365 | window->setFocusable(true); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2366 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2367 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2368 | setFocusedWindow(window); | 
|  | 2369 |  | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2370 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 2371 |  | 
|  | 2372 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); | 
|  | 2373 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2374 |  | 
|  | 2375 | InputEvent* event = window->consume(); | 
|  | 2376 | ASSERT_NE(event, nullptr); | 
|  | 2377 |  | 
|  | 2378 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); | 
|  | 2379 | ASSERT_NE(verified, nullptr); | 
|  | 2380 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); | 
|  | 2381 |  | 
|  | 2382 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); | 
|  | 2383 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); | 
|  | 2384 | ASSERT_EQ(keyArgs.source, verified->source); | 
|  | 2385 | ASSERT_EQ(keyArgs.displayId, verified->displayId); | 
|  | 2386 |  | 
|  | 2387 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); | 
|  | 2388 |  | 
|  | 2389 | ASSERT_EQ(keyArgs.action, verifiedKey.action); | 
|  | 2390 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2391 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); | 
|  | 2392 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); | 
|  | 2393 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); | 
|  | 2394 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); | 
|  | 2395 | ASSERT_EQ(0, verifiedKey.repeatCount); | 
|  | 2396 | } | 
|  | 2397 |  | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2398 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2399 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2400 | sp<FakeWindowHandle> window = | 
|  | 2401 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 2402 |  | 
|  | 2403 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2404 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2405 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2406 |  | 
|  | 2407 | NotifyMotionArgs motionArgs = | 
|  | 2408 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2409 | ADISPLAY_ID_DEFAULT); | 
|  | 2410 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2411 |  | 
|  | 2412 | InputEvent* event = window->consume(); | 
|  | 2413 | ASSERT_NE(event, nullptr); | 
|  | 2414 |  | 
|  | 2415 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); | 
|  | 2416 | ASSERT_NE(verified, nullptr); | 
|  | 2417 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); | 
|  | 2418 |  | 
|  | 2419 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); | 
|  | 2420 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); | 
|  | 2421 | EXPECT_EQ(motionArgs.source, verified->source); | 
|  | 2422 | EXPECT_EQ(motionArgs.displayId, verified->displayId); | 
|  | 2423 |  | 
|  | 2424 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); | 
|  | 2425 |  | 
|  | 2426 | EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX); | 
|  | 2427 | EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY); | 
|  | 2428 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); | 
|  | 2429 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); | 
|  | 2430 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); | 
|  | 2431 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); | 
|  | 2432 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); | 
|  | 2433 | } | 
|  | 2434 |  | 
| Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 2435 | TEST_F(InputDispatcherTest, NonPointerMotionEvent_JoystickAndTouchpadNotTransformed) { | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2436 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2437 | sp<FakeWindowHandle> window = | 
|  | 2438 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); | 
|  | 2439 | const std::string name = window->getName(); | 
|  | 2440 |  | 
|  | 2441 | // Window gets transformed by offset values. | 
|  | 2442 | window->setWindowOffset(500.0f, 500.0f); | 
|  | 2443 |  | 
|  | 2444 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2445 | window->setFocusable(true); | 
|  | 2446 |  | 
|  | 2447 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2448 |  | 
|  | 2449 | // First, we set focused window so that focusedWindowHandle is not null. | 
|  | 2450 | setFocusedWindow(window); | 
|  | 2451 |  | 
|  | 2452 | // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked. | 
|  | 2453 | window->consumeFocusEvent(true); | 
|  | 2454 |  | 
| Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 2455 | constexpr const std::array nonTransformedSources = {std::pair(AINPUT_SOURCE_TOUCHPAD, | 
|  | 2456 | AMOTION_EVENT_ACTION_DOWN), | 
|  | 2457 | std::pair(AINPUT_SOURCE_JOYSTICK, | 
|  | 2458 | AMOTION_EVENT_ACTION_MOVE)}; | 
|  | 2459 | for (const auto& [source, action] : nonTransformedSources) { | 
|  | 2460 | const NotifyMotionArgs motionArgs = generateMotionArgs(action, source, ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2461 | mDispatcher->notifyMotion(&motionArgs); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2462 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 2463 | MotionEvent* event = window->consumeMotion(); | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2464 | ASSERT_NE(event, nullptr); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2465 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 2466 | const MotionEvent& motionEvent = *event; | 
| Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 2467 | EXPECT_EQ(action, motionEvent.getAction()); | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2468 | EXPECT_EQ(motionArgs.pointerCount, motionEvent.getPointerCount()); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2469 |  | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2470 | float expectedX = motionArgs.pointerCoords[0].getX(); | 
|  | 2471 | float expectedY = motionArgs.pointerCoords[0].getY(); | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2472 |  | 
| Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 2473 | // Ensure the axis values from the final motion event are not transformed. | 
|  | 2474 | EXPECT_EQ(expectedX, motionEvent.getX(0)) | 
|  | 2475 | << "expected " << expectedX << " for x coord of " << name.c_str() << ", got " | 
|  | 2476 | << motionEvent.getX(0); | 
|  | 2477 | EXPECT_EQ(expectedY, motionEvent.getY(0)) | 
|  | 2478 | << "expected " << expectedY << " for y coord of " << name.c_str() << ", got " | 
|  | 2479 | << motionEvent.getY(0); | 
|  | 2480 | // Ensure the raw and transformed axis values for the motion event are the same. | 
|  | 2481 | EXPECT_EQ(motionEvent.getRawX(0), motionEvent.getX(0)) | 
|  | 2482 | << "expected raw and transformed X-axis values to be equal"; | 
|  | 2483 | EXPECT_EQ(motionEvent.getRawY(0), motionEvent.getY(0)) | 
|  | 2484 | << "expected raw and transformed Y-axis values to be equal"; | 
|  | 2485 | } | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2486 | } | 
|  | 2487 |  | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 2488 | /** | 
|  | 2489 | * Ensure that separate calls to sign the same data are generating the same key. | 
|  | 2490 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance | 
|  | 2491 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky | 
|  | 2492 | * tests. | 
|  | 2493 | */ | 
|  | 2494 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { | 
|  | 2495 | KeyEvent event = getTestKeyEvent(); | 
|  | 2496 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); | 
|  | 2497 |  | 
|  | 2498 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); | 
|  | 2499 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); | 
|  | 2500 | ASSERT_EQ(hmac1, hmac2); | 
|  | 2501 | } | 
|  | 2502 |  | 
|  | 2503 | /** | 
|  | 2504 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. | 
|  | 2505 | */ | 
|  | 2506 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { | 
|  | 2507 | KeyEvent event = getTestKeyEvent(); | 
|  | 2508 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); | 
|  | 2509 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); | 
|  | 2510 |  | 
|  | 2511 | verifiedEvent.deviceId += 1; | 
|  | 2512 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2513 |  | 
|  | 2514 | verifiedEvent.source += 1; | 
|  | 2515 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2516 |  | 
|  | 2517 | verifiedEvent.eventTimeNanos += 1; | 
|  | 2518 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2519 |  | 
|  | 2520 | verifiedEvent.displayId += 1; | 
|  | 2521 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2522 |  | 
|  | 2523 | verifiedEvent.action += 1; | 
|  | 2524 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2525 |  | 
|  | 2526 | verifiedEvent.downTimeNanos += 1; | 
|  | 2527 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2528 |  | 
|  | 2529 | verifiedEvent.flags += 1; | 
|  | 2530 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2531 |  | 
|  | 2532 | verifiedEvent.keyCode += 1; | 
|  | 2533 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2534 |  | 
|  | 2535 | verifiedEvent.scanCode += 1; | 
|  | 2536 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2537 |  | 
|  | 2538 | verifiedEvent.metaState += 1; | 
|  | 2539 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2540 |  | 
|  | 2541 | verifiedEvent.repeatCount += 1; | 
|  | 2542 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 2543 | } | 
|  | 2544 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2545 | TEST_F(InputDispatcherTest, SetFocusedWindow) { | 
|  | 2546 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2547 | sp<FakeWindowHandle> windowTop = | 
|  | 2548 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 2549 | sp<FakeWindowHandle> windowSecond = | 
|  | 2550 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 2551 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2552 |  | 
|  | 2553 | // Top window is also focusable but is not granted focus. | 
|  | 2554 | windowTop->setFocusable(true); | 
|  | 2555 | windowSecond->setFocusable(true); | 
|  | 2556 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
|  | 2557 | setFocusedWindow(windowSecond); | 
|  | 2558 |  | 
|  | 2559 | windowSecond->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2560 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 2561 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2562 |  | 
|  | 2563 | // Focused window should receive event. | 
|  | 2564 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 2565 | windowTop->assertNoEvents(); | 
|  | 2566 | } | 
|  | 2567 |  | 
|  | 2568 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { | 
|  | 2569 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2570 | sp<FakeWindowHandle> window = | 
|  | 2571 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 2572 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2573 |  | 
|  | 2574 | window->setFocusable(true); | 
|  | 2575 | // Release channel for window is no longer valid. | 
|  | 2576 | window->releaseChannel(); | 
|  | 2577 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2578 | setFocusedWindow(window); | 
|  | 2579 |  | 
|  | 2580 | // Test inject a key down, should timeout. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2581 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 2582 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2583 |  | 
|  | 2584 | // window channel is invalid, so it should not receive any input event. | 
|  | 2585 | window->assertNoEvents(); | 
|  | 2586 | } | 
|  | 2587 |  | 
|  | 2588 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { | 
|  | 2589 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2590 | sp<FakeWindowHandle> window = | 
|  | 2591 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 2592 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2593 |  | 
|  | 2594 | // Window is not focusable. | 
|  | 2595 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2596 | setFocusedWindow(window); | 
|  | 2597 |  | 
|  | 2598 | // Test inject a key down, should timeout. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2599 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 2600 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2601 |  | 
|  | 2602 | // window is invalid, so it should not receive any input event. | 
|  | 2603 | window->assertNoEvents(); | 
|  | 2604 | } | 
|  | 2605 |  | 
|  | 2606 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { | 
|  | 2607 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2608 | sp<FakeWindowHandle> windowTop = | 
|  | 2609 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 2610 | sp<FakeWindowHandle> windowSecond = | 
|  | 2611 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 2612 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2613 |  | 
|  | 2614 | windowTop->setFocusable(true); | 
|  | 2615 | windowSecond->setFocusable(true); | 
|  | 2616 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
|  | 2617 | setFocusedWindow(windowTop); | 
|  | 2618 | windowTop->consumeFocusEvent(true); | 
|  | 2619 |  | 
|  | 2620 | setFocusedWindow(windowSecond, windowTop); | 
|  | 2621 | windowSecond->consumeFocusEvent(true); | 
|  | 2622 | windowTop->consumeFocusEvent(false); | 
|  | 2623 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2624 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 2625 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2626 |  | 
|  | 2627 | // Focused window should receive event. | 
|  | 2628 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 2629 | } | 
|  | 2630 |  | 
|  | 2631 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { | 
|  | 2632 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2633 | sp<FakeWindowHandle> windowTop = | 
|  | 2634 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 2635 | sp<FakeWindowHandle> windowSecond = | 
|  | 2636 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 2637 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2638 |  | 
|  | 2639 | windowTop->setFocusable(true); | 
|  | 2640 | windowSecond->setFocusable(true); | 
|  | 2641 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
|  | 2642 | setFocusedWindow(windowSecond, windowTop); | 
|  | 2643 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2644 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 2645 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2646 |  | 
|  | 2647 | // Event should be dropped. | 
|  | 2648 | windowTop->assertNoEvents(); | 
|  | 2649 | windowSecond->assertNoEvents(); | 
|  | 2650 | } | 
|  | 2651 |  | 
|  | 2652 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { | 
|  | 2653 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2654 | sp<FakeWindowHandle> window = | 
|  | 2655 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 2656 | sp<FakeWindowHandle> previousFocusedWindow = | 
|  | 2657 | new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow", | 
|  | 2658 | ADISPLAY_ID_DEFAULT); | 
|  | 2659 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2660 |  | 
|  | 2661 | window->setFocusable(true); | 
|  | 2662 | previousFocusedWindow->setFocusable(true); | 
|  | 2663 | window->setVisible(false); | 
|  | 2664 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); | 
|  | 2665 | setFocusedWindow(previousFocusedWindow); | 
|  | 2666 | previousFocusedWindow->consumeFocusEvent(true); | 
|  | 2667 |  | 
|  | 2668 | // Requesting focus on invisible window takes focus from currently focused window. | 
|  | 2669 | setFocusedWindow(window); | 
|  | 2670 | previousFocusedWindow->consumeFocusEvent(false); | 
|  | 2671 |  | 
|  | 2672 | // Injected key goes to pending queue. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2673 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2674 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2675 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2676 |  | 
|  | 2677 | // Window does not get focus event or key down. | 
|  | 2678 | window->assertNoEvents(); | 
|  | 2679 |  | 
|  | 2680 | // Window becomes visible. | 
|  | 2681 | window->setVisible(true); | 
|  | 2682 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2683 |  | 
|  | 2684 | // Window receives focus event. | 
|  | 2685 | window->consumeFocusEvent(true); | 
|  | 2686 | // Focused window receives key down. | 
|  | 2687 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 2688 | } | 
|  | 2689 |  | 
| Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 2690 | TEST_F(InputDispatcherTest, DisplayRemoved) { | 
|  | 2691 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2692 | sp<FakeWindowHandle> window = | 
|  | 2693 | new FakeWindowHandle(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); | 
|  | 2694 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2695 |  | 
|  | 2696 | // window is granted focus. | 
|  | 2697 | window->setFocusable(true); | 
|  | 2698 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2699 | setFocusedWindow(window); | 
|  | 2700 | window->consumeFocusEvent(true); | 
|  | 2701 |  | 
|  | 2702 | // When a display is removed window loses focus. | 
|  | 2703 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); | 
|  | 2704 | window->consumeFocusEvent(false); | 
|  | 2705 | } | 
|  | 2706 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2707 | /** | 
|  | 2708 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, | 
|  | 2709 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top | 
|  | 2710 | * of the 'slipperyEnterWindow'. | 
|  | 2711 | * | 
|  | 2712 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such | 
|  | 2713 | * a way so that the touched location is no longer covered by the top window. | 
|  | 2714 | * | 
|  | 2715 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now | 
|  | 2716 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had | 
|  | 2717 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive | 
|  | 2718 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting | 
|  | 2719 | * with ACTION_DOWN). | 
|  | 2720 | * Thus, the touch has been transferred from the top window into the bottom window, because the top | 
|  | 2721 | * window moved itself away from the touched location and had Flag::SLIPPERY. | 
|  | 2722 | * | 
|  | 2723 | * Even though the top window moved away from the touched location, it is still obscuring the bottom | 
|  | 2724 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ | 
|  | 2725 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. | 
|  | 2726 | * | 
|  | 2727 | * In this test, we ensure that the event received by the bottom window has | 
|  | 2728 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. | 
|  | 2729 | */ | 
|  | 2730 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { | 
|  | 2731 | constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1; | 
|  | 2732 | constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1; | 
|  | 2733 |  | 
|  | 2734 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2735 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2736 |  | 
|  | 2737 | sp<FakeWindowHandle> slipperyExitWindow = | 
|  | 2738 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 2739 | slipperyExitWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 2740 | InputWindowInfo::Flag::SLIPPERY); | 
|  | 2741 | // Make sure this one overlaps the bottom window | 
|  | 2742 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); | 
|  | 2743 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom | 
|  | 2744 | // one. Windows with the same owner are not considered to be occluding each other. | 
|  | 2745 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); | 
|  | 2746 |  | 
|  | 2747 | sp<FakeWindowHandle> slipperyEnterWindow = | 
|  | 2748 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 2749 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 2750 |  | 
|  | 2751 | mDispatcher->setInputWindows( | 
|  | 2752 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); | 
|  | 2753 |  | 
|  | 2754 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions | 
|  | 2755 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2756 | ADISPLAY_ID_DEFAULT, {{50, 50}}); | 
|  | 2757 | mDispatcher->notifyMotion(&args); | 
|  | 2758 | slipperyExitWindow->consumeMotionDown(); | 
|  | 2759 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); | 
|  | 2760 | mDispatcher->setInputWindows( | 
|  | 2761 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); | 
|  | 2762 |  | 
|  | 2763 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2764 | ADISPLAY_ID_DEFAULT, {{51, 51}}); | 
|  | 2765 | mDispatcher->notifyMotion(&args); | 
|  | 2766 |  | 
|  | 2767 | slipperyExitWindow->consumeMotionCancel(); | 
|  | 2768 |  | 
|  | 2769 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, | 
|  | 2770 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); | 
|  | 2771 | } | 
|  | 2772 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2773 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { | 
|  | 2774 | protected: | 
|  | 2775 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms | 
|  | 2776 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000;   // 40 ms | 
|  | 2777 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2778 | std::shared_ptr<FakeApplicationHandle> mApp; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2779 | sp<FakeWindowHandle> mWindow; | 
|  | 2780 |  | 
|  | 2781 | virtual void SetUp() override { | 
|  | 2782 | mFakePolicy = new FakeInputDispatcherPolicy(); | 
|  | 2783 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); | 
|  | 2784 | mDispatcher = new InputDispatcher(mFakePolicy); | 
|  | 2785 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); | 
|  | 2786 | ASSERT_EQ(OK, mDispatcher->start()); | 
|  | 2787 |  | 
|  | 2788 | setUpWindow(); | 
|  | 2789 | } | 
|  | 2790 |  | 
|  | 2791 | void setUpWindow() { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2792 | mApp = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2793 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2794 |  | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2795 | mWindow->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2796 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2797 | setFocusedWindow(mWindow); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2798 | mWindow->consumeFocusEvent(true); | 
|  | 2799 | } | 
|  | 2800 |  | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2801 | void sendAndConsumeKeyDown(int32_t deviceId) { | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2802 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2803 | keyArgs.deviceId = deviceId; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2804 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event | 
|  | 2805 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2806 |  | 
|  | 2807 | // Window should receive key down event. | 
|  | 2808 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 2809 | } | 
|  | 2810 |  | 
|  | 2811 | void expectKeyRepeatOnce(int32_t repeatCount) { | 
|  | 2812 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); | 
|  | 2813 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 2814 | ASSERT_NE(nullptr, repeatEvent); | 
|  | 2815 |  | 
|  | 2816 | uint32_t eventType = repeatEvent->getType(); | 
|  | 2817 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); | 
|  | 2818 |  | 
|  | 2819 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); | 
|  | 2820 | uint32_t eventAction = repeatKeyEvent->getAction(); | 
|  | 2821 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); | 
|  | 2822 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); | 
|  | 2823 | } | 
|  | 2824 |  | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2825 | void sendAndConsumeKeyUp(int32_t deviceId) { | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2826 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2827 | keyArgs.deviceId = deviceId; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2828 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event | 
|  | 2829 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2830 |  | 
|  | 2831 | // Window should receive key down event. | 
|  | 2832 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, | 
|  | 2833 | 0 /*expectedFlags*/); | 
|  | 2834 | } | 
|  | 2835 | }; | 
|  | 2836 |  | 
|  | 2837 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2838 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 2839 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 2840 | expectKeyRepeatOnce(repeatCount); | 
|  | 2841 | } | 
|  | 2842 | } | 
|  | 2843 |  | 
|  | 2844 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { | 
|  | 2845 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 2846 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 2847 | expectKeyRepeatOnce(repeatCount); | 
|  | 2848 | } | 
|  | 2849 | sendAndConsumeKeyDown(2 /* deviceId */); | 
|  | 2850 | /* repeatCount will start from 1 for deviceId 2 */ | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2851 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 2852 | expectKeyRepeatOnce(repeatCount); | 
|  | 2853 | } | 
|  | 2854 | } | 
|  | 2855 |  | 
|  | 2856 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2857 | sendAndConsumeKeyDown(1 /* deviceId */); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2858 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2859 | sendAndConsumeKeyUp(1 /* deviceId */); | 
|  | 2860 | mWindow->assertNoEvents(); | 
|  | 2861 | } | 
|  | 2862 |  | 
|  | 2863 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { | 
|  | 2864 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 2865 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 2866 | sendAndConsumeKeyDown(2 /* deviceId */); | 
|  | 2867 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 2868 | // Stale key up from device 1. | 
|  | 2869 | sendAndConsumeKeyUp(1 /* deviceId */); | 
|  | 2870 | // Device 2 is still down, keep repeating | 
|  | 2871 | expectKeyRepeatOnce(2 /*repeatCount*/); | 
|  | 2872 | expectKeyRepeatOnce(3 /*repeatCount*/); | 
|  | 2873 | // Device 2 key up | 
|  | 2874 | sendAndConsumeKeyUp(2 /* deviceId */); | 
|  | 2875 | mWindow->assertNoEvents(); | 
|  | 2876 | } | 
|  | 2877 |  | 
|  | 2878 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { | 
|  | 2879 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 2880 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 2881 | sendAndConsumeKeyDown(2 /* deviceId */); | 
|  | 2882 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 2883 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. | 
|  | 2884 | sendAndConsumeKeyUp(2 /* deviceId */); | 
|  | 2885 | // Device 1 still holds key down, but the repeating was already stopped | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2886 | mWindow->assertNoEvents(); | 
|  | 2887 | } | 
|  | 2888 |  | 
| liushenxiang | 97b0d3b | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 2889 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { | 
|  | 2890 | sendAndConsumeKeyDown(DEVICE_ID); | 
|  | 2891 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 2892 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); | 
|  | 2893 | mDispatcher->notifyDeviceReset(&args); | 
|  | 2894 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, | 
|  | 2895 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); | 
|  | 2896 | mWindow->assertNoEvents(); | 
|  | 2897 | } | 
|  | 2898 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2899 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2900 | sendAndConsumeKeyDown(1 /* deviceId */); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2901 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 2902 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 2903 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; | 
|  | 2904 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, | 
|  | 2905 | IdGenerator::getSource(repeatEvent->getId())); | 
|  | 2906 | } | 
|  | 2907 | } | 
|  | 2908 |  | 
|  | 2909 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2910 | sendAndConsumeKeyDown(1 /* deviceId */); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2911 |  | 
|  | 2912 | std::unordered_set<int32_t> idSet; | 
|  | 2913 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 2914 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 2915 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; | 
|  | 2916 | int32_t id = repeatEvent->getId(); | 
|  | 2917 | EXPECT_EQ(idSet.end(), idSet.find(id)); | 
|  | 2918 | idSet.insert(id); | 
|  | 2919 | } | 
|  | 2920 | } | 
|  | 2921 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2922 | /* Test InputDispatcher for MultiDisplay */ | 
|  | 2923 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { | 
|  | 2924 | public: | 
|  | 2925 | static constexpr int32_t SECOND_DISPLAY_ID = 1; | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2926 | virtual void SetUp() override { | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2927 | InputDispatcherTest::SetUp(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2928 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2929 | application1 = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2930 | windowInPrimary = | 
|  | 2931 | new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2932 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2933 | // Set focus window for primary display, but focused display would be second one. | 
|  | 2934 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2935 | windowInPrimary->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2936 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2937 | setFocusedWindow(windowInPrimary); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2938 | windowInPrimary->consumeFocusEvent(true); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2939 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2940 | application2 = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2941 | windowInSecondary = | 
|  | 2942 | new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2943 | // Set focus to second display window. | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2944 | // Set focus display to second one. | 
|  | 2945 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); | 
|  | 2946 | // Set focus window for second display. | 
|  | 2947 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2948 | windowInSecondary->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2949 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2950 | setFocusedWindow(windowInSecondary); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2951 | windowInSecondary->consumeFocusEvent(true); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2952 | } | 
|  | 2953 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2954 | virtual void TearDown() override { | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2955 | InputDispatcherTest::TearDown(); | 
|  | 2956 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2957 | application1.reset(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2958 | windowInPrimary.clear(); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2959 | application2.reset(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2960 | windowInSecondary.clear(); | 
|  | 2961 | } | 
|  | 2962 |  | 
|  | 2963 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2964 | std::shared_ptr<FakeApplicationHandle> application1; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2965 | sp<FakeWindowHandle> windowInPrimary; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2966 | std::shared_ptr<FakeApplicationHandle> application2; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2967 | sp<FakeWindowHandle> windowInSecondary; | 
|  | 2968 | }; | 
|  | 2969 |  | 
|  | 2970 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { | 
|  | 2971 | // Test touch down on primary display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2972 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2973 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 2974 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2975 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2976 | windowInSecondary->assertNoEvents(); | 
|  | 2977 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2978 | // Test touch down on second display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2979 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2980 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) | 
|  | 2981 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2982 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2983 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2984 | } | 
|  | 2985 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2986 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2987 | // Test inject a key down with display id specified. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 2988 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2989 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2990 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2991 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2992 | windowInSecondary->assertNoEvents(); | 
|  | 2993 |  | 
|  | 2994 | // Test inject a key down without display id specified. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 2995 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2996 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2997 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2998 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2999 |  | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3000 | // Remove all windows in secondary display. | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3001 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3002 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3003 | // Old focus should receive a cancel event. | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3004 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, | 
|  | 3005 | AKEY_EVENT_FLAG_CANCELED); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3006 |  | 
|  | 3007 | // Test inject a key down, should timeout because of no target window. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3008 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3009 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3010 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3011 | windowInSecondary->consumeFocusEvent(false); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3012 | windowInSecondary->assertNoEvents(); | 
|  | 3013 | } | 
|  | 3014 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3015 | // Test per-display input monitors for motion event. | 
|  | 3016 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3017 | FakeMonitorReceiver monitorInPrimary = | 
|  | 3018 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
|  | 3019 | FakeMonitorReceiver monitorInSecondary = | 
|  | 3020 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3021 |  | 
|  | 3022 | // Test touch down on primary display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3023 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3024 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 3025 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3026 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3027 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3028 | windowInSecondary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3029 | monitorInSecondary.assertNoEvents(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3030 |  | 
|  | 3031 | // Test touch down on second display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3032 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3033 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) | 
|  | 3034 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3035 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3036 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3037 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3038 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3039 |  | 
|  | 3040 | // Test inject a non-pointer motion event. | 
|  | 3041 | // If specific a display, it will dispatch to the focused window of particular display, | 
|  | 3042 | // or it will dispatch to the focused window of focused display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3043 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3044 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) | 
|  | 3045 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3046 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3047 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3048 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3049 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3050 | } | 
|  | 3051 |  | 
|  | 3052 | // Test per-display input monitors for key event. | 
|  | 3053 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3054 | // Input monitor per display. | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3055 | FakeMonitorReceiver monitorInPrimary = | 
|  | 3056 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
|  | 3057 | FakeMonitorReceiver monitorInSecondary = | 
|  | 3058 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3059 |  | 
|  | 3060 | // Test inject a key down. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3061 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 3062 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3063 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3064 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3065 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3066 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3067 | } | 
|  | 3068 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3069 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { | 
|  | 3070 | sp<FakeWindowHandle> secondWindowInPrimary = | 
|  | 3071 | new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); | 
|  | 3072 | secondWindowInPrimary->setFocusable(true); | 
|  | 3073 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); | 
|  | 3074 | setFocusedWindow(secondWindowInPrimary); | 
|  | 3075 | windowInPrimary->consumeFocusEvent(false); | 
|  | 3076 | secondWindowInPrimary->consumeFocusEvent(true); | 
|  | 3077 |  | 
|  | 3078 | // Test inject a key down. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3079 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
|  | 3080 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3081 | windowInPrimary->assertNoEvents(); | 
|  | 3082 | windowInSecondary->assertNoEvents(); | 
|  | 3083 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 3084 | } | 
|  | 3085 |  | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3086 | class InputFilterTest : public InputDispatcherTest { | 
|  | 3087 | protected: | 
|  | 3088 | static constexpr int32_t SECOND_DISPLAY_ID = 1; | 
|  | 3089 |  | 
|  | 3090 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) { | 
|  | 3091 | NotifyMotionArgs motionArgs; | 
|  | 3092 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3093 | motionArgs = | 
|  | 3094 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3095 | mDispatcher->notifyMotion(&motionArgs); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3096 | motionArgs = | 
|  | 3097 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3098 | mDispatcher->notifyMotion(&motionArgs); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3099 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3100 | if (expectToBeFiltered) { | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 3101 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3102 | } else { | 
|  | 3103 | mFakePolicy->assertFilterInputEventWasNotCalled(); | 
|  | 3104 | } | 
|  | 3105 | } | 
|  | 3106 |  | 
|  | 3107 | void testNotifyKey(bool expectToBeFiltered) { | 
|  | 3108 | NotifyKeyArgs keyArgs; | 
|  | 3109 |  | 
|  | 3110 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); | 
|  | 3111 | mDispatcher->notifyKey(&keyArgs); | 
|  | 3112 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); | 
|  | 3113 | mDispatcher->notifyKey(&keyArgs); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3114 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3115 |  | 
|  | 3116 | if (expectToBeFiltered) { | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 3117 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3118 | } else { | 
|  | 3119 | mFakePolicy->assertFilterInputEventWasNotCalled(); | 
|  | 3120 | } | 
|  | 3121 | } | 
|  | 3122 | }; | 
|  | 3123 |  | 
|  | 3124 | // Test InputFilter for MotionEvent | 
|  | 3125 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { | 
|  | 3126 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. | 
|  | 3127 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); | 
|  | 3128 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); | 
|  | 3129 |  | 
|  | 3130 | // Enable InputFilter | 
|  | 3131 | mDispatcher->setInputFilterEnabled(true); | 
|  | 3132 | // Test touch on both primary and second display, and check if both events are filtered. | 
|  | 3133 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); | 
|  | 3134 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); | 
|  | 3135 |  | 
|  | 3136 | // Disable InputFilter | 
|  | 3137 | mDispatcher->setInputFilterEnabled(false); | 
|  | 3138 | // Test touch on both primary and second display, and check if both events aren't filtered. | 
|  | 3139 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); | 
|  | 3140 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); | 
|  | 3141 | } | 
|  | 3142 |  | 
|  | 3143 | // Test InputFilter for KeyEvent | 
|  | 3144 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { | 
|  | 3145 | // Since the InputFilter is disabled by default, check if key event aren't filtered. | 
|  | 3146 | testNotifyKey(/*expectToBeFiltered*/ false); | 
|  | 3147 |  | 
|  | 3148 | // Enable InputFilter | 
|  | 3149 | mDispatcher->setInputFilterEnabled(true); | 
|  | 3150 | // Send a key event, and check if it is filtered. | 
|  | 3151 | testNotifyKey(/*expectToBeFiltered*/ true); | 
|  | 3152 |  | 
|  | 3153 | // Disable InputFilter | 
|  | 3154 | mDispatcher->setInputFilterEnabled(false); | 
|  | 3155 | // Send a key event, and check if it isn't filtered. | 
|  | 3156 | testNotifyKey(/*expectToBeFiltered*/ false); | 
|  | 3157 | } | 
|  | 3158 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3159 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { | 
|  | 3160 | protected: | 
|  | 3161 | virtual void SetUp() override { | 
|  | 3162 | InputDispatcherTest::SetUp(); | 
|  | 3163 |  | 
|  | 3164 | /** | 
|  | 3165 | * We don't need to enable input filter to test the injected event policy, but we enabled it | 
|  | 3166 | * here to make the tests more realistic, since this policy only matters when inputfilter is | 
|  | 3167 | * on. | 
|  | 3168 | */ | 
|  | 3169 | mDispatcher->setInputFilterEnabled(true); | 
|  | 3170 |  | 
|  | 3171 | std::shared_ptr<InputApplicationHandle> application = | 
|  | 3172 | std::make_shared<FakeApplicationHandle>(); | 
|  | 3173 | mWindow = | 
|  | 3174 | new FakeWindowHandle(application, mDispatcher, "Test Window", ADISPLAY_ID_DEFAULT); | 
|  | 3175 |  | 
|  | 3176 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3177 | mWindow->setFocusable(true); | 
|  | 3178 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3179 | setFocusedWindow(mWindow); | 
|  | 3180 | mWindow->consumeFocusEvent(true); | 
|  | 3181 | } | 
|  | 3182 |  | 
|  | 3183 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId) { | 
|  | 3184 | KeyEvent event; | 
|  | 3185 |  | 
|  | 3186 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 3187 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, | 
|  | 3188 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, | 
|  | 3189 | KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime); | 
|  | 3190 | const int32_t additionalPolicyFlags = | 
|  | 3191 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; | 
|  | 3192 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3193 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
|  | 3194 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, | 
|  | 3195 | policyFlags | additionalPolicyFlags)); | 
|  | 3196 |  | 
|  | 3197 | InputEvent* received = mWindow->consume(); | 
|  | 3198 | ASSERT_NE(nullptr, received); | 
|  | 3199 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); | 
|  | 3200 | } | 
|  | 3201 |  | 
|  | 3202 | private: | 
|  | 3203 | sp<FakeWindowHandle> mWindow; | 
|  | 3204 | }; | 
|  | 3205 |  | 
|  | 3206 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { | 
|  | 3207 | // We don't need POLICY_FLAG_FILTERED here, but it will be set in practice, so keep it to make | 
|  | 3208 | // the test more closely resemble the real usage | 
|  | 3209 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INPUTFILTER_TRUSTED, 3 /*injectedDeviceId*/, | 
|  | 3210 | 3 /*resolvedDeviceId*/); | 
|  | 3211 | } | 
|  | 3212 |  | 
|  | 3213 | TEST_F(InputFilterInjectionPolicyTest, EventsInjectedFromAccessibility_HaveAccessibilityDeviceId) { | 
|  | 3214 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, | 
|  | 3215 | 3 /*injectedDeviceId*/, ACCESSIBILITY_DEVICE_ID /*resolvedDeviceId*/); | 
|  | 3216 | } | 
|  | 3217 |  | 
|  | 3218 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { | 
|  | 3219 | testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/, | 
|  | 3220 | VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/); | 
|  | 3221 | } | 
|  | 3222 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3223 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3224 | virtual void SetUp() override { | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3225 | InputDispatcherTest::SetUp(); | 
|  | 3226 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3227 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 3228 | std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3229 | mUnfocusedWindow = | 
|  | 3230 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3231 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); | 
|  | 3232 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this | 
|  | 3233 | // window. | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3234 | mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3235 |  | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3236 | mFocusedWindow = | 
|  | 3237 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
|  | 3238 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3239 | mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3240 |  | 
|  | 3241 | // Set focused application. | 
|  | 3242 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3243 | mFocusedWindow->setFocusable(true); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3244 |  | 
|  | 3245 | // Expect one focus window exist in display. | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3246 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3247 | setFocusedWindow(mFocusedWindow); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3248 | mFocusedWindow->consumeFocusEvent(true); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3249 | } | 
|  | 3250 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3251 | virtual void TearDown() override { | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3252 | InputDispatcherTest::TearDown(); | 
|  | 3253 |  | 
|  | 3254 | mUnfocusedWindow.clear(); | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3255 | mFocusedWindow.clear(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3256 | } | 
|  | 3257 |  | 
|  | 3258 | protected: | 
|  | 3259 | sp<FakeWindowHandle> mUnfocusedWindow; | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3260 | sp<FakeWindowHandle> mFocusedWindow; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3261 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3262 | }; | 
|  | 3263 |  | 
|  | 3264 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action | 
|  | 3265 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received | 
|  | 3266 | // the onPointerDownOutsideFocus callback. | 
|  | 3267 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3268 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3269 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3270 | {20, 20})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3271 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3272 | mUnfocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3273 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3274 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3275 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); | 
|  | 3276 | } | 
|  | 3277 |  | 
|  | 3278 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action | 
|  | 3279 | // DOWN on the window that doesn't have focus. Ensure no window received the | 
|  | 3280 | // onPointerDownOutsideFocus callback. | 
|  | 3281 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3282 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3283 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3284 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3285 | mFocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3286 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3287 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3288 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3289 | } | 
|  | 3290 |  | 
|  | 3291 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't | 
|  | 3292 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. | 
|  | 3293 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3294 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3295 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3296 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3297 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3298 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3299 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3300 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3301 | } | 
|  | 3302 |  | 
|  | 3303 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action | 
|  | 3304 | // DOWN on the window that already has focus. Ensure no window received the | 
|  | 3305 | // onPointerDownOutsideFocus callback. | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3306 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3307 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3308 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3309 | FOCUSED_WINDOW_TOUCH_POINT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3310 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3311 | mFocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3312 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3313 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3314 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3315 | } | 
|  | 3316 |  | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3317 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag | 
|  | 3318 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. | 
|  | 3319 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { | 
|  | 3320 | const MotionEvent event = | 
|  | 3321 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 3322 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 3323 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20)) | 
|  | 3324 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) | 
|  | 3325 | .build(); | 
|  | 3326 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) | 
|  | 3327 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 3328 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); | 
|  | 3329 |  | 
|  | 3330 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3331 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
|  | 3332 | // Ensure that the unfocused window did not receive any FOCUS events. | 
|  | 3333 | mUnfocusedWindow->assertNoEvents(); | 
|  | 3334 | } | 
|  | 3335 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3336 | // These tests ensures we can send touch events to a single client when there are multiple input | 
|  | 3337 | // windows that point to the same client token. | 
|  | 3338 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { | 
|  | 3339 | virtual void SetUp() override { | 
|  | 3340 | InputDispatcherTest::SetUp(); | 
|  | 3341 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3342 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 3343 | std::make_shared<FakeApplicationHandle>(); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3344 | mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1", | 
|  | 3345 | ADISPLAY_ID_DEFAULT); | 
|  | 3346 | // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window. | 
|  | 3347 | // 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] | 3348 | mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 3349 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3350 | mWindow1->setFrame(Rect(0, 0, 100, 100)); | 
|  | 3351 |  | 
|  | 3352 | mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2", | 
|  | 3353 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3354 | mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 3355 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3356 | mWindow2->setFrame(Rect(100, 100, 200, 200)); | 
|  | 3357 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3358 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3359 | } | 
|  | 3360 |  | 
|  | 3361 | protected: | 
|  | 3362 | sp<FakeWindowHandle> mWindow1; | 
|  | 3363 | sp<FakeWindowHandle> mWindow2; | 
|  | 3364 |  | 
|  | 3365 | // Helper function to convert the point from screen coordinates into the window's space | 
|  | 3366 | static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) { | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3367 | vec2 vals = windowInfo->transform.transform(point.x, point.y); | 
|  | 3368 | return {vals.x, vals.y}; | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3369 | } | 
|  | 3370 |  | 
|  | 3371 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, | 
|  | 3372 | const std::vector<PointF>& points) { | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3373 | const std::string name = window->getName(); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3374 | InputEvent* event = window->consume(); | 
|  | 3375 |  | 
|  | 3376 | ASSERT_NE(nullptr, event) << name.c_str() | 
|  | 3377 | << ": consumer should have returned non-NULL event."; | 
|  | 3378 |  | 
|  | 3379 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) | 
|  | 3380 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) | 
|  | 3381 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; | 
|  | 3382 |  | 
|  | 3383 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); | 
|  | 3384 | EXPECT_EQ(expectedAction, motionEvent.getAction()); | 
|  | 3385 |  | 
|  | 3386 | for (size_t i = 0; i < points.size(); i++) { | 
|  | 3387 | float expectedX = points[i].x; | 
|  | 3388 | float expectedY = points[i].y; | 
|  | 3389 |  | 
|  | 3390 | EXPECT_EQ(expectedX, motionEvent.getX(i)) | 
|  | 3391 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() | 
|  | 3392 | << ", got " << motionEvent.getX(i); | 
|  | 3393 | EXPECT_EQ(expectedY, motionEvent.getY(i)) | 
|  | 3394 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() | 
|  | 3395 | << ", got " << motionEvent.getY(i); | 
|  | 3396 | } | 
|  | 3397 | } | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3398 |  | 
|  | 3399 | void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints, | 
|  | 3400 | std::vector<PointF> expectedPoints) { | 
|  | 3401 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3402 | ADISPLAY_ID_DEFAULT, touchedPoints); | 
|  | 3403 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 3404 |  | 
|  | 3405 | // Always consume from window1 since it's the window that has the InputReceiver | 
|  | 3406 | consumeMotionEvent(mWindow1, action, expectedPoints); | 
|  | 3407 | } | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3408 | }; | 
|  | 3409 |  | 
|  | 3410 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { | 
|  | 3411 | // Touch Window 1 | 
|  | 3412 | PointF touchedPoint = {10, 10}; | 
|  | 3413 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3414 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3415 |  | 
|  | 3416 | // Release touch on Window 1 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3417 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3418 |  | 
|  | 3419 | // Touch Window 2 | 
|  | 3420 | touchedPoint = {150, 150}; | 
|  | 3421 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3422 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3423 | } | 
|  | 3424 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3425 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { | 
|  | 3426 | // Set scale value for window2 | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3427 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 3428 |  | 
|  | 3429 | // Touch Window 1 | 
|  | 3430 | PointF touchedPoint = {10, 10}; | 
|  | 3431 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3432 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3433 | // Release touch on Window 1 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3434 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3435 |  | 
|  | 3436 | // Touch Window 2 | 
|  | 3437 | touchedPoint = {150, 150}; | 
|  | 3438 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3439 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
|  | 3440 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3441 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3442 | // Update the transform so rotation is set | 
|  | 3443 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 3444 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
|  | 3445 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3446 | } | 
|  | 3447 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3448 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3449 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 3450 |  | 
|  | 3451 | // Touch Window 1 | 
|  | 3452 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 3453 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3454 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3455 |  | 
|  | 3456 | // Touch Window 2 | 
|  | 3457 | int32_t actionPointerDown = | 
|  | 3458 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3459 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 3460 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
|  | 3461 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3462 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3463 | // Release Window 2 | 
|  | 3464 | int32_t actionPointerUp = | 
|  | 3465 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
|  | 3466 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); | 
|  | 3467 | expectedPoints.pop_back(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3468 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3469 | // Update the transform so rotation is set for Window 2 | 
|  | 3470 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 3471 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
|  | 3472 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3473 | } | 
|  | 3474 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3475 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3476 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 3477 |  | 
|  | 3478 | // Touch Window 1 | 
|  | 3479 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 3480 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3481 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3482 |  | 
|  | 3483 | // Touch Window 2 | 
|  | 3484 | int32_t actionPointerDown = | 
|  | 3485 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3486 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 3487 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3488 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3489 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3490 |  | 
|  | 3491 | // Move both windows | 
|  | 3492 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 3493 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 3494 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 3495 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3496 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3497 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3498 | // Release Window 2 | 
|  | 3499 | int32_t actionPointerUp = | 
|  | 3500 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
|  | 3501 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); | 
|  | 3502 | expectedPoints.pop_back(); | 
|  | 3503 |  | 
|  | 3504 | // Touch Window 2 | 
|  | 3505 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 3506 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
|  | 3507 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
|  | 3508 |  | 
|  | 3509 | // Move both windows | 
|  | 3510 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 3511 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 3512 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 3513 |  | 
|  | 3514 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3515 | } | 
|  | 3516 |  | 
|  | 3517 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { | 
|  | 3518 | mWindow1->setWindowScale(0.5f, 0.5f); | 
|  | 3519 |  | 
|  | 3520 | // Touch Window 1 | 
|  | 3521 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 3522 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3523 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3524 |  | 
|  | 3525 | // Touch Window 2 | 
|  | 3526 | int32_t actionPointerDown = | 
|  | 3527 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3528 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 3529 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3530 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3531 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3532 |  | 
|  | 3533 | // Move both windows | 
|  | 3534 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 3535 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 3536 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 3537 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3538 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3539 | } | 
|  | 3540 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3541 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { | 
|  | 3542 | virtual void SetUp() override { | 
|  | 3543 | InputDispatcherTest::SetUp(); | 
|  | 3544 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3545 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3546 | mApplication->setDispatchingTimeout(20ms); | 
|  | 3547 | mWindow = | 
|  | 3548 | new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 3549 | mWindow->setFrame(Rect(0, 0, 30, 30)); | 
| Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 3550 | mWindow->setDispatchingTimeout(30ms); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3551 | mWindow->setFocusable(true); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3552 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this | 
|  | 3553 | // window. | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3554 | mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3555 |  | 
|  | 3556 | // Set focused application. | 
|  | 3557 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); | 
|  | 3558 |  | 
|  | 3559 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3560 | setFocusedWindow(mWindow); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3561 | mWindow->consumeFocusEvent(true); | 
|  | 3562 | } | 
|  | 3563 |  | 
|  | 3564 | virtual void TearDown() override { | 
|  | 3565 | InputDispatcherTest::TearDown(); | 
|  | 3566 | mWindow.clear(); | 
|  | 3567 | } | 
|  | 3568 |  | 
|  | 3569 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3570 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3571 | sp<FakeWindowHandle> mWindow; | 
|  | 3572 | static constexpr PointF WINDOW_LOCATION = {20, 20}; | 
|  | 3573 |  | 
|  | 3574 | void tapOnWindow() { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3575 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3576 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3577 | WINDOW_LOCATION)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3578 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3579 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3580 | WINDOW_LOCATION)); | 
|  | 3581 | } | 
|  | 3582 | }; | 
|  | 3583 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3584 | // Send a tap and respond, which should not cause an ANR. | 
|  | 3585 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { | 
|  | 3586 | tapOnWindow(); | 
|  | 3587 | mWindow->consumeMotionDown(); | 
|  | 3588 | mWindow->consumeMotionUp(); | 
|  | 3589 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3590 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3591 | } | 
|  | 3592 |  | 
|  | 3593 | // Send a regular key and respond, which should not cause an ANR. | 
|  | 3594 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3595 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3596 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 3597 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3598 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3599 | } | 
|  | 3600 |  | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3601 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { | 
|  | 3602 | mWindow->setFocusable(false); | 
|  | 3603 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3604 | mWindow->consumeFocusEvent(false); | 
|  | 3605 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3606 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3607 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3608 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, | 
|  | 3609 | false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3610 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 3611 | // Key will not go to window because we have no focused window. | 
|  | 3612 | // The 'no focused window' ANR timer should start instead. | 
|  | 3613 |  | 
|  | 3614 | // Now, the focused application goes away. | 
|  | 3615 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); | 
|  | 3616 | // The key should get dropped and there should be no ANR. | 
|  | 3617 |  | 
|  | 3618 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3619 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3620 | } | 
|  | 3621 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3622 | // 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] | 3623 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. | 
|  | 3624 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3625 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3626 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3627 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3628 | WINDOW_LOCATION)); | 
|  | 3629 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3630 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN | 
|  | 3631 | ASSERT_TRUE(sequenceNum); | 
|  | 3632 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3633 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3634 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3635 | mWindow->finishEvent(*sequenceNum); | 
|  | 3636 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, | 
|  | 3637 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3638 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3639 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3640 | } | 
|  | 3641 |  | 
|  | 3642 | // Send a key to the app and have the app not respond right away. | 
|  | 3643 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { | 
|  | 3644 | // Inject a key, and don't respond - expect that ANR is called. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3645 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3646 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); | 
|  | 3647 | ASSERT_TRUE(sequenceNum); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3648 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3649 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3650 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3651 | } | 
|  | 3652 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3653 | // We have a focused application, but no focused window | 
|  | 3654 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3655 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3656 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3657 | mWindow->consumeFocusEvent(false); | 
|  | 3658 |  | 
|  | 3659 | // taps on the window work as normal | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3660 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3661 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3662 | WINDOW_LOCATION)); | 
|  | 3663 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); | 
|  | 3664 | mDispatcher->waitForIdle(); | 
|  | 3665 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3666 |  | 
|  | 3667 | // Once a focused event arrives, we get an ANR for this application | 
|  | 3668 | // We specify the injection timeout to be smaller than the application timeout, to ensure that | 
|  | 3669 | // injection times out (instead of failing). | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3670 | const InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3671 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3672 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3673 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3674 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3675 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3676 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3677 | } | 
|  | 3678 |  | 
|  | 3679 | // We have a focused application, but no focused window | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3680 | // Make sure that we don't notify policy twice about the same ANR. | 
|  | 3681 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3682 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3683 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3684 | mWindow->consumeFocusEvent(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3685 |  | 
|  | 3686 | // Once a focused event arrives, we get an ANR for this application | 
|  | 3687 | // We specify the injection timeout to be smaller than the application timeout, to ensure that | 
|  | 3688 | // injection times out (instead of failing). | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3689 | const InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3690 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3691 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3692 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3693 | const std::chrono::duration appTimeout = | 
|  | 3694 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3695 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3696 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3697 | std::this_thread::sleep_for(appTimeout); | 
|  | 3698 | // ANR should not be raised again. It is up to policy to do that if it desires. | 
|  | 3699 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3700 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3701 | // If we now get a focused window, the ANR should stop, but the policy handles that via | 
|  | 3702 | // '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] | 3703 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3704 | } | 
|  | 3705 |  | 
|  | 3706 | // We have a focused application, but no focused window | 
|  | 3707 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3708 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3709 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3710 | mWindow->consumeFocusEvent(false); | 
|  | 3711 |  | 
|  | 3712 | // Once a focused event arrives, we get an ANR for this application | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3713 | const InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3714 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3715 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); | 
|  | 3716 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3717 |  | 
|  | 3718 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3719 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3720 |  | 
|  | 3721 | // Future focused events get dropped right away | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3722 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3723 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 3724 | mWindow->assertNoEvents(); | 
|  | 3725 | } | 
|  | 3726 |  | 
|  | 3727 | /** | 
|  | 3728 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the | 
|  | 3729 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. | 
|  | 3730 | * If we process 1 of the events, but ANR on the second event with the same timestamp, | 
|  | 3731 | * the ANR mechanism should still work. | 
|  | 3732 | * | 
|  | 3733 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the | 
|  | 3734 | * DOWN event, while not responding on the second one. | 
|  | 3735 | */ | 
|  | 3736 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { | 
|  | 3737 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 3738 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3739 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, | 
|  | 3740 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 3741 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3742 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3743 |  | 
|  | 3744 | // Now send ACTION_UP, with identical timestamp | 
|  | 3745 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3746 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, | 
|  | 3747 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 3748 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3749 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3750 |  | 
|  | 3751 | // We have now sent down and up. Let's consume first event and then ANR on the second. | 
|  | 3752 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3753 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3754 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3755 | } | 
|  | 3756 |  | 
|  | 3757 | // If an app is not responding to a key event, gesture monitors should continue to receive | 
|  | 3758 | // new motion events | 
|  | 3759 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) { | 
|  | 3760 | FakeMonitorReceiver monitor = | 
|  | 3761 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, | 
|  | 3762 | true /*isGestureMonitor*/); | 
|  | 3763 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3764 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3765 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3766 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3767 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3768 |  | 
|  | 3769 | // Stuck on the ACTION_UP | 
|  | 3770 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3771 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3772 |  | 
|  | 3773 | // New tap will go to the gesture monitor, but not to the window | 
|  | 3774 | tapOnWindow(); | 
|  | 3775 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3776 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 3777 |  | 
|  | 3778 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion | 
|  | 3779 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3780 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3781 | mWindow->assertNoEvents(); | 
|  | 3782 | monitor.assertNoEvents(); | 
|  | 3783 | } | 
|  | 3784 |  | 
|  | 3785 | // If an app is not responding to a motion event, gesture monitors should continue to receive | 
|  | 3786 | // new motion events | 
|  | 3787 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) { | 
|  | 3788 | FakeMonitorReceiver monitor = | 
|  | 3789 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, | 
|  | 3790 | true /*isGestureMonitor*/); | 
|  | 3791 |  | 
|  | 3792 | tapOnWindow(); | 
|  | 3793 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3794 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 3795 |  | 
|  | 3796 | mWindow->consumeMotionDown(); | 
|  | 3797 | // Stuck on the ACTION_UP | 
|  | 3798 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3799 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3800 |  | 
|  | 3801 | // New tap will go to the gesture monitor, but not to the window | 
|  | 3802 | tapOnWindow(); | 
|  | 3803 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3804 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 3805 |  | 
|  | 3806 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion | 
|  | 3807 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3808 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3809 | mWindow->assertNoEvents(); | 
|  | 3810 | monitor.assertNoEvents(); | 
|  | 3811 | } | 
|  | 3812 |  | 
|  | 3813 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to | 
|  | 3814 | // process events, you don't get an anr. When the window later becomes unresponsive again, you | 
|  | 3815 | // get an ANR again. | 
|  | 3816 | // 1. tap -> block on ACTION_UP -> receive ANR | 
|  | 3817 | // 2. consume all pending events (= queue becomes healthy again) | 
|  | 3818 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time | 
|  | 3819 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { | 
|  | 3820 | tapOnWindow(); | 
|  | 3821 |  | 
|  | 3822 | mWindow->consumeMotionDown(); | 
|  | 3823 | // Block on ACTION_UP | 
|  | 3824 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3825 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3826 | mWindow->consumeMotionUp(); // Now the connection should be healthy again | 
|  | 3827 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3828 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3829 | mWindow->assertNoEvents(); | 
|  | 3830 |  | 
|  | 3831 | tapOnWindow(); | 
|  | 3832 | mWindow->consumeMotionDown(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3833 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3834 | mWindow->consumeMotionUp(); | 
|  | 3835 |  | 
|  | 3836 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3837 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3838 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3839 | mWindow->assertNoEvents(); | 
|  | 3840 | } | 
|  | 3841 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3842 | // If a connection remains unresponsive for a while, make sure policy is only notified once about | 
|  | 3843 | // it. | 
|  | 3844 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3845 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3846 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3847 | WINDOW_LOCATION)); | 
|  | 3848 |  | 
|  | 3849 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3850 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3851 | std::this_thread::sleep_for(windowTimeout); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3852 | // 'notifyConnectionUnresponsive' should only be called once per connection | 
|  | 3853 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 3854 | // 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] | 3855 | mWindow->consumeMotionDown(); | 
|  | 3856 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, | 
|  | 3857 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 3858 | mWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3859 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 3860 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 3861 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3862 | } | 
|  | 3863 |  | 
|  | 3864 | /** | 
|  | 3865 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 3866 | * not to to the focused window until the motion is processed. | 
|  | 3867 | * | 
|  | 3868 | * Warning!!! | 
|  | 3869 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT | 
|  | 3870 | * and the injection timeout that we specify when injecting the key. | 
|  | 3871 | * We must have the injection timeout (10ms) be smaller than | 
|  | 3872 | *  KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). | 
|  | 3873 | * | 
|  | 3874 | * If that value changes, this test should also change. | 
|  | 3875 | */ | 
|  | 3876 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { | 
|  | 3877 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering | 
|  | 3878 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3879 |  | 
|  | 3880 | tapOnWindow(); | 
|  | 3881 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); | 
|  | 3882 | ASSERT_TRUE(downSequenceNum); | 
|  | 3883 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); | 
|  | 3884 | ASSERT_TRUE(upSequenceNum); | 
|  | 3885 | // Don't finish the events yet, and send a key | 
|  | 3886 | // Injection will "succeed" because we will eventually give up and send the key to the focused | 
|  | 3887 | // window even if motions are still being processed. But because the injection timeout is short, | 
|  | 3888 | // we will receive INJECTION_TIMED_OUT as the result. | 
|  | 3889 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3890 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3891 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3892 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); | 
|  | 3893 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3894 | // Key will not be sent to the window, yet, because the window is still processing events | 
|  | 3895 | // and the key remains pending, waiting for the touch events to be processed | 
|  | 3896 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); | 
|  | 3897 | ASSERT_FALSE(keySequenceNum); | 
|  | 3898 |  | 
|  | 3899 | std::this_thread::sleep_for(500ms); | 
|  | 3900 | // if we wait long enough though, dispatcher will give up, and still send the key | 
|  | 3901 | // to the focused window, even though we have not yet finished the motion event | 
|  | 3902 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 3903 | mWindow->finishEvent(*downSequenceNum); | 
|  | 3904 | mWindow->finishEvent(*upSequenceNum); | 
|  | 3905 | } | 
|  | 3906 |  | 
|  | 3907 | /** | 
|  | 3908 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 3909 | * not go to the focused window until the motion is processed. | 
|  | 3910 | * If then a new motion comes in, then the pending key event should be going to the currently | 
|  | 3911 | * focused window right away. | 
|  | 3912 | */ | 
|  | 3913 | TEST_F(InputDispatcherSingleWindowAnr, | 
|  | 3914 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { | 
|  | 3915 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering | 
|  | 3916 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 3917 |  | 
|  | 3918 | tapOnWindow(); | 
|  | 3919 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); | 
|  | 3920 | ASSERT_TRUE(downSequenceNum); | 
|  | 3921 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); | 
|  | 3922 | ASSERT_TRUE(upSequenceNum); | 
|  | 3923 | // Don't finish the events yet, and send a key | 
|  | 3924 | // Injection is async, so it will succeed | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3925 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3926 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3927 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3928 | // At this point, key is still pending, and should not be sent to the application yet. | 
|  | 3929 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); | 
|  | 3930 | ASSERT_FALSE(keySequenceNum); | 
|  | 3931 |  | 
|  | 3932 | // Now tap down again. It should cause the pending key to go to the focused window right away. | 
|  | 3933 | tapOnWindow(); | 
|  | 3934 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd | 
|  | 3935 | // the other events yet. We can finish events in any order. | 
|  | 3936 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN | 
|  | 3937 | mWindow->finishEvent(*upSequenceNum);   // first tap's ACTION_UP | 
|  | 3938 | mWindow->consumeMotionDown(); | 
|  | 3939 | mWindow->consumeMotionUp(); | 
|  | 3940 | mWindow->assertNoEvents(); | 
|  | 3941 | } | 
|  | 3942 |  | 
|  | 3943 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { | 
|  | 3944 | virtual void SetUp() override { | 
|  | 3945 | InputDispatcherTest::SetUp(); | 
|  | 3946 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3947 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3948 | mApplication->setDispatchingTimeout(10ms); | 
|  | 3949 | mUnfocusedWindow = | 
|  | 3950 | new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT); | 
|  | 3951 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); | 
|  | 3952 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this | 
|  | 3953 | // window. | 
|  | 3954 | // 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] | 3955 | mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 3956 | InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH | | 
|  | 3957 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3958 |  | 
|  | 3959 | mFocusedWindow = | 
|  | 3960 | new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 3961 | mFocusedWindow->setDispatchingTimeout(30ms); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3962 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3963 | mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | | 
|  | 3964 | InputWindowInfo::Flag::SPLIT_TOUCH); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3965 |  | 
|  | 3966 | // Set focused application. | 
|  | 3967 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3968 | mFocusedWindow->setFocusable(true); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3969 |  | 
|  | 3970 | // Expect one focus window exist in display. | 
|  | 3971 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3972 | setFocusedWindow(mFocusedWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3973 | mFocusedWindow->consumeFocusEvent(true); | 
|  | 3974 | } | 
|  | 3975 |  | 
|  | 3976 | virtual void TearDown() override { | 
|  | 3977 | InputDispatcherTest::TearDown(); | 
|  | 3978 |  | 
|  | 3979 | mUnfocusedWindow.clear(); | 
|  | 3980 | mFocusedWindow.clear(); | 
|  | 3981 | } | 
|  | 3982 |  | 
|  | 3983 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3984 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3985 | sp<FakeWindowHandle> mUnfocusedWindow; | 
|  | 3986 | sp<FakeWindowHandle> mFocusedWindow; | 
|  | 3987 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; | 
|  | 3988 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; | 
|  | 3989 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; | 
|  | 3990 |  | 
|  | 3991 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } | 
|  | 3992 |  | 
|  | 3993 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } | 
|  | 3994 |  | 
|  | 3995 | private: | 
|  | 3996 | void tap(const PointF& location) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3997 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3998 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3999 | location)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4000 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4001 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4002 | location)); | 
|  | 4003 | } | 
|  | 4004 | }; | 
|  | 4005 |  | 
|  | 4006 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout | 
|  | 4007 | // should be ANR'd first. | 
|  | 4008 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4009 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4010 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4011 | FOCUSED_WINDOW_LOCATION)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4012 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4013 | mFocusedWindow->consumeMotionDown(); | 
|  | 4014 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
|  | 4015 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 4016 | // We consumed all events, so no ANR | 
|  | 4017 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4018 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4019 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4020 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4021 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4022 | FOCUSED_WINDOW_LOCATION)); | 
|  | 4023 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 4024 | ASSERT_TRUE(unfocusedSequenceNum); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4025 |  | 
|  | 4026 | const std::chrono::duration timeout = | 
|  | 4027 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4028 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4029 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event | 
|  | 4030 | // sequence to make it consistent | 
|  | 4031 | mFocusedWindow->consumeMotionCancel(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4032 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4033 | mFocusedWindow->consumeMotionDown(); | 
|  | 4034 | // This cancel is generated because the connection was unresponsive | 
|  | 4035 | mFocusedWindow->consumeMotionCancel(); | 
|  | 4036 | mFocusedWindow->assertNoEvents(); | 
|  | 4037 | mUnfocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4038 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4039 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4040 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4041 | } | 
|  | 4042 |  | 
|  | 4043 | // If we have 2 windows with identical timeouts that are both unresponsive, | 
|  | 4044 | // it doesn't matter which order they should have ANR. | 
|  | 4045 | // But we should receive ANR for both. | 
|  | 4046 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { | 
|  | 4047 | // Set the timeout for unfocused window to match the focused window | 
|  | 4048 | mUnfocusedWindow->setDispatchingTimeout(10ms); | 
|  | 4049 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
|  | 4050 |  | 
|  | 4051 | tapOnFocusedWindow(); | 
|  | 4052 | // 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] | 4053 | sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms); | 
|  | 4054 | sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4055 |  | 
|  | 4056 | // 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] | 4057 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || | 
|  | 4058 | mFocusedWindow->getToken() == anrConnectionToken2); | 
|  | 4059 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || | 
|  | 4060 | mUnfocusedWindow->getToken() == anrConnectionToken2); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4061 |  | 
|  | 4062 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4063 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4064 |  | 
|  | 4065 | mFocusedWindow->consumeMotionDown(); | 
|  | 4066 | mFocusedWindow->consumeMotionUp(); | 
|  | 4067 | mUnfocusedWindow->consumeMotionOutside(); | 
|  | 4068 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4069 | sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken(); | 
|  | 4070 | sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4071 |  | 
|  | 4072 | // Both applications should be marked as responsive, in any order | 
|  | 4073 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || | 
|  | 4074 | mFocusedWindow->getToken() == responsiveToken2); | 
|  | 4075 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || | 
|  | 4076 | mUnfocusedWindow->getToken() == responsiveToken2); | 
|  | 4077 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4078 | } | 
|  | 4079 |  | 
|  | 4080 | // If a window is already not responding, the second tap on the same window should be ignored. | 
|  | 4081 | // We should also log an error to account for the dropped event (not tested here). | 
|  | 4082 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. | 
|  | 4083 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { | 
|  | 4084 | tapOnFocusedWindow(); | 
|  | 4085 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
|  | 4086 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 4087 | // Receive the events, but don't respond | 
|  | 4088 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN | 
|  | 4089 | ASSERT_TRUE(downEventSequenceNum); | 
|  | 4090 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP | 
|  | 4091 | ASSERT_TRUE(upEventSequenceNum); | 
|  | 4092 | const std::chrono::duration timeout = | 
|  | 4093 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4094 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4095 |  | 
|  | 4096 | // Tap once again | 
|  | 4097 | // 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] | 4098 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4099 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4100 | FOCUSED_WINDOW_LOCATION)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4101 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4102 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4103 | FOCUSED_WINDOW_LOCATION)); | 
|  | 4104 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a | 
|  | 4105 | // valid touch target | 
|  | 4106 | mUnfocusedWindow->assertNoEvents(); | 
|  | 4107 |  | 
|  | 4108 | // Consume the first tap | 
|  | 4109 | mFocusedWindow->finishEvent(*downEventSequenceNum); | 
|  | 4110 | mFocusedWindow->finishEvent(*upEventSequenceNum); | 
|  | 4111 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4112 | // The second tap did not go to the focused window | 
|  | 4113 | mFocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4114 | // Since all events are finished, connection should be deemed healthy again | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4115 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4116 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4117 | } | 
|  | 4118 |  | 
|  | 4119 | // If you tap outside of all windows, there will not be ANR | 
|  | 4120 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4121 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4122 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4123 | LOCATION_OUTSIDE_ALL_WINDOWS)); | 
|  | 4124 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4125 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4126 | } | 
|  | 4127 |  | 
|  | 4128 | // Since the focused window is paused, tapping on it should not produce any events | 
|  | 4129 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { | 
|  | 4130 | mFocusedWindow->setPaused(true); | 
|  | 4131 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
|  | 4132 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4133 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4134 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4135 | FOCUSED_WINDOW_LOCATION)); | 
|  | 4136 |  | 
|  | 4137 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); | 
|  | 4138 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4139 | // Should not ANR because the window is paused, and touches shouldn't go to it | 
|  | 4140 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4141 |  | 
|  | 4142 | mFocusedWindow->assertNoEvents(); | 
|  | 4143 | mUnfocusedWindow->assertNoEvents(); | 
|  | 4144 | } | 
|  | 4145 |  | 
|  | 4146 | /** | 
|  | 4147 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 4148 | * not to to the focused window until the motion is processed. | 
|  | 4149 | * If a different window becomes focused at this time, the key should go to that window instead. | 
|  | 4150 | * | 
|  | 4151 | * Warning!!! | 
|  | 4152 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT | 
|  | 4153 | * and the injection timeout that we specify when injecting the key. | 
|  | 4154 | * We must have the injection timeout (10ms) be smaller than | 
|  | 4155 | *  KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). | 
|  | 4156 | * | 
|  | 4157 | * If that value changes, this test should also change. | 
|  | 4158 | */ | 
|  | 4159 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { | 
|  | 4160 | // Set a long ANR timeout to prevent it from triggering | 
|  | 4161 | mFocusedWindow->setDispatchingTimeout(2s); | 
|  | 4162 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 4163 |  | 
|  | 4164 | tapOnUnfocusedWindow(); | 
|  | 4165 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 4166 | ASSERT_TRUE(downSequenceNum); | 
|  | 4167 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 4168 | ASSERT_TRUE(upSequenceNum); | 
|  | 4169 | // Don't finish the events yet, and send a key | 
|  | 4170 | // Injection will succeed because we will eventually give up and send the key to the focused | 
|  | 4171 | // window even if motions are still being processed. | 
|  | 4172 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4173 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4174 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4175 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); | 
|  | 4176 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4177 | // Key will not be sent to the window, yet, because the window is still processing events | 
|  | 4178 | // and the key remains pending, waiting for the touch events to be processed | 
|  | 4179 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); | 
|  | 4180 | ASSERT_FALSE(keySequenceNum); | 
|  | 4181 |  | 
|  | 4182 | // 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] | 4183 | mFocusedWindow->setFocusable(false); | 
|  | 4184 | mUnfocusedWindow->setFocusable(true); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4185 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4186 | setFocusedWindow(mUnfocusedWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4187 |  | 
|  | 4188 | // Focus events should precede the key events | 
|  | 4189 | mUnfocusedWindow->consumeFocusEvent(true); | 
|  | 4190 | mFocusedWindow->consumeFocusEvent(false); | 
|  | 4191 |  | 
|  | 4192 | // Finish the tap events, which should unblock dispatcher | 
|  | 4193 | mUnfocusedWindow->finishEvent(*downSequenceNum); | 
|  | 4194 | mUnfocusedWindow->finishEvent(*upSequenceNum); | 
|  | 4195 |  | 
|  | 4196 | // Now that all queues are cleared and no backlog in the connections, the key event | 
|  | 4197 | // can finally go to the newly focused "mUnfocusedWindow". | 
|  | 4198 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 4199 | mFocusedWindow->assertNoEvents(); | 
|  | 4200 | mUnfocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4201 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4202 | } | 
|  | 4203 |  | 
|  | 4204 | // When the touch stream is split across 2 windows, and one of them does not respond, | 
|  | 4205 | // then ANR should be raised and the touch should be canceled for the unresponsive window. | 
|  | 4206 | // The other window should not be affected by that. | 
|  | 4207 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { | 
|  | 4208 | // Touch Window 1 | 
|  | 4209 | NotifyMotionArgs motionArgs = | 
|  | 4210 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4211 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); | 
|  | 4212 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4213 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
|  | 4214 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 4215 |  | 
|  | 4216 | // Touch Window 2 | 
|  | 4217 | int32_t actionPointerDown = | 
|  | 4218 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
|  | 4219 |  | 
|  | 4220 | motionArgs = | 
|  | 4221 | generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4222 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); | 
|  | 4223 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4224 |  | 
|  | 4225 | const std::chrono::duration timeout = | 
|  | 4226 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4227 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4228 |  | 
|  | 4229 | mUnfocusedWindow->consumeMotionDown(); | 
|  | 4230 | mFocusedWindow->consumeMotionDown(); | 
|  | 4231 | // Focused window may or may not receive ACTION_MOVE | 
|  | 4232 | // But it should definitely receive ACTION_CANCEL due to the ANR | 
|  | 4233 | InputEvent* event; | 
|  | 4234 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); | 
|  | 4235 | ASSERT_TRUE(moveOrCancelSequenceNum); | 
|  | 4236 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); | 
|  | 4237 | ASSERT_NE(nullptr, event); | 
|  | 4238 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); | 
|  | 4239 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 4240 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { | 
|  | 4241 | mFocusedWindow->consumeMotionCancel(); | 
|  | 4242 | } else { | 
|  | 4243 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); | 
|  | 4244 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4245 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4246 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4247 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4248 | mUnfocusedWindow->assertNoEvents(); | 
|  | 4249 | mFocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4250 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4251 | } | 
|  | 4252 |  | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 4253 | /** | 
|  | 4254 | * If we have no focused window, and a key comes in, we start the ANR timer. | 
|  | 4255 | * The focused application should add a focused window before the timer runs out to prevent ANR. | 
|  | 4256 | * | 
|  | 4257 | * If the user touches another application during this time, the key should be dropped. | 
|  | 4258 | * Next, if a new focused window comes in, without toggling the focused application, | 
|  | 4259 | * then no ANR should occur. | 
|  | 4260 | * | 
|  | 4261 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', | 
|  | 4262 | * but in some cases the policy may not update the focused application. | 
|  | 4263 | */ | 
|  | 4264 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { | 
|  | 4265 | std::shared_ptr<FakeApplicationHandle> focusedApplication = | 
|  | 4266 | std::make_shared<FakeApplicationHandle>(); | 
|  | 4267 | focusedApplication->setDispatchingTimeout(60ms); | 
|  | 4268 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); | 
|  | 4269 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. | 
|  | 4270 | mFocusedWindow->setFocusable(false); | 
|  | 4271 |  | 
|  | 4272 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 4273 | mFocusedWindow->consumeFocusEvent(false); | 
|  | 4274 |  | 
|  | 4275 | // Send a key. The ANR timer should start because there is no focused window. | 
|  | 4276 | // 'focusedApplication' will get blamed if this timer completes. | 
|  | 4277 | // 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] | 4278 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 4279 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4280 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, | 
|  | 4281 | false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4282 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 4283 |  | 
|  | 4284 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, | 
|  | 4285 | // then the injected touches won't cause the focused event to get dropped. | 
|  | 4286 | // The dispatcher only checks for whether the queue should be pruned upon queueing. | 
|  | 4287 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would | 
|  | 4288 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. | 
|  | 4289 | // For this test, it means that the key would get delivered to the window once it becomes | 
|  | 4290 | // focused. | 
|  | 4291 | std::this_thread::sleep_for(10ms); | 
|  | 4292 |  | 
|  | 4293 | // Touch unfocused window. This should force the pending key to get dropped. | 
|  | 4294 | NotifyMotionArgs motionArgs = | 
|  | 4295 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4296 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); | 
|  | 4297 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4298 |  | 
|  | 4299 | // We do not consume the motion right away, because that would require dispatcher to first | 
|  | 4300 | // process (== drop) the key event, and by that time, ANR will be raised. | 
|  | 4301 | // Set the focused window first. | 
|  | 4302 | mFocusedWindow->setFocusable(true); | 
|  | 4303 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 4304 | setFocusedWindow(mFocusedWindow); | 
|  | 4305 | mFocusedWindow->consumeFocusEvent(true); | 
|  | 4306 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs | 
|  | 4307 | // to another application. This could be a bug / behaviour in the policy. | 
|  | 4308 |  | 
|  | 4309 | mUnfocusedWindow->consumeMotionDown(); | 
|  | 4310 |  | 
|  | 4311 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4312 | // Should not ANR because we actually have a focused window. It was just added too slowly. | 
|  | 4313 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); | 
|  | 4314 | } | 
|  | 4315 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4316 | // These tests ensure we cannot send touch events to a window that's positioned behind a window | 
|  | 4317 | // that has feature NO_INPUT_CHANNEL. | 
|  | 4318 | // Layout: | 
|  | 4319 | //   Top (closest to user) | 
|  | 4320 | //       mNoInputWindow (above all windows) | 
|  | 4321 | //       mBottomWindow | 
|  | 4322 | //   Bottom (furthest from user) | 
|  | 4323 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { | 
|  | 4324 | virtual void SetUp() override { | 
|  | 4325 | InputDispatcherTest::SetUp(); | 
|  | 4326 |  | 
|  | 4327 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
|  | 4328 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, | 
|  | 4329 | "Window without input channel", ADISPLAY_ID_DEFAULT, | 
|  | 4330 | std::make_optional<sp<IBinder>>(nullptr) /*token*/); | 
|  | 4331 |  | 
|  | 4332 | mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL); | 
|  | 4333 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 4334 | // It's perfectly valid for this window to not have an associated input channel | 
|  | 4335 |  | 
|  | 4336 | mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window", | 
|  | 4337 | ADISPLAY_ID_DEFAULT); | 
|  | 4338 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 4339 |  | 
|  | 4340 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); | 
|  | 4341 | } | 
|  | 4342 |  | 
|  | 4343 | protected: | 
|  | 4344 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
|  | 4345 | sp<FakeWindowHandle> mNoInputWindow; | 
|  | 4346 | sp<FakeWindowHandle> mBottomWindow; | 
|  | 4347 | }; | 
|  | 4348 |  | 
|  | 4349 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { | 
|  | 4350 | PointF touchedPoint = {10, 10}; | 
|  | 4351 |  | 
|  | 4352 | NotifyMotionArgs motionArgs = | 
|  | 4353 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4354 | ADISPLAY_ID_DEFAULT, {touchedPoint}); | 
|  | 4355 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4356 |  | 
|  | 4357 | mNoInputWindow->assertNoEvents(); | 
|  | 4358 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have | 
|  | 4359 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, | 
|  | 4360 | // and therefore should prevent mBottomWindow from receiving touches | 
|  | 4361 | mBottomWindow->assertNoEvents(); | 
|  | 4362 | } | 
|  | 4363 |  | 
|  | 4364 | /** | 
|  | 4365 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, | 
|  | 4366 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. | 
|  | 4367 | */ | 
|  | 4368 | TEST_F(InputDispatcherMultiWindowOcclusionTests, | 
|  | 4369 | NoInputChannelFeature_DropsTouchesWithValidChannel) { | 
|  | 4370 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, | 
|  | 4371 | "Window with input channel and NO_INPUT_CHANNEL", | 
|  | 4372 | ADISPLAY_ID_DEFAULT); | 
|  | 4373 |  | 
|  | 4374 | mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL); | 
|  | 4375 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 4376 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); | 
|  | 4377 |  | 
|  | 4378 | PointF touchedPoint = {10, 10}; | 
|  | 4379 |  | 
|  | 4380 | NotifyMotionArgs motionArgs = | 
|  | 4381 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4382 | ADISPLAY_ID_DEFAULT, {touchedPoint}); | 
|  | 4383 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4384 |  | 
|  | 4385 | mNoInputWindow->assertNoEvents(); | 
|  | 4386 | mBottomWindow->assertNoEvents(); | 
|  | 4387 | } | 
|  | 4388 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4389 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { | 
|  | 4390 | protected: | 
|  | 4391 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 4392 | sp<FakeWindowHandle> mWindow; | 
|  | 4393 | sp<FakeWindowHandle> mMirror; | 
|  | 4394 |  | 
|  | 4395 | virtual void SetUp() override { | 
|  | 4396 | InputDispatcherTest::SetUp(); | 
|  | 4397 | mApp = std::make_shared<FakeApplicationHandle>(); | 
|  | 4398 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 4399 | mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT, | 
|  | 4400 | mWindow->getToken()); | 
|  | 4401 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
|  | 4402 | mWindow->setFocusable(true); | 
|  | 4403 | mMirror->setFocusable(true); | 
|  | 4404 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4405 | } | 
|  | 4406 | }; | 
|  | 4407 |  | 
|  | 4408 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { | 
|  | 4409 | // Request focus on a mirrored window | 
|  | 4410 | setFocusedWindow(mMirror); | 
|  | 4411 |  | 
|  | 4412 | // window gets focused | 
|  | 4413 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4414 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4415 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4416 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 4417 | } | 
|  | 4418 |  | 
|  | 4419 | // A focused & mirrored window remains focused only if the window and its mirror are both | 
|  | 4420 | // focusable. | 
|  | 4421 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { | 
|  | 4422 | setFocusedWindow(mMirror); | 
|  | 4423 |  | 
|  | 4424 | // window gets focused | 
|  | 4425 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4426 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4427 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4428 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4429 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4430 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4431 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4432 |  | 
|  | 4433 | mMirror->setFocusable(false); | 
|  | 4434 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4435 |  | 
|  | 4436 | // window loses focus since one of the windows associated with the token in not focusable | 
|  | 4437 | mWindow->consumeFocusEvent(false); | 
|  | 4438 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4439 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 4440 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4441 | mWindow->assertNoEvents(); | 
|  | 4442 | } | 
|  | 4443 |  | 
|  | 4444 | // A focused & mirrored window remains focused until the window and its mirror both become | 
|  | 4445 | // invisible. | 
|  | 4446 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { | 
|  | 4447 | setFocusedWindow(mMirror); | 
|  | 4448 |  | 
|  | 4449 | // window gets focused | 
|  | 4450 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4451 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4452 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4453 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4454 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4455 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4456 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4457 |  | 
|  | 4458 | mMirror->setVisible(false); | 
|  | 4459 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4460 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4461 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4462 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4463 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4464 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4465 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4466 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4467 |  | 
|  | 4468 | mWindow->setVisible(false); | 
|  | 4469 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4470 |  | 
|  | 4471 | // window loses focus only after all windows associated with the token become invisible. | 
|  | 4472 | mWindow->consumeFocusEvent(false); | 
|  | 4473 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4474 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 4475 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4476 | mWindow->assertNoEvents(); | 
|  | 4477 | } | 
|  | 4478 |  | 
|  | 4479 | // A focused & mirrored window remains focused until both windows are removed. | 
|  | 4480 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { | 
|  | 4481 | setFocusedWindow(mMirror); | 
|  | 4482 |  | 
|  | 4483 | // window gets focused | 
|  | 4484 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4485 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4486 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4487 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4488 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4489 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4490 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4491 |  | 
|  | 4492 | // single window is removed but the window token remains focused | 
|  | 4493 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); | 
|  | 4494 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4495 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4496 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4497 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4498 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 4499 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4500 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 4501 |  | 
|  | 4502 | // Both windows are removed | 
|  | 4503 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); | 
|  | 4504 | mWindow->consumeFocusEvent(false); | 
|  | 4505 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4506 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 4507 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4508 | mWindow->assertNoEvents(); | 
|  | 4509 | } | 
|  | 4510 |  | 
|  | 4511 | // Focus request can be pending until one window becomes visible. | 
|  | 4512 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { | 
|  | 4513 | // Request focus on an invisible mirror. | 
|  | 4514 | mWindow->setVisible(false); | 
|  | 4515 | mMirror->setVisible(false); | 
|  | 4516 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4517 | setFocusedWindow(mMirror); | 
|  | 4518 |  | 
|  | 4519 | // Injected key goes to pending queue. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4520 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4521 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4522 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4523 |  | 
|  | 4524 | mMirror->setVisible(true); | 
|  | 4525 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 4526 |  | 
|  | 4527 | // window gets focused | 
|  | 4528 | mWindow->consumeFocusEvent(true); | 
|  | 4529 | // window gets the pending key event | 
|  | 4530 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 4531 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4532 |  | 
|  | 4533 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { | 
|  | 4534 | protected: | 
|  | 4535 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 4536 | sp<FakeWindowHandle> mWindow; | 
|  | 4537 | sp<FakeWindowHandle> mSecondWindow; | 
|  | 4538 |  | 
|  | 4539 | void SetUp() override { | 
|  | 4540 | InputDispatcherTest::SetUp(); | 
|  | 4541 | mApp = std::make_shared<FakeApplicationHandle>(); | 
|  | 4542 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 4543 | mWindow->setFocusable(true); | 
|  | 4544 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); | 
|  | 4545 | mSecondWindow->setFocusable(true); | 
|  | 4546 |  | 
|  | 4547 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
|  | 4548 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); | 
|  | 4549 |  | 
|  | 4550 | setFocusedWindow(mWindow); | 
|  | 4551 | mWindow->consumeFocusEvent(true); | 
|  | 4552 | } | 
|  | 4553 |  | 
|  | 4554 | void notifyPointerCaptureChanged(bool enabled) { | 
|  | 4555 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(enabled); | 
|  | 4556 | mDispatcher->notifyPointerCaptureChanged(&args); | 
|  | 4557 | } | 
|  | 4558 |  | 
|  | 4559 | void requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, bool enabled) { | 
|  | 4560 | mDispatcher->requestPointerCapture(window->getToken(), enabled); | 
|  | 4561 | mFakePolicy->waitForSetPointerCapture(enabled); | 
|  | 4562 | notifyPointerCaptureChanged(enabled); | 
|  | 4563 | window->consumeCaptureEvent(enabled); | 
|  | 4564 | } | 
|  | 4565 | }; | 
|  | 4566 |  | 
|  | 4567 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { | 
|  | 4568 | // Ensure that capture cannot be obtained for unfocused windows. | 
|  | 4569 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); | 
|  | 4570 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 4571 | mSecondWindow->assertNoEvents(); | 
|  | 4572 |  | 
|  | 4573 | // Ensure that capture can be enabled from the focus window. | 
|  | 4574 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 4575 |  | 
|  | 4576 | // Ensure that capture cannot be disabled from a window that does not have capture. | 
|  | 4577 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); | 
|  | 4578 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 4579 |  | 
|  | 4580 | // Ensure that capture can be disabled from the window with capture. | 
|  | 4581 | requestAndVerifyPointerCapture(mWindow, false); | 
|  | 4582 | } | 
|  | 4583 |  | 
|  | 4584 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { | 
|  | 4585 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 4586 |  | 
|  | 4587 | setFocusedWindow(mSecondWindow); | 
|  | 4588 |  | 
|  | 4589 | // Ensure that the capture disabled event was sent first. | 
|  | 4590 | mWindow->consumeCaptureEvent(false); | 
|  | 4591 | mWindow->consumeFocusEvent(false); | 
|  | 4592 | mSecondWindow->consumeFocusEvent(true); | 
|  | 4593 | mFakePolicy->waitForSetPointerCapture(false); | 
|  | 4594 |  | 
|  | 4595 | // Ensure that additional state changes from InputReader are not sent to the window. | 
|  | 4596 | notifyPointerCaptureChanged(false); | 
|  | 4597 | notifyPointerCaptureChanged(true); | 
|  | 4598 | notifyPointerCaptureChanged(false); | 
|  | 4599 | mWindow->assertNoEvents(); | 
|  | 4600 | mSecondWindow->assertNoEvents(); | 
|  | 4601 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 4602 | } | 
|  | 4603 |  | 
|  | 4604 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { | 
|  | 4605 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 4606 |  | 
|  | 4607 | // InputReader unexpectedly disables and enables pointer capture. | 
|  | 4608 | notifyPointerCaptureChanged(false); | 
|  | 4609 | notifyPointerCaptureChanged(true); | 
|  | 4610 |  | 
|  | 4611 | // Ensure that Pointer Capture is disabled. | 
| Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 4612 | mFakePolicy->waitForSetPointerCapture(false); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4613 | mWindow->consumeCaptureEvent(false); | 
|  | 4614 | mWindow->assertNoEvents(); | 
|  | 4615 | } | 
|  | 4616 |  | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 4617 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { | 
|  | 4618 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 4619 |  | 
|  | 4620 | // The first window loses focus. | 
|  | 4621 | setFocusedWindow(mSecondWindow); | 
|  | 4622 | mFakePolicy->waitForSetPointerCapture(false); | 
|  | 4623 | mWindow->consumeCaptureEvent(false); | 
|  | 4624 |  | 
|  | 4625 | // Request Pointer Capture from the second window before the notification from InputReader | 
|  | 4626 | // arrives. | 
|  | 4627 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); | 
|  | 4628 | mFakePolicy->waitForSetPointerCapture(true); | 
|  | 4629 |  | 
|  | 4630 | // InputReader notifies Pointer Capture was disabled (because of the focus change). | 
|  | 4631 | notifyPointerCaptureChanged(false); | 
|  | 4632 |  | 
|  | 4633 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). | 
|  | 4634 | notifyPointerCaptureChanged(true); | 
|  | 4635 |  | 
|  | 4636 | mSecondWindow->consumeFocusEvent(true); | 
|  | 4637 | mSecondWindow->consumeCaptureEvent(true); | 
|  | 4638 | } | 
|  | 4639 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4640 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { | 
|  | 4641 | protected: | 
|  | 4642 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4643 |  | 
|  | 4644 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; | 
|  | 4645 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); | 
|  | 4646 |  | 
|  | 4647 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; | 
|  | 4648 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); | 
|  | 4649 |  | 
|  | 4650 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold | 
|  | 4651 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; | 
|  | 4652 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); | 
|  | 4653 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < | 
|  | 4654 | MAXIMUM_OBSCURING_OPACITY); | 
|  | 4655 |  | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4656 | static const int32_t TOUCHED_APP_UID = 10001; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4657 | static const int32_t APP_B_UID = 10002; | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4658 | static const int32_t APP_C_UID = 10003; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4659 |  | 
|  | 4660 | sp<FakeWindowHandle> mTouchWindow; | 
|  | 4661 |  | 
|  | 4662 | virtual void SetUp() override { | 
|  | 4663 | InputDispatcherTest::SetUp(); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4664 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4665 | mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK); | 
|  | 4666 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); | 
|  | 4667 | } | 
|  | 4668 |  | 
|  | 4669 | virtual void TearDown() override { | 
|  | 4670 | InputDispatcherTest::TearDown(); | 
|  | 4671 | mTouchWindow.clear(); | 
|  | 4672 | } | 
|  | 4673 |  | 
|  | 4674 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4675 | os::TouchOcclusionMode mode, float alpha = 1.0f) { | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4676 | sp<FakeWindowHandle> window = getWindow(uid, name); | 
|  | 4677 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCHABLE); | 
|  | 4678 | window->setTouchOcclusionMode(mode); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4679 | window->setAlpha(alpha); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4680 | return window; | 
|  | 4681 | } | 
|  | 4682 |  | 
|  | 4683 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { | 
|  | 4684 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); | 
|  | 4685 | sp<FakeWindowHandle> window = | 
|  | 4686 | new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); | 
|  | 4687 | // Generate an arbitrary PID based on the UID | 
|  | 4688 | window->setOwnerInfo(1777 + (uid % 10000), uid); | 
|  | 4689 | return window; | 
|  | 4690 | } | 
|  | 4691 |  | 
|  | 4692 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { | 
|  | 4693 | NotifyMotionArgs args = | 
|  | 4694 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4695 | ADISPLAY_ID_DEFAULT, points); | 
|  | 4696 | mDispatcher->notifyMotion(&args); | 
|  | 4697 | } | 
|  | 4698 | }; | 
|  | 4699 |  | 
|  | 4700 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4701 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4702 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4703 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4704 |  | 
|  | 4705 | touch(); | 
|  | 4706 |  | 
|  | 4707 | mTouchWindow->assertNoEvents(); | 
|  | 4708 | } | 
|  | 4709 |  | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 4710 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4711 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { | 
|  | 4712 | const sp<FakeWindowHandle>& w = | 
|  | 4713 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); | 
|  | 4714 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4715 |  | 
|  | 4716 | touch(); | 
|  | 4717 |  | 
|  | 4718 | mTouchWindow->assertNoEvents(); | 
|  | 4719 | } | 
|  | 4720 |  | 
|  | 4721 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 4722 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { | 
|  | 4723 | const sp<FakeWindowHandle>& w = | 
|  | 4724 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 4725 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4726 |  | 
|  | 4727 | touch(); | 
|  | 4728 |  | 
|  | 4729 | w->assertNoEvents(); | 
|  | 4730 | } | 
|  | 4731 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4732 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4733 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); | 
|  | 4734 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4735 |  | 
|  | 4736 | touch(); | 
|  | 4737 |  | 
|  | 4738 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4739 | } | 
|  | 4740 |  | 
|  | 4741 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4742 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4743 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4744 | w->setFrame(Rect(0, 0, 50, 50)); | 
|  | 4745 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4746 |  | 
|  | 4747 | touch({PointF{100, 100}}); | 
|  | 4748 |  | 
|  | 4749 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4750 | } | 
|  | 4751 |  | 
|  | 4752 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4753 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4754 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4755 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4756 |  | 
|  | 4757 | touch(); | 
|  | 4758 |  | 
|  | 4759 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4760 | } | 
|  | 4761 |  | 
|  | 4762 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { | 
|  | 4763 | const sp<FakeWindowHandle>& w = | 
|  | 4764 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
|  | 4765 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4766 |  | 
|  | 4767 | touch(); | 
|  | 4768 |  | 
|  | 4769 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4770 | } | 
|  | 4771 |  | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 4772 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { | 
|  | 4773 | const sp<FakeWindowHandle>& w = | 
|  | 4774 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
|  | 4775 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4776 |  | 
|  | 4777 | touch(); | 
|  | 4778 |  | 
|  | 4779 | w->assertNoEvents(); | 
|  | 4780 | } | 
|  | 4781 |  | 
|  | 4782 | /** | 
|  | 4783 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs | 
|  | 4784 | * inside) while letting them pass-through. Note that even though touch passes through the occluding | 
|  | 4785 | * window, the occluding window will still receive ACTION_OUTSIDE event. | 
|  | 4786 | */ | 
|  | 4787 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 4788 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { | 
|  | 4789 | const sp<FakeWindowHandle>& w = | 
|  | 4790 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
|  | 4791 | w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH); | 
|  | 4792 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4793 |  | 
|  | 4794 | touch(); | 
|  | 4795 |  | 
|  | 4796 | w->consumeMotionOutside(); | 
|  | 4797 | } | 
|  | 4798 |  | 
|  | 4799 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { | 
|  | 4800 | const sp<FakeWindowHandle>& w = | 
|  | 4801 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
|  | 4802 | w->addFlags(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH); | 
|  | 4803 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4804 |  | 
|  | 4805 | touch(); | 
|  | 4806 |  | 
|  | 4807 | InputEvent* event = w->consume(); | 
|  | 4808 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()); | 
|  | 4809 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 4810 | EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX()); | 
|  | 4811 | EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY()); | 
|  | 4812 | } | 
|  | 4813 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4814 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4815 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4816 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4817 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4818 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4819 |  | 
|  | 4820 | touch(); | 
|  | 4821 |  | 
|  | 4822 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4823 | } | 
|  | 4824 |  | 
|  | 4825 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { | 
|  | 4826 | const sp<FakeWindowHandle>& w = | 
|  | 4827 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4828 | MAXIMUM_OBSCURING_OPACITY); | 
|  | 4829 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4830 |  | 
|  | 4831 | touch(); | 
|  | 4832 |  | 
|  | 4833 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4834 | } | 
|  | 4835 |  | 
|  | 4836 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4837 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4838 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4839 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4840 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4841 |  | 
|  | 4842 | touch(); | 
|  | 4843 |  | 
|  | 4844 | mTouchWindow->assertNoEvents(); | 
|  | 4845 | } | 
|  | 4846 |  | 
|  | 4847 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { | 
|  | 4848 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 | 
|  | 4849 | const sp<FakeWindowHandle>& w1 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4850 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, | 
|  | 4851 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4852 | const sp<FakeWindowHandle>& w2 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4853 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, | 
|  | 4854 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4855 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 4856 |  | 
|  | 4857 | touch(); | 
|  | 4858 |  | 
|  | 4859 | mTouchWindow->assertNoEvents(); | 
|  | 4860 | } | 
|  | 4861 |  | 
|  | 4862 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { | 
|  | 4863 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 | 
|  | 4864 | const sp<FakeWindowHandle>& w1 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4865 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, | 
|  | 4866 | OPACITY_FAR_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4867 | const sp<FakeWindowHandle>& w2 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4868 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, | 
|  | 4869 | OPACITY_FAR_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4870 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 4871 |  | 
|  | 4872 | touch(); | 
|  | 4873 |  | 
|  | 4874 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4875 | } | 
|  | 4876 |  | 
|  | 4877 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 4878 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { | 
|  | 4879 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4880 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4881 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4882 | const sp<FakeWindowHandle>& wC = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4883 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 4884 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4885 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
|  | 4886 |  | 
|  | 4887 | touch(); | 
|  | 4888 |  | 
|  | 4889 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4890 | } | 
|  | 4891 |  | 
|  | 4892 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { | 
|  | 4893 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4894 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4895 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4896 | const sp<FakeWindowHandle>& wC = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4897 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 4898 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 4899 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 4900 |  | 
|  | 4901 | touch(); | 
|  | 4902 |  | 
|  | 4903 | mTouchWindow->assertNoEvents(); | 
|  | 4904 | } | 
|  | 4905 |  | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4906 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 4907 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { | 
|  | 4908 | const sp<FakeWindowHandle>& wA = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4909 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 4910 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4911 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4912 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4913 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4914 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); | 
|  | 4915 |  | 
|  | 4916 | touch(); | 
|  | 4917 |  | 
|  | 4918 | mTouchWindow->assertNoEvents(); | 
|  | 4919 | } | 
|  | 4920 |  | 
|  | 4921 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 4922 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { | 
|  | 4923 | const sp<FakeWindowHandle>& wA = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4924 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 4925 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4926 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4927 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4928 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4929 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); | 
|  | 4930 |  | 
|  | 4931 | touch(); | 
|  | 4932 |  | 
|  | 4933 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4934 | } | 
|  | 4935 |  | 
|  | 4936 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { | 
|  | 4937 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4938 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 4939 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 4940 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4941 |  | 
|  | 4942 | touch(); | 
|  | 4943 |  | 
|  | 4944 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4945 | } | 
|  | 4946 |  | 
|  | 4947 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { | 
|  | 4948 | const sp<FakeWindowHandle>& w = | 
|  | 4949 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 4950 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4951 |  | 
|  | 4952 | touch(); | 
|  | 4953 |  | 
|  | 4954 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4955 | } | 
|  | 4956 |  | 
| Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 4957 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 4958 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { | 
|  | 4959 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); | 
|  | 4960 | const sp<FakeWindowHandle>& w = | 
|  | 4961 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); | 
|  | 4962 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4963 |  | 
|  | 4964 | touch(); | 
|  | 4965 |  | 
|  | 4966 | mTouchWindow->assertNoEvents(); | 
|  | 4967 | } | 
|  | 4968 |  | 
|  | 4969 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { | 
|  | 4970 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); | 
|  | 4971 | const sp<FakeWindowHandle>& w = | 
|  | 4972 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); | 
|  | 4973 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4974 |  | 
|  | 4975 | touch(); | 
|  | 4976 |  | 
|  | 4977 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4978 | } | 
|  | 4979 |  | 
|  | 4980 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 4981 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { | 
|  | 4982 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); | 
|  | 4983 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 4984 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 4985 | OPACITY_ABOVE_THRESHOLD); | 
|  | 4986 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 4987 |  | 
|  | 4988 | touch(); | 
|  | 4989 |  | 
|  | 4990 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 4991 | } | 
|  | 4992 |  | 
|  | 4993 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 4994 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { | 
|  | 4995 | const sp<FakeWindowHandle>& w1 = | 
|  | 4996 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, | 
|  | 4997 | OPACITY_BELOW_THRESHOLD); | 
|  | 4998 | const sp<FakeWindowHandle>& w2 = | 
|  | 4999 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 5000 | OPACITY_BELOW_THRESHOLD); | 
|  | 5001 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 5002 |  | 
|  | 5003 | touch(); | 
|  | 5004 |  | 
|  | 5005 | mTouchWindow->assertNoEvents(); | 
|  | 5006 | } | 
|  | 5007 |  | 
|  | 5008 | /** | 
|  | 5009 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the | 
|  | 5010 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold | 
|  | 5011 | * (which alone would result in allowing touches) does not affect the blocking behavior. | 
|  | 5012 | */ | 
|  | 5013 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5014 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { | 
|  | 5015 | const sp<FakeWindowHandle>& wB = | 
|  | 5016 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, | 
|  | 5017 | OPACITY_BELOW_THRESHOLD); | 
|  | 5018 | const sp<FakeWindowHandle>& wC = | 
|  | 5019 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 5020 | OPACITY_BELOW_THRESHOLD); | 
|  | 5021 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
|  | 5022 |  | 
|  | 5023 | touch(); | 
|  | 5024 |  | 
|  | 5025 | mTouchWindow->assertNoEvents(); | 
|  | 5026 | } | 
|  | 5027 |  | 
|  | 5028 | /** | 
|  | 5029 | * This test is testing that a window from a different UID but with same application token doesn't | 
|  | 5030 | * block the touch. Apps can share the application token for close UI collaboration for example. | 
|  | 5031 | */ | 
|  | 5032 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 5033 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { | 
|  | 5034 | const sp<FakeWindowHandle>& w = | 
|  | 5035 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 5036 | w->setApplicationToken(mTouchWindow->getApplicationToken()); | 
| Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 5037 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 5038 |  | 
|  | 5039 | touch(); | 
|  | 5040 |  | 
|  | 5041 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 5042 | } | 
|  | 5043 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5044 | class InputDispatcherDragTests : public InputDispatcherTest { | 
|  | 5045 | protected: | 
|  | 5046 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 5047 | sp<FakeWindowHandle> mWindow; | 
|  | 5048 | sp<FakeWindowHandle> mSecondWindow; | 
|  | 5049 | sp<FakeWindowHandle> mDragWindow; | 
|  | 5050 |  | 
|  | 5051 | void SetUp() override { | 
|  | 5052 | InputDispatcherTest::SetUp(); | 
|  | 5053 | mApp = std::make_shared<FakeApplicationHandle>(); | 
|  | 5054 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 5055 | mWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 5056 | mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
|  | 5057 |  | 
|  | 5058 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); | 
|  | 5059 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); | 
|  | 5060 | mSecondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
|  | 5061 |  | 
|  | 5062 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
|  | 5063 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); | 
|  | 5064 | } | 
|  | 5065 |  | 
|  | 5066 | // Start performing drag, we will create a drag window and transfer touch to it. | 
|  | 5067 | void performDrag() { | 
|  | 5068 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5069 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5070 | {50, 50})) | 
|  | 5071 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5072 |  | 
|  | 5073 | // Window should receive motion event. | 
|  | 5074 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5075 |  | 
|  | 5076 | // The drag window covers the entire display | 
|  | 5077 | mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); | 
|  | 5078 | mDispatcher->setInputWindows( | 
|  | 5079 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); | 
|  | 5080 |  | 
|  | 5081 | // Transfer touch focus to the drag window | 
|  | 5082 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), | 
|  | 5083 | true /* isDragDrop */); | 
|  | 5084 | mWindow->consumeMotionCancel(); | 
|  | 5085 | mDragWindow->consumeMotionDown(); | 
|  | 5086 | } | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5087 |  | 
|  | 5088 | // Start performing drag, we will create a drag window and transfer touch to it. | 
|  | 5089 | void performStylusDrag() { | 
|  | 5090 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5091 | injectMotionEvent(mDispatcher, | 
|  | 5092 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, | 
|  | 5093 | AINPUT_SOURCE_STYLUS) | 
|  | 5094 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) | 
|  | 5095 | .pointer(PointerBuilder(0, | 
|  | 5096 | AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 5097 | .x(50) | 
|  | 5098 | .y(50)) | 
|  | 5099 | .build())); | 
|  | 5100 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5101 |  | 
|  | 5102 | // The drag window covers the entire display | 
|  | 5103 | mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); | 
|  | 5104 | mDispatcher->setInputWindows( | 
|  | 5105 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); | 
|  | 5106 |  | 
|  | 5107 | // Transfer touch focus to the drag window | 
|  | 5108 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), | 
|  | 5109 | true /* isDragDrop */); | 
|  | 5110 | mWindow->consumeMotionCancel(); | 
|  | 5111 | mDragWindow->consumeMotionDown(); | 
|  | 5112 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5113 | }; | 
|  | 5114 |  | 
|  | 5115 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { | 
|  | 5116 | performDrag(); | 
|  | 5117 |  | 
|  | 5118 | // Move on window. | 
|  | 5119 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5120 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5121 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5122 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5123 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5124 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5125 | mSecondWindow->assertNoEvents(); | 
|  | 5126 |  | 
|  | 5127 | // Move to another window. | 
|  | 5128 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5129 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5130 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 5131 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5132 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5133 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 5134 | mSecondWindow->consumeDragEvent(false, 50, 50); | 
|  | 5135 |  | 
|  | 5136 | // Move back to original window. | 
|  | 5137 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5138 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5139 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5140 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5141 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5142 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5143 | mSecondWindow->consumeDragEvent(true, -50, 50); | 
|  | 5144 |  | 
|  | 5145 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5146 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5147 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5148 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 5149 | mWindow->assertNoEvents(); | 
|  | 5150 | mSecondWindow->assertNoEvents(); | 
|  | 5151 | } | 
|  | 5152 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5153 | TEST_F(InputDispatcherDragTests, DragAndDrop) { | 
|  | 5154 | performDrag(); | 
|  | 5155 |  | 
|  | 5156 | // Move on window. | 
|  | 5157 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5158 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5159 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5160 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5161 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5162 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5163 | mSecondWindow->assertNoEvents(); | 
|  | 5164 |  | 
|  | 5165 | // Move to another window. | 
|  | 5166 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5167 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5168 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 5169 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5170 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5171 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 5172 | mSecondWindow->consumeDragEvent(false, 50, 50); | 
|  | 5173 |  | 
|  | 5174 | // drop to another window. | 
|  | 5175 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5176 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5177 | {150, 50})) | 
|  | 5178 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5179 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 5180 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); | 
|  | 5181 | mWindow->assertNoEvents(); | 
|  | 5182 | mSecondWindow->assertNoEvents(); | 
|  | 5183 | } | 
|  | 5184 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5185 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { | 
|  | 5186 | performStylusDrag(); | 
|  | 5187 |  | 
|  | 5188 | // Move on window and keep button pressed. | 
|  | 5189 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5190 | injectMotionEvent(mDispatcher, | 
|  | 5191 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) | 
|  | 5192 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) | 
|  | 5193 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 5194 | .x(50) | 
|  | 5195 | .y(50)) | 
|  | 5196 | .build())) | 
|  | 5197 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5198 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5199 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5200 | mSecondWindow->assertNoEvents(); | 
|  | 5201 |  | 
|  | 5202 | // Move to another window and release button, expect to drop item. | 
|  | 5203 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5204 | injectMotionEvent(mDispatcher, | 
|  | 5205 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) | 
|  | 5206 | .buttonState(0) | 
|  | 5207 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 5208 | .x(150) | 
|  | 5209 | .y(50)) | 
|  | 5210 | .build())) | 
|  | 5211 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5212 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5213 | mWindow->assertNoEvents(); | 
|  | 5214 | mSecondWindow->assertNoEvents(); | 
|  | 5215 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); | 
|  | 5216 |  | 
|  | 5217 | // nothing to the window. | 
|  | 5218 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5219 | injectMotionEvent(mDispatcher, | 
|  | 5220 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) | 
|  | 5221 | .buttonState(0) | 
|  | 5222 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 5223 | .x(150) | 
|  | 5224 | .y(50)) | 
|  | 5225 | .build())) | 
|  | 5226 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5227 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 5228 | mWindow->assertNoEvents(); | 
|  | 5229 | mSecondWindow->assertNoEvents(); | 
|  | 5230 | } | 
|  | 5231 |  | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 5232 | TEST_F(InputDispatcherDragTests, DragAndDrop_InvalidWindow) { | 
|  | 5233 | performDrag(); | 
|  | 5234 |  | 
|  | 5235 | // Set second window invisible. | 
|  | 5236 | mSecondWindow->setVisible(false); | 
|  | 5237 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); | 
|  | 5238 |  | 
|  | 5239 | // Move on window. | 
|  | 5240 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5241 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5242 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 5243 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5244 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5245 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 5246 | mSecondWindow->assertNoEvents(); | 
|  | 5247 |  | 
|  | 5248 | // Move to another window. | 
|  | 5249 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5250 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5251 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 5252 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5253 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5254 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 5255 | mSecondWindow->assertNoEvents(); | 
|  | 5256 |  | 
|  | 5257 | // drop to another window. | 
|  | 5258 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5259 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5260 | {150, 50})) | 
|  | 5261 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5262 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 5263 | mFakePolicy->assertDropTargetEquals(nullptr); | 
|  | 5264 | mWindow->assertNoEvents(); | 
|  | 5265 | mSecondWindow->assertNoEvents(); | 
|  | 5266 | } | 
|  | 5267 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5268 | } // namespace android::inputdispatcher |