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 | |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 19 | #include <android-base/properties.h> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 20 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 21 | #include <android-base/thread_annotations.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 22 | #include <binder/Binder.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 23 | #include <gtest/gtest.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 24 | #include <input/Input.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 25 | #include <linux/input.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 26 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 27 | #include <cinttypes> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 28 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 29 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 30 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 31 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 32 | using android::base::StringPrintf; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 33 | using android::gui::FocusRequest; |
| 34 | using android::gui::TouchOcclusionMode; |
| 35 | using android::gui::WindowInfo; |
| 36 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 37 | using android::os::InputEventInjectionResult; |
| 38 | using android::os::InputEventInjectionSync; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 39 | using namespace android::flag_operators; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 40 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 41 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 42 | |
| 43 | // An arbitrary time value. |
| 44 | static const nsecs_t ARBITRARY_TIME = 1234; |
| 45 | |
| 46 | // An arbitrary device id. |
| 47 | static const int32_t DEVICE_ID = 1; |
| 48 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 49 | // An arbitrary display id. |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 50 | static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
| 51 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 52 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 53 | // An arbitrary injector pid / uid pair that has permission to inject events. |
| 54 | static const int32_t INJECTOR_PID = 999; |
| 55 | static const int32_t INJECTOR_UID = 1001; |
| 56 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 57 | // An arbitrary pid of the gesture monitor window |
| 58 | static constexpr int32_t MONITOR_PID = 2001; |
| 59 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 60 | struct PointF { |
| 61 | float x; |
| 62 | float y; |
| 63 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 64 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 65 | /** |
| 66 | * Return a DOWN key event with KEYCODE_A. |
| 67 | */ |
| 68 | static KeyEvent getTestKeyEvent() { |
| 69 | KeyEvent event; |
| 70 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 71 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 72 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 73 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 74 | return event; |
| 75 | } |
| 76 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 77 | static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) { |
| 78 | ASSERT_EQ(expectedAction, receivedAction) |
| 79 | << "expected " << MotionEvent::actionToString(expectedAction) << ", got " |
| 80 | << MotionEvent::actionToString(receivedAction); |
| 81 | } |
| 82 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 83 | // --- FakeInputDispatcherPolicy --- |
| 84 | |
| 85 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 86 | InputDispatcherConfiguration mConfig; |
| 87 | |
| 88 | protected: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 89 | virtual ~FakeInputDispatcherPolicy() {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 90 | |
| 91 | public: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 92 | FakeInputDispatcherPolicy() {} |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 93 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 94 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 95 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
| 96 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_KEY); |
| 97 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 98 | |
| 99 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 100 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 101 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 102 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 103 | } |
| 104 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 105 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 106 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
| 107 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_MOTION); |
| 108 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 109 | |
| 110 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 111 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 112 | EXPECT_EQ(motionEvent.getAction(), args.action); |
| 113 | EXPECT_EQ(motionEvent.getX(0), point.x); |
| 114 | EXPECT_EQ(motionEvent.getY(0), point.y); |
| 115 | EXPECT_EQ(motionEvent.getRawX(0), point.x); |
| 116 | EXPECT_EQ(motionEvent.getRawY(0), point.y); |
| 117 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 118 | } |
| 119 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 120 | void assertFilterInputEventWasNotCalled() { |
| 121 | std::scoped_lock lock(mLock); |
| 122 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 123 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 124 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 125 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 126 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 127 | ASSERT_TRUE(mConfigurationChangedTime) |
| 128 | << "Timed out waiting for configuration changed call"; |
| 129 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 130 | mConfigurationChangedTime = std::nullopt; |
| 131 | } |
| 132 | |
| 133 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 134 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 135 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 136 | // 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] | 137 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 138 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 139 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 140 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 141 | mLastNotifySwitch = std::nullopt; |
| 142 | } |
| 143 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 144 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 145 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 146 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 147 | mOnPointerDownToken.clear(); |
| 148 | } |
| 149 | |
| 150 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 151 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 152 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 153 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 156 | // This function must be called soon after the expected ANR timer starts, |
| 157 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 158 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 159 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 160 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
| 161 | std::shared_ptr<InputApplicationHandle> application; |
| 162 | { // acquire lock |
| 163 | std::unique_lock lock(mLock); |
| 164 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 165 | ASSERT_NO_FATAL_FAILURE( |
| 166 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
| 167 | } // release lock |
| 168 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 171 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 172 | const sp<IBinder>& expectedConnectionToken) { |
| 173 | sp<IBinder> connectionToken = getUnresponsiveWindowToken(timeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 174 | ASSERT_EQ(expectedConnectionToken, connectionToken); |
| 175 | } |
| 176 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 177 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedConnectionToken) { |
| 178 | sp<IBinder> connectionToken = getResponsiveWindowToken(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 179 | ASSERT_EQ(expectedConnectionToken, connectionToken); |
| 180 | } |
| 181 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 182 | void assertNotifyMonitorUnresponsiveWasCalled(std::chrono::nanoseconds timeout) { |
| 183 | int32_t pid = getUnresponsiveMonitorPid(timeout); |
| 184 | ASSERT_EQ(MONITOR_PID, pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 185 | } |
| 186 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 187 | void assertNotifyMonitorResponsiveWasCalled() { |
| 188 | int32_t pid = getResponsiveMonitorPid(); |
| 189 | ASSERT_EQ(MONITOR_PID, pid); |
| 190 | } |
| 191 | |
| 192 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 193 | std::unique_lock lock(mLock); |
| 194 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 195 | return getAnrTokenLockedInterruptible(timeout, mAnrWindowTokens, lock); |
| 196 | } |
| 197 | |
| 198 | sp<IBinder> getResponsiveWindowToken() { |
| 199 | std::unique_lock lock(mLock); |
| 200 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 201 | return getAnrTokenLockedInterruptible(0s, mResponsiveWindowTokens, lock); |
| 202 | } |
| 203 | |
| 204 | int32_t getUnresponsiveMonitorPid(std::chrono::nanoseconds timeout) { |
| 205 | std::unique_lock lock(mLock); |
| 206 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 207 | return getAnrTokenLockedInterruptible(timeout, mAnrMonitorPids, lock); |
| 208 | } |
| 209 | |
| 210 | int32_t getResponsiveMonitorPid() { |
| 211 | std::unique_lock lock(mLock); |
| 212 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 213 | return getAnrTokenLockedInterruptible(0s, mResponsiveMonitorPids, lock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 217 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 218 | // first entry from the container and erase it. |
| 219 | template <class T> |
| 220 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 221 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 222 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 223 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 224 | |
| 225 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 226 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 227 | // before checking if ANR was called. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 228 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 229 | // to provide it some time to act. 100ms seems reasonable. |
| 230 | mNotifyAnr.wait_for(lock, timeToWait, |
| 231 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 232 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 233 | if (storage.empty()) { |
| 234 | ADD_FAILURE() << "Did not receive the ANR callback"; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 235 | return {}; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 236 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 237 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 238 | // the dispatcher started counting before this function was called |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 239 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 240 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 241 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 242 | << "ms, but waited " |
| 243 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 244 | << "ms instead"; |
| 245 | } |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 246 | T token = storage.front(); |
| 247 | storage.pop(); |
| 248 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void assertNotifyAnrWasNotCalled() { |
| 252 | std::scoped_lock lock(mLock); |
| 253 | ASSERT_TRUE(mAnrApplications.empty()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 254 | ASSERT_TRUE(mAnrWindowTokens.empty()); |
| 255 | ASSERT_TRUE(mAnrMonitorPids.empty()); |
| 256 | ASSERT_TRUE(mResponsiveWindowTokens.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 257 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 258 | "signal"; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 259 | ASSERT_TRUE(mResponsiveMonitorPids.empty()) |
| 260 | << "Monitor ANR was not called, but please also consume the 'monitor is responsive'" |
| 261 | " signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 264 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 265 | mConfig.keyRepeatTimeout = timeout; |
| 266 | mConfig.keyRepeatDelay = delay; |
| 267 | } |
| 268 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 269 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 270 | std::unique_lock lock(mLock); |
| 271 | base::ScopedLockAssertion assumeLocked(mLock); |
| 272 | |
| 273 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 274 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 275 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 276 | enabled; |
| 277 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 278 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 279 | << ") to be called."; |
| 280 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 281 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 282 | auto request = *mPointerCaptureRequest; |
| 283 | mPointerCaptureRequest.reset(); |
| 284 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void assertSetPointerCaptureNotCalled() { |
| 288 | std::unique_lock lock(mLock); |
| 289 | base::ScopedLockAssertion assumeLocked(mLock); |
| 290 | |
| 291 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 292 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 293 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 294 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 295 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 296 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 297 | } |
| 298 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 299 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { |
| 300 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 301 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 302 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 303 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 304 | } |
| 305 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 306 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 307 | std::mutex mLock; |
| 308 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 309 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 310 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 311 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 312 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 313 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 314 | |
| 315 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 316 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 317 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 318 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 319 | std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock); |
| 320 | std::queue<sp<IBinder>> mResponsiveWindowTokens GUARDED_BY(mLock); |
| 321 | std::queue<int32_t> mAnrMonitorPids GUARDED_BY(mLock); |
| 322 | std::queue<int32_t> mResponsiveMonitorPids GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 323 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 324 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 325 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 326 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 327 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 328 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 329 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 330 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 333 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 334 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 335 | mAnrWindowTokens.push(connectionToken); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 336 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 337 | } |
| 338 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 339 | void notifyMonitorUnresponsive(int32_t pid, const std::string&) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 340 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 341 | mAnrMonitorPids.push(pid); |
| 342 | mNotifyAnr.notify_all(); |
| 343 | } |
| 344 | |
| 345 | void notifyWindowResponsive(const sp<IBinder>& connectionToken) override { |
| 346 | std::scoped_lock lock(mLock); |
| 347 | mResponsiveWindowTokens.push(connectionToken); |
| 348 | mNotifyAnr.notify_all(); |
| 349 | } |
| 350 | |
| 351 | void notifyMonitorResponsive(int32_t pid) override { |
| 352 | std::scoped_lock lock(mLock); |
| 353 | mResponsiveMonitorPids.push(pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 354 | mNotifyAnr.notify_all(); |
| 355 | } |
| 356 | |
| 357 | void notifyNoFocusedWindowAnr( |
| 358 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 359 | std::scoped_lock lock(mLock); |
| 360 | mAnrApplications.push(applicationHandle); |
| 361 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 362 | } |
| 363 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 364 | void notifyInputChannelBroken(const sp<IBinder>&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 365 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 366 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 367 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 368 | void notifyUntrustedTouch(const std::string& obscuringPackage) override {} |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 369 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 370 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 371 | const std::vector<float>& values) override {} |
| 372 | |
| 373 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 374 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 375 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 376 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 377 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 378 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 379 | *outConfig = mConfig; |
| 380 | } |
| 381 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 382 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 383 | std::scoped_lock lock(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 384 | switch (inputEvent->getType()) { |
| 385 | case AINPUT_EVENT_TYPE_KEY: { |
| 386 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 387 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 388 | break; |
| 389 | } |
| 390 | |
| 391 | case AINPUT_EVENT_TYPE_MOTION: { |
| 392 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 393 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 394 | break; |
| 395 | } |
| 396 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 397 | return true; |
| 398 | } |
| 399 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 400 | void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 401 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 402 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 403 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 404 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 405 | return 0; |
| 406 | } |
| 407 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 408 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 409 | return false; |
| 410 | } |
| 411 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 412 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 413 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 414 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 415 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 416 | * essentially a passthrough for notifySwitch. |
| 417 | */ |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 418 | mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 421 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 422 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 423 | bool checkInjectEventsPermissionNonReentrant(int32_t pid, int32_t uid) override { |
| 424 | return pid == INJECTOR_PID && uid == INJECTOR_UID; |
| 425 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 426 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 427 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 428 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 429 | mOnPointerDownToken = newToken; |
| 430 | } |
| 431 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 432 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 433 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 434 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 435 | mPointerCaptureChangedCondition.notify_all(); |
| 436 | } |
| 437 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 438 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 439 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 440 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 441 | mDropTargetWindowToken = token; |
| 442 | } |
| 443 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 444 | void assertFilterInputEventWasCalledInternal( |
| 445 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 446 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 447 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 448 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 449 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 450 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 451 | }; |
| 452 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 453 | // --- InputDispatcherTest --- |
| 454 | |
| 455 | class InputDispatcherTest : public testing::Test { |
| 456 | protected: |
| 457 | sp<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 458 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 459 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 460 | void SetUp() override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 461 | mFakePolicy = new FakeInputDispatcherPolicy(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 462 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 463 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 464 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 465 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 468 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 469 | ASSERT_EQ(OK, mDispatcher->stop()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 470 | mFakePolicy.clear(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 471 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 472 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 473 | |
| 474 | /** |
| 475 | * Used for debugging when writing the test |
| 476 | */ |
| 477 | void dumpDispatcherState() { |
| 478 | std::string dump; |
| 479 | mDispatcher->dump(dump); |
| 480 | std::stringstream ss(dump); |
| 481 | std::string to; |
| 482 | |
| 483 | while (std::getline(ss, to, '\n')) { |
| 484 | ALOGE("%s", to.c_str()); |
| 485 | } |
| 486 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 487 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 488 | void setFocusedWindow(const sp<WindowInfoHandle>& window, |
| 489 | const sp<WindowInfoHandle>& focusedWindow = nullptr) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 490 | FocusRequest request; |
| 491 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 492 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 493 | if (focusedWindow) { |
| 494 | request.focusedToken = focusedWindow->getToken(); |
| 495 | } |
| 496 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 497 | request.displayId = window->getInfo()->displayId; |
| 498 | mDispatcher->setFocusedWindow(request); |
| 499 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 500 | }; |
| 501 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 502 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 503 | KeyEvent event; |
| 504 | |
| 505 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 506 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 507 | INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 508 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 509 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 510 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 511 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 512 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 513 | << "Should reject key events with undefined action."; |
| 514 | |
| 515 | // 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] | 516 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 517 | 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] | 518 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 519 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 520 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 521 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 522 | << "Should reject key events with ACTION_MULTIPLE."; |
| 523 | } |
| 524 | |
| 525 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 526 | MotionEvent event; |
| 527 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 528 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
| 529 | for (int i = 0; i <= MAX_POINTERS; i++) { |
| 530 | pointerProperties[i].clear(); |
| 531 | pointerProperties[i].id = i; |
| 532 | pointerCoords[i].clear(); |
| 533 | } |
| 534 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 535 | // Some constants commonly used below |
| 536 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 537 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 538 | constexpr int32_t metaState = AMETA_NONE; |
| 539 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 540 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 541 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 542 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 543 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 544 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, |
| 545 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 546 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 547 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 548 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 549 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 550 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 551 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 552 | << "Should reject motion events with undefined action."; |
| 553 | |
| 554 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 555 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 556 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 557 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 558 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 559 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 560 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 561 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 562 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 563 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 564 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 565 | << "Should reject motion events with pointer down index too large."; |
| 566 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 567 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 568 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 569 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 570 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 571 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 572 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 573 | /*pointerCount*/ 1, pointerProperties, 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 down index too small."; |
| 578 | |
| 579 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 580 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 581 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 582 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 583 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 584 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 585 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 586 | /*pointerCount*/ 1, pointerProperties, 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 large."; |
| 591 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 592 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 593 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 594 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 595 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 596 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 597 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 598 | /*pointerCount*/ 1, 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 pointer up index too small."; |
| 603 | |
| 604 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 605 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 606 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 607 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 608 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 609 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 610 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 611 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 612 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 613 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 614 | << "Should reject motion events with 0 pointers."; |
| 615 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 616 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 617 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 618 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 619 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 620 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 621 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 622 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 623 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 624 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 625 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 626 | |
| 627 | // Rejects motion events with invalid pointer ids. |
| 628 | pointerProperties[0].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, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 632 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 633 | 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 less than 0."; |
| 639 | |
| 640 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 641 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 642 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 643 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 644 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 645 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 646 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 647 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 648 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 649 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 650 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 651 | |
| 652 | // Rejects motion events with duplicate pointer ids. |
| 653 | pointerProperties[0].id = 1; |
| 654 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 655 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 656 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 657 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 658 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 659 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 660 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 661 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 662 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 663 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 664 | << "Should reject motion events with duplicate pointer ids."; |
| 665 | } |
| 666 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 667 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 668 | |
| 669 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 670 | constexpr nsecs_t eventTime = 20; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 671 | NotifyConfigurationChangedArgs args(10 /*id*/, eventTime); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 672 | mDispatcher->notifyConfigurationChanged(&args); |
| 673 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 674 | |
| 675 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 676 | } |
| 677 | |
| 678 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 679 | NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/, |
| 680 | 2 /*switchMask*/); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 681 | mDispatcher->notifySwitch(&args); |
| 682 | |
| 683 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 684 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 685 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 686 | } |
| 687 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 688 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 689 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 690 | // Default input dispatching timeout if there is no focused application or paused window |
| 691 | // from which to determine an appropriate dispatching timeout. |
| 692 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 693 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 694 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 695 | |
| 696 | class FakeApplicationHandle : public InputApplicationHandle { |
| 697 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 698 | FakeApplicationHandle() { |
| 699 | mInfo.name = "Fake Application"; |
| 700 | mInfo.token = new BBinder(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 701 | mInfo.dispatchingTimeoutMillis = |
| 702 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 703 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 704 | virtual ~FakeApplicationHandle() {} |
| 705 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 706 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 707 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 708 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 709 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 710 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 711 | }; |
| 712 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 713 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 714 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 715 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 716 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 717 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 718 | } |
| 719 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 720 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 721 | InputEvent* event; |
| 722 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 723 | if (!consumeSeq) { |
| 724 | return nullptr; |
| 725 | } |
| 726 | finishEvent(*consumeSeq); |
| 727 | return event; |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * Receive an event without acknowledging it. |
| 732 | * Return the sequence number that could later be used to send finished signal. |
| 733 | */ |
| 734 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 735 | uint32_t consumeSeq; |
| 736 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 737 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 738 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 739 | status_t status = WOULD_BLOCK; |
| 740 | while (status == WOULD_BLOCK) { |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 741 | status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 742 | &event); |
| 743 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 744 | if (elapsed > 100ms) { |
| 745 | break; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | if (status == WOULD_BLOCK) { |
| 750 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 751 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | if (status != OK) { |
| 755 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 756 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 757 | } |
| 758 | if (event == nullptr) { |
| 759 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 760 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 761 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 762 | if (outEvent != nullptr) { |
| 763 | *outEvent = event; |
| 764 | } |
| 765 | return consumeSeq; |
| 766 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 767 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 768 | /** |
| 769 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 770 | */ |
| 771 | void finishEvent(uint32_t consumeSeq) { |
| 772 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 773 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 774 | } |
| 775 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 776 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 777 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); |
| 778 | ASSERT_EQ(OK, status); |
| 779 | } |
| 780 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 781 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 782 | std::optional<int32_t> expectedDisplayId, |
| 783 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 784 | InputEvent* event = consume(); |
| 785 | |
| 786 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 787 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 788 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 789 | << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType) |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 790 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 791 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 792 | if (expectedDisplayId.has_value()) { |
| 793 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 794 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 795 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 796 | switch (expectedEventType) { |
| 797 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 798 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 799 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 800 | if (expectedFlags.has_value()) { |
| 801 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 802 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 803 | break; |
| 804 | } |
| 805 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 806 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 807 | assertMotionAction(expectedAction, motionEvent.getAction()); |
| 808 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 809 | if (expectedFlags.has_value()) { |
| 810 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 811 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 812 | break; |
| 813 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 814 | case AINPUT_EVENT_TYPE_FOCUS: { |
| 815 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 816 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 817 | case AINPUT_EVENT_TYPE_CAPTURE: { |
| 818 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 819 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 820 | case AINPUT_EVENT_TYPE_DRAG: { |
| 821 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 822 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 823 | default: { |
| 824 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; |
| 825 | } |
| 826 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 827 | } |
| 828 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 829 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 830 | InputEvent* event = consume(); |
| 831 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 832 | << ": consumer should have returned non-NULL event."; |
| 833 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType()) |
| 834 | << "Got " << inputEventTypeToString(event->getType()) |
| 835 | << " event instead of FOCUS event"; |
| 836 | |
| 837 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 838 | << mName.c_str() << ": event displayId should always be NONE."; |
| 839 | |
| 840 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 841 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
| 842 | EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode()); |
| 843 | } |
| 844 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 845 | void consumeCaptureEvent(bool hasCapture) { |
| 846 | const InputEvent* event = consume(); |
| 847 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 848 | << ": consumer should have returned non-NULL event."; |
| 849 | ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType()) |
| 850 | << "Got " << inputEventTypeToString(event->getType()) |
| 851 | << " event instead of CAPTURE event"; |
| 852 | |
| 853 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 854 | << mName.c_str() << ": event displayId should always be NONE."; |
| 855 | |
| 856 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 857 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 858 | } |
| 859 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 860 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 861 | const InputEvent* event = consume(); |
| 862 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 863 | << ": consumer should have returned non-NULL event."; |
| 864 | ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType()) |
| 865 | << "Got " << inputEventTypeToString(event->getType()) |
| 866 | << " event instead of DRAG event"; |
| 867 | |
| 868 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 869 | << mName.c_str() << ": event displayId should always be NONE."; |
| 870 | |
| 871 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 872 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 873 | EXPECT_EQ(x, dragEvent.getX()); |
| 874 | EXPECT_EQ(y, dragEvent.getY()); |
| 875 | } |
| 876 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 877 | void assertNoEvents() { |
| 878 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 879 | if (event == nullptr) { |
| 880 | return; |
| 881 | } |
| 882 | if (event->getType() == AINPUT_EVENT_TYPE_KEY) { |
| 883 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 884 | ADD_FAILURE() << "Received key event " |
| 885 | << KeyEvent::actionToString(keyEvent.getAction()); |
| 886 | } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) { |
| 887 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 888 | ADD_FAILURE() << "Received motion event " |
| 889 | << MotionEvent::actionToString(motionEvent.getAction()); |
| 890 | } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) { |
| 891 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 892 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 893 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 894 | } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) { |
| 895 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 896 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 897 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 898 | } |
| 899 | FAIL() << mName.c_str() |
| 900 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 904 | |
| 905 | protected: |
| 906 | std::unique_ptr<InputConsumer> mConsumer; |
| 907 | PreallocatedInputEventFactory mEventFactory; |
| 908 | |
| 909 | std::string mName; |
| 910 | }; |
| 911 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 912 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 913 | public: |
| 914 | static const int32_t WIDTH = 600; |
| 915 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 916 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 917 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 918 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 919 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 920 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 921 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 922 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 923 | dispatcher->createInputChannel(name); |
| 924 | token = (*channel)->getConnectionToken(); |
| 925 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | inputApplicationHandle->updateInfo(); |
| 929 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 930 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 931 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 932 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 933 | mInfo.name = name; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 934 | mInfo.type = WindowInfo::Type::APPLICATION; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 935 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 936 | mInfo.alpha = 1.0; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 937 | mInfo.frameLeft = 0; |
| 938 | mInfo.frameTop = 0; |
| 939 | mInfo.frameRight = WIDTH; |
| 940 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 941 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 942 | mInfo.globalScaleFactor = 1.0; |
| 943 | mInfo.touchableRegion.clear(); |
| 944 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
| 945 | mInfo.visible = true; |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 946 | mInfo.focusable = false; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 947 | mInfo.hasWallpaper = false; |
| 948 | mInfo.paused = false; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 949 | mInfo.ownerPid = INJECTOR_PID; |
| 950 | mInfo.ownerUid = INJECTOR_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 951 | mInfo.displayId = displayId; |
| 952 | } |
| 953 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 954 | sp<FakeWindowHandle> clone( |
| 955 | const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 956 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 957 | sp<FakeWindowHandle> handle = |
| 958 | new FakeWindowHandle(inputApplicationHandle, dispatcher, mInfo.name + "(Mirror)", |
| 959 | displayId, mInfo.token); |
| 960 | return handle; |
| 961 | } |
| 962 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 963 | void setFocusable(bool focusable) { mInfo.focusable = focusable; } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 964 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 965 | void setVisible(bool visible) { mInfo.visible = visible; } |
| 966 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 967 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 968 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 971 | void setPaused(bool paused) { mInfo.paused = paused; } |
| 972 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 973 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 974 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 975 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 976 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 977 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 978 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame^] | 979 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 980 | mInfo.frameLeft = frame.left; |
| 981 | mInfo.frameTop = frame.top; |
| 982 | mInfo.frameRight = frame.right; |
| 983 | mInfo.frameBottom = frame.bottom; |
| 984 | mInfo.touchableRegion.clear(); |
| 985 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame^] | 986 | |
| 987 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 988 | ui::Transform translate; |
| 989 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 990 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 991 | } |
| 992 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 993 | void setType(WindowInfo::Type type) { mInfo.type = type; } |
| 994 | |
| 995 | void setHasWallpaper(bool hasWallpaper) { mInfo.hasWallpaper = hasWallpaper; } |
| 996 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 997 | void addFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags |= flags; } |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 998 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 999 | void setFlags(Flags<WindowInfo::Flag> flags) { mInfo.flags = flags; } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1000 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1001 | void setInputFeatures(WindowInfo::Feature features) { mInfo.inputFeatures = features; } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1002 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1003 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1004 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1005 | } |
| 1006 | |
| 1007 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1008 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1009 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1010 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1011 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1012 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1013 | expectedFlags); |
| 1014 | } |
| 1015 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1016 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1017 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
| 1018 | } |
| 1019 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1020 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1021 | int32_t expectedFlags = 0) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1022 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId, |
| 1023 | expectedFlags); |
| 1024 | } |
| 1025 | |
| 1026 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1027 | int32_t expectedFlags = 0) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1028 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId, |
| 1029 | expectedFlags); |
| 1030 | } |
| 1031 | |
| 1032 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1033 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1034 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1035 | } |
| 1036 | |
| 1037 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1038 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1039 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1040 | expectedFlags); |
| 1041 | } |
| 1042 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1043 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1044 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1045 | int32_t expectedFlags = 0) { |
| 1046 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1047 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1048 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1049 | } |
| 1050 | |
| 1051 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1052 | int32_t expectedFlags = 0) { |
| 1053 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1054 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1055 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1056 | } |
| 1057 | |
| 1058 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1059 | int32_t expectedFlags = 0) { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1060 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
| 1061 | expectedFlags); |
| 1062 | } |
| 1063 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1064 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1065 | int32_t expectedFlags = 0) { |
| 1066 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
| 1067 | expectedFlags); |
| 1068 | } |
| 1069 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1070 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1071 | ASSERT_NE(mInputReceiver, nullptr) |
| 1072 | << "Cannot consume events from a window with no receiver"; |
| 1073 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1074 | } |
| 1075 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1076 | void consumeCaptureEvent(bool hasCapture) { |
| 1077 | ASSERT_NE(mInputReceiver, nullptr) |
| 1078 | << "Cannot consume events from a window with no receiver"; |
| 1079 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1080 | } |
| 1081 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1082 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 1083 | std::optional<int32_t> expectedDisplayId, |
| 1084 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1085 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1086 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1087 | expectedFlags); |
| 1088 | } |
| 1089 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1090 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1091 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1092 | } |
| 1093 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1094 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1095 | if (mInputReceiver == nullptr) { |
| 1096 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1097 | return std::nullopt; |
| 1098 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1099 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | void finishEvent(uint32_t sequenceNum) { |
| 1103 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1104 | mInputReceiver->finishEvent(sequenceNum); |
| 1105 | } |
| 1106 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1107 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1108 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1109 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1110 | } |
| 1111 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1112 | InputEvent* consume() { |
| 1113 | if (mInputReceiver == nullptr) { |
| 1114 | return nullptr; |
| 1115 | } |
| 1116 | return mInputReceiver->consume(); |
| 1117 | } |
| 1118 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1119 | MotionEvent* consumeMotion() { |
| 1120 | InputEvent* event = consume(); |
| 1121 | if (event == nullptr) { |
| 1122 | ADD_FAILURE() << "Consume failed : no event"; |
| 1123 | return nullptr; |
| 1124 | } |
| 1125 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 1126 | ADD_FAILURE() << "Instead of motion event, got " |
| 1127 | << inputEventTypeToString(event->getType()); |
| 1128 | return nullptr; |
| 1129 | } |
| 1130 | return static_cast<MotionEvent*>(event); |
| 1131 | } |
| 1132 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1133 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1134 | if (mInputReceiver == nullptr && |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1135 | mInfo.inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1136 | return; // Can't receive events if the window does not have input channel |
| 1137 | } |
| 1138 | ASSERT_NE(nullptr, mInputReceiver) |
| 1139 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1140 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1141 | } |
| 1142 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1143 | sp<IBinder> getToken() { return mInfo.token; } |
| 1144 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1145 | const std::string& getName() { return mName; } |
| 1146 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1147 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { |
| 1148 | mInfo.ownerPid = ownerPid; |
| 1149 | mInfo.ownerUid = ownerUid; |
| 1150 | } |
| 1151 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1152 | private: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1153 | const std::string mName; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1154 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1155 | 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] | 1156 | }; |
| 1157 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1158 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1159 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1160 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1161 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1162 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1163 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1164 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
| 1165 | bool allowKeyRepeat = true) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1166 | KeyEvent event; |
| 1167 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1168 | |
| 1169 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1170 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1171 | INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE, |
| 1172 | repeatCount, currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1173 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1174 | int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 1175 | if (!allowKeyRepeat) { |
| 1176 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1177 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1178 | // Inject event until dispatch out. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1179 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1180 | injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1181 | } |
| 1182 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1183 | static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1184 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1185 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId); |
| 1186 | } |
| 1187 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1188 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1189 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1190 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1191 | static InputEventInjectionResult injectKeyDownNoRepeat( |
| 1192 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1193 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId, |
| 1194 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
| 1195 | /* allowKeyRepeat */ false); |
| 1196 | } |
| 1197 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1198 | static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1199 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1200 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId); |
| 1201 | } |
| 1202 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1203 | class PointerBuilder { |
| 1204 | public: |
| 1205 | PointerBuilder(int32_t id, int32_t toolType) { |
| 1206 | mProperties.clear(); |
| 1207 | mProperties.id = id; |
| 1208 | mProperties.toolType = toolType; |
| 1209 | mCoords.clear(); |
| 1210 | } |
| 1211 | |
| 1212 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } |
| 1213 | |
| 1214 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } |
| 1215 | |
| 1216 | PointerBuilder& axis(int32_t axis, float value) { |
| 1217 | mCoords.setAxisValue(axis, value); |
| 1218 | return *this; |
| 1219 | } |
| 1220 | |
| 1221 | PointerProperties buildProperties() const { return mProperties; } |
| 1222 | |
| 1223 | PointerCoords buildCoords() const { return mCoords; } |
| 1224 | |
| 1225 | private: |
| 1226 | PointerProperties mProperties; |
| 1227 | PointerCoords mCoords; |
| 1228 | }; |
| 1229 | |
| 1230 | class MotionEventBuilder { |
| 1231 | public: |
| 1232 | MotionEventBuilder(int32_t action, int32_t source) { |
| 1233 | mAction = action; |
| 1234 | mSource = source; |
| 1235 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1236 | } |
| 1237 | |
| 1238 | MotionEventBuilder& eventTime(nsecs_t eventTime) { |
| 1239 | mEventTime = eventTime; |
| 1240 | return *this; |
| 1241 | } |
| 1242 | |
| 1243 | MotionEventBuilder& displayId(int32_t displayId) { |
| 1244 | mDisplayId = displayId; |
| 1245 | return *this; |
| 1246 | } |
| 1247 | |
| 1248 | MotionEventBuilder& actionButton(int32_t actionButton) { |
| 1249 | mActionButton = actionButton; |
| 1250 | return *this; |
| 1251 | } |
| 1252 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1253 | MotionEventBuilder& buttonState(int32_t buttonState) { |
| 1254 | mButtonState = buttonState; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1255 | return *this; |
| 1256 | } |
| 1257 | |
| 1258 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 1259 | mRawXCursorPosition = rawXCursorPosition; |
| 1260 | return *this; |
| 1261 | } |
| 1262 | |
| 1263 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 1264 | mRawYCursorPosition = rawYCursorPosition; |
| 1265 | return *this; |
| 1266 | } |
| 1267 | |
| 1268 | MotionEventBuilder& pointer(PointerBuilder pointer) { |
| 1269 | mPointers.push_back(pointer); |
| 1270 | return *this; |
| 1271 | } |
| 1272 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1273 | MotionEventBuilder& addFlag(uint32_t flags) { |
| 1274 | mFlags |= flags; |
| 1275 | return *this; |
| 1276 | } |
| 1277 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1278 | MotionEvent build() { |
| 1279 | std::vector<PointerProperties> pointerProperties; |
| 1280 | std::vector<PointerCoords> pointerCoords; |
| 1281 | for (const PointerBuilder& pointer : mPointers) { |
| 1282 | pointerProperties.push_back(pointer.buildProperties()); |
| 1283 | pointerCoords.push_back(pointer.buildCoords()); |
| 1284 | } |
| 1285 | |
| 1286 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 1287 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 1288 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 1289 | mPointers.size() == 1) { |
| 1290 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 1291 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 1292 | } |
| 1293 | |
| 1294 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1295 | ui::Transform identityTransform; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1296 | event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC, |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1297 | mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1298 | mButtonState, MotionClassification::NONE, identityTransform, |
| 1299 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 1300 | mRawYCursorPosition, identityTransform, mEventTime, mEventTime, |
| 1301 | mPointers.size(), pointerProperties.data(), pointerCoords.data()); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1302 | |
| 1303 | return event; |
| 1304 | } |
| 1305 | |
| 1306 | private: |
| 1307 | int32_t mAction; |
| 1308 | int32_t mSource; |
| 1309 | nsecs_t mEventTime; |
| 1310 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1311 | int32_t mActionButton{0}; |
| 1312 | int32_t mButtonState{0}; |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1313 | int32_t mFlags{0}; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1314 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1315 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1316 | |
| 1317 | std::vector<PointerBuilder> mPointers; |
| 1318 | }; |
| 1319 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1320 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1321 | const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1322 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1323 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1324 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode, |
| 1325 | injectionTimeout, |
| 1326 | POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
| 1327 | } |
| 1328 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1329 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1330 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source, |
| 1331 | int32_t displayId, const PointF& position, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1332 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1333 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1334 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1335 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1336 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1337 | MotionEvent event = MotionEventBuilder(action, source) |
| 1338 | .displayId(displayId) |
| 1339 | .eventTime(eventTime) |
| 1340 | .rawXCursorPosition(cursorPosition.x) |
| 1341 | .rawYCursorPosition(cursorPosition.y) |
| 1342 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1343 | .x(position.x) |
| 1344 | .y(position.y)) |
| 1345 | .build(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1346 | |
| 1347 | // Inject event until dispatch out. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1348 | return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1349 | } |
| 1350 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1351 | static InputEventInjectionResult injectMotionDown( |
| 1352 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId, |
| 1353 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1354 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1357 | static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1358 | int32_t source, int32_t displayId, |
| 1359 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1360 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1363 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1364 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1365 | // Define a valid key event. |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1366 | NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 1367 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, |
| 1368 | KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1369 | |
| 1370 | return args; |
| 1371 | } |
| 1372 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1373 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, |
| 1374 | const std::vector<PointF>& points) { |
| 1375 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1376 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1377 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1378 | } |
| 1379 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1380 | PointerProperties pointerProperties[pointerCount]; |
| 1381 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1382 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1383 | for (size_t i = 0; i < pointerCount; i++) { |
| 1384 | pointerProperties[i].clear(); |
| 1385 | pointerProperties[i].id = i; |
| 1386 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1387 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1388 | pointerCoords[i].clear(); |
| 1389 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1390 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1391 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1392 | |
| 1393 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1394 | // Define a valid motion event. |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1395 | NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1396 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 1397 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1398 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 1399 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1400 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 1401 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1402 | |
| 1403 | return args; |
| 1404 | } |
| 1405 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1406 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1407 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1408 | } |
| 1409 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1410 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1411 | const PointerCaptureRequest& request) { |
| 1412 | return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1413 | } |
| 1414 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1415 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1416 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1417 | sp<FakeWindowHandle> window = |
| 1418 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1419 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1420 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1421 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1422 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1423 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1424 | |
| 1425 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1426 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1427 | } |
| 1428 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame^] | 1429 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1430 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1431 | sp<FakeWindowHandle> window = |
| 1432 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1433 | |
| 1434 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1435 | // Inject a MotionEvent to an unknown display. |
| 1436 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1437 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
| 1438 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1439 | |
| 1440 | // Window should receive motion event. |
| 1441 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1442 | } |
| 1443 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1444 | /** |
| 1445 | * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues. |
| 1446 | * To ensure that window receives only events that were directly inside of it, add |
| 1447 | * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input |
| 1448 | * when finding touched windows. |
| 1449 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1450 | * called twice. |
| 1451 | */ |
| 1452 | TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1453 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1454 | sp<FakeWindowHandle> window = |
| 1455 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1456 | window->setFrame(Rect(0, 0, 100, 100)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1457 | window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1458 | |
| 1459 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1460 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1461 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1462 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1463 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1464 | |
| 1465 | // Window should receive motion event. |
| 1466 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1467 | } |
| 1468 | |
| 1469 | /** |
| 1470 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
| 1471 | * To ensure that window receives only events that were directly inside of it, add |
| 1472 | * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input |
| 1473 | * when finding touched windows. |
| 1474 | */ |
| 1475 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1476 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1477 | sp<FakeWindowHandle> window = |
| 1478 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1479 | window->setFrame(Rect(0, 0, 100, 100)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1480 | window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1481 | |
| 1482 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1483 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1484 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1485 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1486 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1487 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1488 | |
| 1489 | // Window should receive motion event. |
| 1490 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1491 | } |
| 1492 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1493 | // The foreground window should receive the first touch down event. |
| 1494 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1495 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1496 | sp<FakeWindowHandle> windowTop = |
| 1497 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 1498 | sp<FakeWindowHandle> windowSecond = |
| 1499 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1500 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1501 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1502 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1503 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1504 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1505 | |
| 1506 | // 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] | 1507 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1508 | windowSecond->assertNoEvents(); |
| 1509 | } |
| 1510 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1511 | /** |
| 1512 | * Two windows: A top window, and a wallpaper behind the window. |
| 1513 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1514 | * gets ACTION_CANCEL. |
| 1515 | * 1. foregroundWindow <-- has wallpaper (hasWallpaper=true) |
| 1516 | * 2. wallpaperWindow <-- is wallpaper (type=InputWindowInfo::Type::WALLPAPER) |
| 1517 | */ |
| 1518 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1519 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1520 | sp<FakeWindowHandle> foregroundWindow = |
| 1521 | new FakeWindowHandle(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 1522 | foregroundWindow->setHasWallpaper(true); |
| 1523 | sp<FakeWindowHandle> wallpaperWindow = |
| 1524 | new FakeWindowHandle(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 1525 | wallpaperWindow->setType(WindowInfo::Type::WALLPAPER); |
| 1526 | constexpr int expectedWallpaperFlags = |
| 1527 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1528 | |
| 1529 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1530 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1531 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1532 | {100, 200})) |
| 1533 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1534 | |
| 1535 | // Both foreground window and its wallpaper should receive the touch down |
| 1536 | foregroundWindow->consumeMotionDown(); |
| 1537 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1538 | |
| 1539 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1540 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1541 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1542 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1543 | |
| 1544 | foregroundWindow->consumeMotionMove(); |
| 1545 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1546 | |
| 1547 | // Now the foreground window goes away, but the wallpaper stays |
| 1548 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1549 | foregroundWindow->consumeMotionCancel(); |
| 1550 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1551 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1552 | } |
| 1553 | |
| 1554 | /** |
| 1555 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1556 | * The top window gets a multitouch gesture. |
| 1557 | * Ensure that wallpaper gets the same gesture. |
| 1558 | */ |
| 1559 | TEST_F(InputDispatcherTest, WallpaperWindow_ReceivesMultiTouch) { |
| 1560 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1561 | sp<FakeWindowHandle> window = |
| 1562 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 1563 | window->setHasWallpaper(true); |
| 1564 | |
| 1565 | sp<FakeWindowHandle> wallpaperWindow = |
| 1566 | new FakeWindowHandle(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 1567 | wallpaperWindow->setType(WindowInfo::Type::WALLPAPER); |
| 1568 | constexpr int expectedWallpaperFlags = |
| 1569 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1570 | |
| 1571 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, wallpaperWindow}}}); |
| 1572 | |
| 1573 | // Touch down on top window |
| 1574 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1575 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1576 | {100, 100})) |
| 1577 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1578 | |
| 1579 | // Both top window and its wallpaper should receive the touch down |
| 1580 | window->consumeMotionDown(); |
| 1581 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1582 | |
| 1583 | // Second finger down on the top window |
| 1584 | const MotionEvent secondFingerDownEvent = |
| 1585 | MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1586 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1587 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1588 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1589 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1590 | .x(100) |
| 1591 | .y(100)) |
| 1592 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1593 | .x(150) |
| 1594 | .y(150)) |
| 1595 | .build(); |
| 1596 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1597 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1598 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1599 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1600 | |
| 1601 | window->consumeMotionPointerDown(1 /* pointerIndex */); |
| 1602 | wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT, |
| 1603 | expectedWallpaperFlags); |
| 1604 | window->assertNoEvents(); |
| 1605 | wallpaperWindow->assertNoEvents(); |
| 1606 | } |
| 1607 | |
| 1608 | /** |
| 1609 | * Two windows: a window on the left and window on the right. |
| 1610 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1611 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1612 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1613 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1614 | * ACTION_POINTER_DOWN(1). |
| 1615 | */ |
| 1616 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1617 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1618 | sp<FakeWindowHandle> leftWindow = |
| 1619 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1620 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1621 | leftWindow->setFlags(WindowInfo::Flag::SPLIT_TOUCH | WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 1622 | leftWindow->setHasWallpaper(true); |
| 1623 | |
| 1624 | sp<FakeWindowHandle> rightWindow = |
| 1625 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1626 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 1627 | rightWindow->setFlags(WindowInfo::Flag::SPLIT_TOUCH | WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 1628 | rightWindow->setHasWallpaper(true); |
| 1629 | |
| 1630 | sp<FakeWindowHandle> wallpaperWindow = |
| 1631 | new FakeWindowHandle(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 1632 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
| 1633 | wallpaperWindow->setType(WindowInfo::Type::WALLPAPER); |
| 1634 | constexpr int expectedWallpaperFlags = |
| 1635 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1636 | |
| 1637 | mDispatcher->setInputWindows( |
| 1638 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
| 1639 | |
| 1640 | // Touch down on left window |
| 1641 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1642 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1643 | {100, 100})) |
| 1644 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1645 | |
| 1646 | // Both foreground window and its wallpaper should receive the touch down |
| 1647 | leftWindow->consumeMotionDown(); |
| 1648 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1649 | |
| 1650 | // Second finger down on the right window |
| 1651 | const MotionEvent secondFingerDownEvent = |
| 1652 | MotionEventBuilder(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1653 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1654 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1655 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1656 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1657 | .x(100) |
| 1658 | .y(100)) |
| 1659 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1660 | .x(300) |
| 1661 | .y(100)) |
| 1662 | .build(); |
| 1663 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1664 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1665 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1666 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1667 | |
| 1668 | leftWindow->consumeMotionMove(); |
| 1669 | // Since the touch is split, right window gets ACTION_DOWN |
| 1670 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1671 | wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT, |
| 1672 | expectedWallpaperFlags); |
| 1673 | |
| 1674 | // Now, leftWindow, which received the first finger, disappears. |
| 1675 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); |
| 1676 | leftWindow->consumeMotionCancel(); |
| 1677 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1678 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1679 | |
| 1680 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 1681 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 1682 | const MotionEvent secondFingerMoveEvent = |
| 1683 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1684 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1685 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1686 | .x(100) |
| 1687 | .y(100)) |
| 1688 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1689 | .x(310) |
| 1690 | .y(110)) |
| 1691 | .build(); |
| 1692 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1693 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 1694 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 1695 | rightWindow->consumeMotionMove(); |
| 1696 | |
| 1697 | leftWindow->assertNoEvents(); |
| 1698 | rightWindow->assertNoEvents(); |
| 1699 | wallpaperWindow->assertNoEvents(); |
| 1700 | } |
| 1701 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1702 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1703 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1704 | sp<FakeWindowHandle> windowLeft = |
| 1705 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1706 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1707 | windowLeft->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1708 | sp<FakeWindowHandle> windowRight = |
| 1709 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1710 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1711 | windowRight->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1712 | |
| 1713 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1714 | |
| 1715 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 1716 | |
| 1717 | // 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] | 1718 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1719 | injectMotionEvent(mDispatcher, |
| 1720 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1721 | AINPUT_SOURCE_MOUSE) |
| 1722 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1723 | .x(900) |
| 1724 | .y(400)) |
| 1725 | .build())); |
| 1726 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1727 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1728 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1729 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1730 | |
| 1731 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1732 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1733 | injectMotionEvent(mDispatcher, |
| 1734 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1735 | AINPUT_SOURCE_MOUSE) |
| 1736 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1737 | .x(300) |
| 1738 | .y(400)) |
| 1739 | .build())); |
| 1740 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1741 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1742 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1743 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1744 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1745 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1746 | |
| 1747 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1748 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1749 | injectMotionEvent(mDispatcher, |
| 1750 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 1751 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1752 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1753 | .x(300) |
| 1754 | .y(400)) |
| 1755 | .build())); |
| 1756 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1757 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1758 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1759 | injectMotionEvent(mDispatcher, |
| 1760 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1761 | AINPUT_SOURCE_MOUSE) |
| 1762 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1763 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1764 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1765 | .x(300) |
| 1766 | .y(400)) |
| 1767 | .build())); |
| 1768 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1769 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1770 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1771 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1772 | injectMotionEvent(mDispatcher, |
| 1773 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1774 | AINPUT_SOURCE_MOUSE) |
| 1775 | .buttonState(0) |
| 1776 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1777 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1778 | .x(300) |
| 1779 | .y(400)) |
| 1780 | .build())); |
| 1781 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1782 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1783 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1784 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1785 | injectMotionEvent(mDispatcher, |
| 1786 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 1787 | .buttonState(0) |
| 1788 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1789 | .x(300) |
| 1790 | .y(400)) |
| 1791 | .build())); |
| 1792 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1793 | |
| 1794 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1795 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1796 | injectMotionEvent(mDispatcher, |
| 1797 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1798 | AINPUT_SOURCE_MOUSE) |
| 1799 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1800 | .x(900) |
| 1801 | .y(400)) |
| 1802 | .build())); |
| 1803 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1804 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1805 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1806 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1807 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1808 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1809 | } |
| 1810 | |
| 1811 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 1812 | // directly in this test. |
| 1813 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1814 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1815 | sp<FakeWindowHandle> window = |
| 1816 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1817 | window->setFrame(Rect(0, 0, 1200, 800)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1818 | window->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1819 | |
| 1820 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1821 | |
| 1822 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1823 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1824 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1825 | injectMotionEvent(mDispatcher, |
| 1826 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1827 | AINPUT_SOURCE_MOUSE) |
| 1828 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1829 | .x(300) |
| 1830 | .y(400)) |
| 1831 | .build())); |
| 1832 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1833 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1834 | |
| 1835 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1836 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1837 | injectMotionEvent(mDispatcher, |
| 1838 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 1839 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1840 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1841 | .x(300) |
| 1842 | .y(400)) |
| 1843 | .build())); |
| 1844 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1845 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1846 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1847 | injectMotionEvent(mDispatcher, |
| 1848 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1849 | AINPUT_SOURCE_MOUSE) |
| 1850 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1851 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1852 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1853 | .x(300) |
| 1854 | .y(400)) |
| 1855 | .build())); |
| 1856 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1857 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1858 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1859 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1860 | injectMotionEvent(mDispatcher, |
| 1861 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1862 | AINPUT_SOURCE_MOUSE) |
| 1863 | .buttonState(0) |
| 1864 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1865 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1866 | .x(300) |
| 1867 | .y(400)) |
| 1868 | .build())); |
| 1869 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1870 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1871 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1872 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1873 | injectMotionEvent(mDispatcher, |
| 1874 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 1875 | .buttonState(0) |
| 1876 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1877 | .x(300) |
| 1878 | .y(400)) |
| 1879 | .build())); |
| 1880 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1881 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1882 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1883 | injectMotionEvent(mDispatcher, |
| 1884 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1885 | AINPUT_SOURCE_MOUSE) |
| 1886 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1887 | .x(300) |
| 1888 | .y(400)) |
| 1889 | .build())); |
| 1890 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1891 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1892 | } |
| 1893 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1894 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1895 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1896 | |
| 1897 | sp<FakeWindowHandle> windowLeft = |
| 1898 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1899 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1900 | windowLeft->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1901 | sp<FakeWindowHandle> windowRight = |
| 1902 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1903 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1904 | windowRight->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1905 | |
| 1906 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1907 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1908 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1909 | |
| 1910 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 1911 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1912 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1913 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1914 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1915 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1916 | windowRight->assertNoEvents(); |
| 1917 | } |
| 1918 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1919 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1920 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1921 | sp<FakeWindowHandle> window = |
| 1922 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1923 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1924 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1925 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1926 | setFocusedWindow(window); |
| 1927 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1928 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1929 | |
| 1930 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1931 | mDispatcher->notifyKey(&keyArgs); |
| 1932 | |
| 1933 | // Window should receive key down event. |
| 1934 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 1935 | |
| 1936 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 1937 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1938 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1939 | mDispatcher->notifyDeviceReset(&args); |
| 1940 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 1941 | AKEY_EVENT_FLAG_CANCELED); |
| 1942 | } |
| 1943 | |
| 1944 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1945 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1946 | sp<FakeWindowHandle> window = |
| 1947 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1948 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1949 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1950 | |
| 1951 | NotifyMotionArgs motionArgs = |
| 1952 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1953 | ADISPLAY_ID_DEFAULT); |
| 1954 | mDispatcher->notifyMotion(&motionArgs); |
| 1955 | |
| 1956 | // Window should receive motion down event. |
| 1957 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1958 | |
| 1959 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 1960 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1961 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1962 | mDispatcher->notifyDeviceReset(&args); |
| 1963 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, |
| 1964 | 0 /*expectedFlags*/); |
| 1965 | } |
| 1966 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame^] | 1967 | /** |
| 1968 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 1969 | * |
| 1970 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 1971 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 1972 | * space. |
| 1973 | */ |
| 1974 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 1975 | public: |
| 1976 | void SetUp() override { |
| 1977 | InputDispatcherTest::SetUp(); |
| 1978 | mDisplayInfos.clear(); |
| 1979 | mWindowInfos.clear(); |
| 1980 | } |
| 1981 | |
| 1982 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 1983 | gui::DisplayInfo info; |
| 1984 | info.displayId = displayId; |
| 1985 | info.transform = transform; |
| 1986 | mDisplayInfos.push_back(std::move(info)); |
| 1987 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 1988 | } |
| 1989 | |
| 1990 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 1991 | mWindowInfos.push_back(*windowHandle->getInfo()); |
| 1992 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 1993 | } |
| 1994 | |
| 1995 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 1996 | // on the display. |
| 1997 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 1998 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 1999 | // respectively. |
| 2000 | ui::Transform displayTransform; |
| 2001 | displayTransform.set(2, 0, 0, 4); |
| 2002 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 2003 | |
| 2004 | std::shared_ptr<FakeApplicationHandle> application = |
| 2005 | std::make_shared<FakeApplicationHandle>(); |
| 2006 | |
| 2007 | // Add two windows to the display. Their frames are represented in the display space. |
| 2008 | sp<FakeWindowHandle> firstWindow = |
| 2009 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 2010 | firstWindow->addFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 2011 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 2012 | addWindow(firstWindow); |
| 2013 | |
| 2014 | sp<FakeWindowHandle> secondWindow = |
| 2015 | new FakeWindowHandle(application, mDispatcher, "Second Window", |
| 2016 | ADISPLAY_ID_DEFAULT); |
| 2017 | secondWindow->addFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 2018 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 2019 | addWindow(secondWindow); |
| 2020 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 2021 | } |
| 2022 | |
| 2023 | private: |
| 2024 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 2025 | std::vector<gui::WindowInfo> mWindowInfos; |
| 2026 | }; |
| 2027 | |
| 2028 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) { |
| 2029 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2030 | // Send down to the first window. The point is represented in the display space. The point is |
| 2031 | // selected so that if the hit test was done with the transform applied to it, then it would |
| 2032 | // end up in the incorrect window. |
| 2033 | NotifyMotionArgs downMotionArgs = |
| 2034 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2035 | ADISPLAY_ID_DEFAULT, {PointF{75, 55}}); |
| 2036 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2037 | |
| 2038 | firstWindow->consumeMotionDown(); |
| 2039 | secondWindow->assertNoEvents(); |
| 2040 | } |
| 2041 | |
| 2042 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 2043 | // the event should be treated as being in the logical display space. |
| 2044 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 2045 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2046 | // Send down to the first window. The point is represented in the logical display space. The |
| 2047 | // point is selected so that if the hit test was done in logical display space, then it would |
| 2048 | // end up in the incorrect window. |
| 2049 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2050 | PointF{75 * 2, 55 * 4}); |
| 2051 | |
| 2052 | firstWindow->consumeMotionDown(); |
| 2053 | secondWindow->assertNoEvents(); |
| 2054 | } |
| 2055 | |
| 2056 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 2057 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2058 | |
| 2059 | // Send down to the second window. |
| 2060 | NotifyMotionArgs downMotionArgs = |
| 2061 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2062 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}}); |
| 2063 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2064 | |
| 2065 | firstWindow->assertNoEvents(); |
| 2066 | const MotionEvent* event = secondWindow->consumeMotion(); |
| 2067 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 2068 | |
| 2069 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 2070 | EXPECT_EQ(300, event->getRawX(0)); |
| 2071 | EXPECT_EQ(880, event->getRawY(0)); |
| 2072 | |
| 2073 | // Ensure that the x and y values are in the window's coordinate space. |
| 2074 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 2075 | // the logical display space. This will be the origin of the window space. |
| 2076 | EXPECT_EQ(100, event->getX(0)); |
| 2077 | EXPECT_EQ(80, event->getY(0)); |
| 2078 | } |
| 2079 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2080 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 2081 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2082 | |
| 2083 | class TransferTouchFixture : public InputDispatcherTest, |
| 2084 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 2085 | |
| 2086 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2087 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2088 | |
| 2089 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2090 | sp<FakeWindowHandle> firstWindow = |
| 2091 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 2092 | sp<FakeWindowHandle> secondWindow = |
| 2093 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2094 | |
| 2095 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2096 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2097 | |
| 2098 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2099 | NotifyMotionArgs downMotionArgs = |
| 2100 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2101 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2102 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2103 | // Only the first window should get the down event |
| 2104 | firstWindow->consumeMotionDown(); |
| 2105 | secondWindow->assertNoEvents(); |
| 2106 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2107 | // Transfer touch to the second window |
| 2108 | TransferFunction f = GetParam(); |
| 2109 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 2110 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2111 | // The first window gets cancel and the second gets down |
| 2112 | firstWindow->consumeMotionCancel(); |
| 2113 | secondWindow->consumeMotionDown(); |
| 2114 | |
| 2115 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2116 | NotifyMotionArgs upMotionArgs = |
| 2117 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2118 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2119 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2120 | // The first window gets no events and the second gets up |
| 2121 | firstWindow->assertNoEvents(); |
| 2122 | secondWindow->consumeMotionUp(); |
| 2123 | } |
| 2124 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2125 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2126 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2127 | |
| 2128 | PointF touchPoint = {10, 10}; |
| 2129 | |
| 2130 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2131 | sp<FakeWindowHandle> firstWindow = |
| 2132 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 2133 | sp<FakeWindowHandle> secondWindow = |
| 2134 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2135 | |
| 2136 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2137 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2138 | |
| 2139 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2140 | NotifyMotionArgs downMotionArgs = |
| 2141 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2142 | ADISPLAY_ID_DEFAULT, {touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2143 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2144 | // Only the first window should get the down event |
| 2145 | firstWindow->consumeMotionDown(); |
| 2146 | secondWindow->assertNoEvents(); |
| 2147 | |
| 2148 | // Send pointer down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2149 | NotifyMotionArgs pointerDownMotionArgs = |
| 2150 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 2151 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 2152 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2153 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2154 | mDispatcher->notifyMotion(&pointerDownMotionArgs); |
| 2155 | // Only the first window should get the pointer down event |
| 2156 | firstWindow->consumeMotionPointerDown(1); |
| 2157 | secondWindow->assertNoEvents(); |
| 2158 | |
| 2159 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2160 | TransferFunction f = GetParam(); |
| 2161 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 2162 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2163 | // The first window gets cancel and the second gets down and pointer down |
| 2164 | firstWindow->consumeMotionCancel(); |
| 2165 | secondWindow->consumeMotionDown(); |
| 2166 | secondWindow->consumeMotionPointerDown(1); |
| 2167 | |
| 2168 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2169 | NotifyMotionArgs pointerUpMotionArgs = |
| 2170 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | |
| 2171 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 2172 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2173 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2174 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 2175 | // The first window gets nothing and the second gets pointer up |
| 2176 | firstWindow->assertNoEvents(); |
| 2177 | secondWindow->consumeMotionPointerUp(1); |
| 2178 | |
| 2179 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2180 | NotifyMotionArgs upMotionArgs = |
| 2181 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2182 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2183 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2184 | // The first window gets nothing and the second gets up |
| 2185 | firstWindow->assertNoEvents(); |
| 2186 | secondWindow->consumeMotionUp(); |
| 2187 | } |
| 2188 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2189 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 2190 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 2191 | // for the case where there are multiple pointers split across several windows. |
| 2192 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 2193 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2194 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 2195 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2196 | return dispatcher->transferTouch(destChannelToken); |
| 2197 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2198 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 2199 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2200 | return dispatcher->transferTouchFocus(from, to, |
| 2201 | false /*isDragAndDrop*/); |
| 2202 | })); |
| 2203 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2204 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2205 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2206 | |
| 2207 | // Create a non touch modal window that supports split touch |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2208 | sp<FakeWindowHandle> firstWindow = |
| 2209 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2210 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2211 | firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2212 | |
| 2213 | // Create a non touch modal window that supports split touch |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2214 | sp<FakeWindowHandle> secondWindow = |
| 2215 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2216 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2217 | secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2218 | |
| 2219 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2220 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2221 | |
| 2222 | PointF pointInFirst = {300, 200}; |
| 2223 | PointF pointInSecond = {300, 600}; |
| 2224 | |
| 2225 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2226 | NotifyMotionArgs firstDownMotionArgs = |
| 2227 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2228 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2229 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 2230 | // Only the first window should get the down event |
| 2231 | firstWindow->consumeMotionDown(); |
| 2232 | secondWindow->assertNoEvents(); |
| 2233 | |
| 2234 | // Send down to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2235 | NotifyMotionArgs secondDownMotionArgs = |
| 2236 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 2237 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 2238 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2239 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2240 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 2241 | // The first window gets a move and the second a down |
| 2242 | firstWindow->consumeMotionMove(); |
| 2243 | secondWindow->consumeMotionDown(); |
| 2244 | |
| 2245 | // Transfer touch focus to the second window |
| 2246 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 2247 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 2248 | firstWindow->consumeMotionCancel(); |
| 2249 | secondWindow->consumeMotionPointerDown(1); |
| 2250 | |
| 2251 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2252 | NotifyMotionArgs pointerUpMotionArgs = |
| 2253 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | |
| 2254 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 2255 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2256 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2257 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 2258 | // The first window gets nothing and the second gets pointer up |
| 2259 | firstWindow->assertNoEvents(); |
| 2260 | secondWindow->consumeMotionPointerUp(1); |
| 2261 | |
| 2262 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2263 | NotifyMotionArgs upMotionArgs = |
| 2264 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2265 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2266 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2267 | // The first window gets nothing and the second gets up |
| 2268 | firstWindow->assertNoEvents(); |
| 2269 | secondWindow->consumeMotionUp(); |
| 2270 | } |
| 2271 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2272 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 2273 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 2274 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 2275 | // window should get nothing. |
| 2276 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 2277 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2278 | |
| 2279 | // Create a non touch modal window that supports split touch |
| 2280 | sp<FakeWindowHandle> firstWindow = |
| 2281 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 2282 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2283 | firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2284 | |
| 2285 | // Create a non touch modal window that supports split touch |
| 2286 | sp<FakeWindowHandle> secondWindow = |
| 2287 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
| 2288 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2289 | secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2290 | |
| 2291 | // Add the windows to the dispatcher |
| 2292 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 2293 | |
| 2294 | PointF pointInFirst = {300, 200}; |
| 2295 | PointF pointInSecond = {300, 600}; |
| 2296 | |
| 2297 | // Send down to the first window |
| 2298 | NotifyMotionArgs firstDownMotionArgs = |
| 2299 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2300 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 2301 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 2302 | // Only the first window should get the down event |
| 2303 | firstWindow->consumeMotionDown(); |
| 2304 | secondWindow->assertNoEvents(); |
| 2305 | |
| 2306 | // Send down to the second window |
| 2307 | NotifyMotionArgs secondDownMotionArgs = |
| 2308 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 2309 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 2310 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2311 | {pointInFirst, pointInSecond}); |
| 2312 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 2313 | // The first window gets a move and the second a down |
| 2314 | firstWindow->consumeMotionMove(); |
| 2315 | secondWindow->consumeMotionDown(); |
| 2316 | |
| 2317 | // Transfer touch focus to the second window |
| 2318 | const bool transferred = mDispatcher->transferTouch(secondWindow->getToken()); |
| 2319 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 2320 | ASSERT_FALSE(transferred); |
| 2321 | firstWindow->assertNoEvents(); |
| 2322 | secondWindow->assertNoEvents(); |
| 2323 | |
| 2324 | // The rest of the dispatch should proceed as normal |
| 2325 | // Send pointer up to the second window |
| 2326 | NotifyMotionArgs pointerUpMotionArgs = |
| 2327 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | |
| 2328 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 2329 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2330 | {pointInFirst, pointInSecond}); |
| 2331 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 2332 | // The first window gets MOVE and the second gets pointer up |
| 2333 | firstWindow->consumeMotionMove(); |
| 2334 | secondWindow->consumeMotionUp(); |
| 2335 | |
| 2336 | // Send up event to the first window |
| 2337 | NotifyMotionArgs upMotionArgs = |
| 2338 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2339 | ADISPLAY_ID_DEFAULT); |
| 2340 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2341 | // The first window gets nothing and the second gets up |
| 2342 | firstWindow->consumeMotionUp(); |
| 2343 | secondWindow->assertNoEvents(); |
| 2344 | } |
| 2345 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2346 | // This case will create two windows and one mirrored window on the default display and mirror |
| 2347 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 2348 | // the windows info of second display before default display. |
| 2349 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 2350 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2351 | sp<FakeWindowHandle> firstWindowInPrimary = |
| 2352 | new FakeWindowHandle(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
| 2353 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
| 2354 | firstWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 2355 | sp<FakeWindowHandle> secondWindowInPrimary = |
| 2356 | new FakeWindowHandle(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
| 2357 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
| 2358 | secondWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 2359 | |
| 2360 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 2361 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 2362 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
| 2363 | mirrorWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 2364 | |
| 2365 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 2366 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 2367 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
| 2368 | firstWindowInSecondary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 2369 | |
| 2370 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 2371 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 2372 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
| 2373 | secondWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 2374 | |
| 2375 | // Update window info, let it find window handle of second display first. |
| 2376 | mDispatcher->setInputWindows( |
| 2377 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 2378 | {ADISPLAY_ID_DEFAULT, |
| 2379 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 2380 | |
| 2381 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2382 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2383 | {50, 50})) |
| 2384 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2385 | |
| 2386 | // Window should receive motion event. |
| 2387 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2388 | |
| 2389 | // Transfer touch focus |
| 2390 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 2391 | secondWindowInPrimary->getToken())); |
| 2392 | // The first window gets cancel. |
| 2393 | firstWindowInPrimary->consumeMotionCancel(); |
| 2394 | secondWindowInPrimary->consumeMotionDown(); |
| 2395 | |
| 2396 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2397 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2398 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 2399 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2400 | firstWindowInPrimary->assertNoEvents(); |
| 2401 | secondWindowInPrimary->consumeMotionMove(); |
| 2402 | |
| 2403 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2404 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2405 | {150, 50})) |
| 2406 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2407 | firstWindowInPrimary->assertNoEvents(); |
| 2408 | secondWindowInPrimary->consumeMotionUp(); |
| 2409 | } |
| 2410 | |
| 2411 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 2412 | // 'transferTouch' api. |
| 2413 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 2414 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2415 | sp<FakeWindowHandle> firstWindowInPrimary = |
| 2416 | new FakeWindowHandle(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
| 2417 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
| 2418 | firstWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 2419 | sp<FakeWindowHandle> secondWindowInPrimary = |
| 2420 | new FakeWindowHandle(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
| 2421 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
| 2422 | secondWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 2423 | |
| 2424 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 2425 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 2426 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
| 2427 | mirrorWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 2428 | |
| 2429 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 2430 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 2431 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
| 2432 | firstWindowInSecondary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 2433 | |
| 2434 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 2435 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 2436 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
| 2437 | secondWindowInPrimary->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
| 2438 | |
| 2439 | // Update window info, let it find window handle of second display first. |
| 2440 | mDispatcher->setInputWindows( |
| 2441 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 2442 | {ADISPLAY_ID_DEFAULT, |
| 2443 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 2444 | |
| 2445 | // Touch on second display. |
| 2446 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2447 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) |
| 2448 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2449 | |
| 2450 | // Window should receive motion event. |
| 2451 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 2452 | |
| 2453 | // Transfer touch focus |
| 2454 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken())); |
| 2455 | |
| 2456 | // The first window gets cancel. |
| 2457 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 2458 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 2459 | |
| 2460 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2461 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2462 | SECOND_DISPLAY_ID, {150, 50})) |
| 2463 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2464 | firstWindowInPrimary->assertNoEvents(); |
| 2465 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 2466 | |
| 2467 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2468 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
| 2469 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2470 | firstWindowInPrimary->assertNoEvents(); |
| 2471 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 2472 | } |
| 2473 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2474 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2475 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2476 | sp<FakeWindowHandle> window = |
| 2477 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2478 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2479 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2480 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2481 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2482 | |
| 2483 | window->consumeFocusEvent(true); |
| 2484 | |
| 2485 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2486 | mDispatcher->notifyKey(&keyArgs); |
| 2487 | |
| 2488 | // Window should receive key down event. |
| 2489 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2490 | } |
| 2491 | |
| 2492 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2493 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2494 | sp<FakeWindowHandle> window = |
| 2495 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2496 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2497 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2498 | |
| 2499 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2500 | mDispatcher->notifyKey(&keyArgs); |
| 2501 | mDispatcher->waitForIdle(); |
| 2502 | |
| 2503 | window->assertNoEvents(); |
| 2504 | } |
| 2505 | |
| 2506 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 2507 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2508 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2509 | sp<FakeWindowHandle> window = |
| 2510 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2511 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2512 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2513 | |
| 2514 | // Send key |
| 2515 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2516 | mDispatcher->notifyKey(&keyArgs); |
| 2517 | // Send motion |
| 2518 | NotifyMotionArgs motionArgs = |
| 2519 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2520 | ADISPLAY_ID_DEFAULT); |
| 2521 | mDispatcher->notifyMotion(&motionArgs); |
| 2522 | |
| 2523 | // Window should receive only the motion event |
| 2524 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2525 | window->assertNoEvents(); // Key event or focus event will not be received |
| 2526 | } |
| 2527 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2528 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 2529 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2530 | |
| 2531 | // Create first non touch modal window that supports split touch |
| 2532 | sp<FakeWindowHandle> firstWindow = |
| 2533 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 2534 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2535 | firstWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2536 | |
| 2537 | // Create second non touch modal window that supports split touch |
| 2538 | sp<FakeWindowHandle> secondWindow = |
| 2539 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
| 2540 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2541 | secondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2542 | |
| 2543 | // Add the windows to the dispatcher |
| 2544 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 2545 | |
| 2546 | PointF pointInFirst = {300, 200}; |
| 2547 | PointF pointInSecond = {300, 600}; |
| 2548 | |
| 2549 | // Send down to the first window |
| 2550 | NotifyMotionArgs firstDownMotionArgs = |
| 2551 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2552 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 2553 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 2554 | // Only the first window should get the down event |
| 2555 | firstWindow->consumeMotionDown(); |
| 2556 | secondWindow->assertNoEvents(); |
| 2557 | |
| 2558 | // Send down to the second window |
| 2559 | NotifyMotionArgs secondDownMotionArgs = |
| 2560 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 2561 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 2562 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2563 | {pointInFirst, pointInSecond}); |
| 2564 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 2565 | // The first window gets a move and the second a down |
| 2566 | firstWindow->consumeMotionMove(); |
| 2567 | secondWindow->consumeMotionDown(); |
| 2568 | |
| 2569 | // Send pointer cancel to the second window |
| 2570 | NotifyMotionArgs pointerUpMotionArgs = |
| 2571 | generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP | |
| 2572 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 2573 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2574 | {pointInFirst, pointInSecond}); |
| 2575 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 2576 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 2577 | // The first window gets move and the second gets cancel. |
| 2578 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 2579 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 2580 | |
| 2581 | // Send up event. |
| 2582 | NotifyMotionArgs upMotionArgs = |
| 2583 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2584 | ADISPLAY_ID_DEFAULT); |
| 2585 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2586 | // The first window gets up and the second gets nothing. |
| 2587 | firstWindow->consumeMotionUp(); |
| 2588 | secondWindow->assertNoEvents(); |
| 2589 | } |
| 2590 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 2591 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 2592 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2593 | |
| 2594 | sp<FakeWindowHandle> window = |
| 2595 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2596 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2597 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 2598 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 2599 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 2600 | |
| 2601 | window->sendTimeline(1 /*inputEventId*/, graphicsTimeline); |
| 2602 | window->assertNoEvents(); |
| 2603 | mDispatcher->waitForIdle(); |
| 2604 | } |
| 2605 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2606 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2607 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2608 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2609 | int32_t displayId, bool isGestureMonitor = false) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 2610 | base::Result<std::unique_ptr<InputChannel>> channel = |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 2611 | dispatcher->createInputMonitor(displayId, isGestureMonitor, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 2612 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2615 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 2616 | |
| 2617 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 2618 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, |
| 2619 | expectedDisplayId, expectedFlags); |
| 2620 | } |
| 2621 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2622 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 2623 | |
| 2624 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 2625 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2626 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 2627 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 2628 | expectedDisplayId, expectedFlags); |
| 2629 | } |
| 2630 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2631 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 2632 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 2633 | expectedDisplayId, expectedFlags); |
| 2634 | } |
| 2635 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2636 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 2637 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, |
| 2638 | expectedDisplayId, expectedFlags); |
| 2639 | } |
| 2640 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2641 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 2642 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 2643 | expectedDisplayId, expectedFlags); |
| 2644 | } |
| 2645 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2646 | MotionEvent* consumeMotion() { |
| 2647 | InputEvent* event = mInputReceiver->consume(); |
| 2648 | if (!event) { |
| 2649 | ADD_FAILURE() << "No event was produced"; |
| 2650 | return nullptr; |
| 2651 | } |
| 2652 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 2653 | ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion"; |
| 2654 | return nullptr; |
| 2655 | } |
| 2656 | return static_cast<MotionEvent*>(event); |
| 2657 | } |
| 2658 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2659 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 2660 | |
| 2661 | private: |
| 2662 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2663 | }; |
| 2664 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2665 | /** |
| 2666 | * Two entities that receive touch: A window, and a global monitor. |
| 2667 | * The touch goes to the window, and then the window disappears. |
| 2668 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 2669 | * for the monitor, as well. |
| 2670 | * 1. foregroundWindow |
| 2671 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 2672 | */ |
| 2673 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_GlobalMonitorTouchIsCanceled) { |
| 2674 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2675 | sp<FakeWindowHandle> window = |
| 2676 | new FakeWindowHandle(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 2677 | |
| 2678 | FakeMonitorReceiver monitor = |
| 2679 | FakeMonitorReceiver(mDispatcher, "GlobalMonitor", ADISPLAY_ID_DEFAULT, |
| 2680 | false /*isGestureMonitor*/); |
| 2681 | |
| 2682 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2683 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2684 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2685 | {100, 200})) |
| 2686 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2687 | |
| 2688 | // Both the foreground window and the global monitor should receive the touch down |
| 2689 | window->consumeMotionDown(); |
| 2690 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2691 | |
| 2692 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2693 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2694 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 2695 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2696 | |
| 2697 | window->consumeMotionMove(); |
| 2698 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 2699 | |
| 2700 | // Now the foreground window goes away |
| 2701 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 2702 | window->consumeMotionCancel(); |
| 2703 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 2704 | |
| 2705 | // If more events come in, there will be no more foreground window to send them to. This will |
| 2706 | // cause a cancel for the monitor, as well. |
| 2707 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 2708 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2709 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 2710 | << "Injection should fail because the window was removed"; |
| 2711 | window->assertNoEvents(); |
| 2712 | // Global monitor now gets the cancel |
| 2713 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 2714 | } |
| 2715 | |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2716 | // Tests for gesture monitors |
| 2717 | TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2718 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2719 | sp<FakeWindowHandle> window = |
| 2720 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2721 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2722 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2723 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 2724 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2725 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2726 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2727 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2728 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2729 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2730 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2734 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2735 | sp<FakeWindowHandle> window = |
| 2736 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2737 | |
| 2738 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2739 | window->setFocusable(true); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2740 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2741 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2742 | setFocusedWindow(window); |
| 2743 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2744 | window->consumeFocusEvent(true); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2745 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2746 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 2747 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2748 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2749 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 2750 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2751 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2752 | monitor.assertNoEvents(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
| 2755 | TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2756 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2757 | sp<FakeWindowHandle> window = |
| 2758 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2759 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2760 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2761 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 2762 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2763 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2764 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2765 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2766 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2767 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2768 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2769 | |
| 2770 | window->releaseChannel(); |
| 2771 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2772 | mDispatcher->pilferPointers(monitor.getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2773 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2774 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2775 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2776 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2777 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2780 | TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) { |
| 2781 | FakeMonitorReceiver monitor = |
| 2782 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, |
| 2783 | true /*isGestureMonitor*/); |
| 2784 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2785 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2786 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
| 2787 | std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 2788 | ASSERT_TRUE(consumeSeq); |
| 2789 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 2790 | mFakePolicy->assertNotifyMonitorUnresponsiveWasCalled(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2791 | monitor.finishEvent(*consumeSeq); |
| 2792 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 2793 | mFakePolicy->assertNotifyMonitorResponsiveWasCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2794 | } |
| 2795 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 2796 | // Tests for gesture monitors |
| 2797 | TEST_F(InputDispatcherTest, GestureMonitor_NoWindowTransform) { |
| 2798 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2799 | sp<FakeWindowHandle> window = |
| 2800 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2801 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2802 | window->setWindowOffset(20, 40); |
| 2803 | window->setWindowTransform(0, 1, -1, 0); |
| 2804 | |
| 2805 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 2806 | true /*isGestureMonitor*/); |
| 2807 | |
| 2808 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2809 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 2810 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2811 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2812 | MotionEvent* event = monitor.consumeMotion(); |
| 2813 | // Even though window has transform, gesture monitor must not. |
| 2814 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 2815 | } |
| 2816 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2817 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2818 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2819 | sp<FakeWindowHandle> window = |
| 2820 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2821 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2822 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2823 | |
| 2824 | NotifyMotionArgs motionArgs = |
| 2825 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2826 | ADISPLAY_ID_DEFAULT); |
| 2827 | |
| 2828 | mDispatcher->notifyMotion(&motionArgs); |
| 2829 | // Window should receive motion down event. |
| 2830 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2831 | |
| 2832 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2833 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 2834 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2835 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 2836 | motionArgs.pointerCoords[0].getX() - 10); |
| 2837 | |
| 2838 | mDispatcher->notifyMotion(&motionArgs); |
| 2839 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
| 2840 | 0 /*expectedFlags*/); |
| 2841 | } |
| 2842 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2843 | /** |
| 2844 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 2845 | * the device default right away. In the test scenario, we check both the default value, |
| 2846 | * and the action of enabling / disabling. |
| 2847 | */ |
| 2848 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2849 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2850 | sp<FakeWindowHandle> window = |
| 2851 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 2852 | |
| 2853 | // Set focused application. |
| 2854 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2855 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2856 | |
| 2857 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2858 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2859 | setFocusedWindow(window); |
| 2860 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2861 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 2862 | |
| 2863 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2864 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2865 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2866 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); |
| 2867 | |
| 2868 | SCOPED_TRACE("Disable touch mode"); |
| 2869 | mDispatcher->setInTouchMode(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2870 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2871 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2872 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2873 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); |
| 2874 | |
| 2875 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2876 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2877 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2878 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); |
| 2879 | |
| 2880 | SCOPED_TRACE("Enable touch mode again"); |
| 2881 | mDispatcher->setInTouchMode(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2882 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2883 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2884 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2885 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 2886 | |
| 2887 | window->assertNoEvents(); |
| 2888 | } |
| 2889 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2890 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2891 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2892 | sp<FakeWindowHandle> window = |
| 2893 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 2894 | |
| 2895 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2896 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2897 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2898 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2899 | setFocusedWindow(window); |
| 2900 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2901 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 2902 | |
| 2903 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 2904 | mDispatcher->notifyKey(&keyArgs); |
| 2905 | |
| 2906 | InputEvent* event = window->consume(); |
| 2907 | ASSERT_NE(event, nullptr); |
| 2908 | |
| 2909 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 2910 | ASSERT_NE(verified, nullptr); |
| 2911 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 2912 | |
| 2913 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 2914 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 2915 | ASSERT_EQ(keyArgs.source, verified->source); |
| 2916 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 2917 | |
| 2918 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 2919 | |
| 2920 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
| 2921 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 2922 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
| 2923 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 2924 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 2925 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 2926 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 2927 | } |
| 2928 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2929 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2930 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2931 | sp<FakeWindowHandle> window = |
| 2932 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 2933 | |
| 2934 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2935 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 2936 | ui::Transform transform; |
| 2937 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 2938 | |
| 2939 | gui::DisplayInfo displayInfo; |
| 2940 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 2941 | displayInfo.transform = transform; |
| 2942 | |
| 2943 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2944 | |
| 2945 | NotifyMotionArgs motionArgs = |
| 2946 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2947 | ADISPLAY_ID_DEFAULT); |
| 2948 | mDispatcher->notifyMotion(&motionArgs); |
| 2949 | |
| 2950 | InputEvent* event = window->consume(); |
| 2951 | ASSERT_NE(event, nullptr); |
| 2952 | |
| 2953 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 2954 | ASSERT_NE(verified, nullptr); |
| 2955 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 2956 | |
| 2957 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 2958 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 2959 | EXPECT_EQ(motionArgs.source, verified->source); |
| 2960 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 2961 | |
| 2962 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 2963 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 2964 | const vec2 rawXY = |
| 2965 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 2966 | motionArgs.pointerCoords[0].getXYValue()); |
| 2967 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 2968 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 2969 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
| 2970 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
| 2971 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
| 2972 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 2973 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 2974 | } |
| 2975 | |
Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 2976 | TEST_F(InputDispatcherTest, NonPointerMotionEvent_JoystickAndTouchpadNotTransformed) { |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 2977 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2978 | sp<FakeWindowHandle> window = |
| 2979 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 2980 | const std::string name = window->getName(); |
| 2981 | |
| 2982 | // Window gets transformed by offset values. |
| 2983 | window->setWindowOffset(500.0f, 500.0f); |
| 2984 | |
| 2985 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2986 | window->setFocusable(true); |
| 2987 | |
| 2988 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2989 | |
| 2990 | // First, we set focused window so that focusedWindowHandle is not null. |
| 2991 | setFocusedWindow(window); |
| 2992 | |
| 2993 | // Second, we consume focus event if it is right or wrong according to onFocusChangedLocked. |
| 2994 | window->consumeFocusEvent(true); |
| 2995 | |
Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 2996 | constexpr const std::array nonTransformedSources = {std::pair(AINPUT_SOURCE_TOUCHPAD, |
| 2997 | AMOTION_EVENT_ACTION_DOWN), |
| 2998 | std::pair(AINPUT_SOURCE_JOYSTICK, |
| 2999 | AMOTION_EVENT_ACTION_MOVE)}; |
| 3000 | for (const auto& [source, action] : nonTransformedSources) { |
| 3001 | const NotifyMotionArgs motionArgs = generateMotionArgs(action, source, ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 3002 | mDispatcher->notifyMotion(&motionArgs); |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 3003 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3004 | MotionEvent* event = window->consumeMotion(); |
Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 3005 | ASSERT_NE(event, nullptr); |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 3006 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3007 | const MotionEvent& motionEvent = *event; |
Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 3008 | EXPECT_EQ(action, motionEvent.getAction()); |
Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 3009 | EXPECT_EQ(motionArgs.pointerCount, motionEvent.getPointerCount()); |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 3010 | |
Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 3011 | float expectedX = motionArgs.pointerCoords[0].getX(); |
| 3012 | float expectedY = motionArgs.pointerCoords[0].getY(); |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 3013 | |
Prabir Pradhan | bd52771 | 2021-03-09 19:17:09 -0800 | [diff] [blame] | 3014 | // Ensure the axis values from the final motion event are not transformed. |
| 3015 | EXPECT_EQ(expectedX, motionEvent.getX(0)) |
| 3016 | << "expected " << expectedX << " for x coord of " << name.c_str() << ", got " |
| 3017 | << motionEvent.getX(0); |
| 3018 | EXPECT_EQ(expectedY, motionEvent.getY(0)) |
| 3019 | << "expected " << expectedY << " for y coord of " << name.c_str() << ", got " |
| 3020 | << motionEvent.getY(0); |
| 3021 | // Ensure the raw and transformed axis values for the motion event are the same. |
| 3022 | EXPECT_EQ(motionEvent.getRawX(0), motionEvent.getX(0)) |
| 3023 | << "expected raw and transformed X-axis values to be equal"; |
| 3024 | EXPECT_EQ(motionEvent.getRawY(0), motionEvent.getY(0)) |
| 3025 | << "expected raw and transformed Y-axis values to be equal"; |
| 3026 | } |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 3027 | } |
| 3028 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3029 | /** |
| 3030 | * Ensure that separate calls to sign the same data are generating the same key. |
| 3031 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 3032 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 3033 | * tests. |
| 3034 | */ |
| 3035 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 3036 | KeyEvent event = getTestKeyEvent(); |
| 3037 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 3038 | |
| 3039 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 3040 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 3041 | ASSERT_EQ(hmac1, hmac2); |
| 3042 | } |
| 3043 | |
| 3044 | /** |
| 3045 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 3046 | */ |
| 3047 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 3048 | KeyEvent event = getTestKeyEvent(); |
| 3049 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 3050 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 3051 | |
| 3052 | verifiedEvent.deviceId += 1; |
| 3053 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3054 | |
| 3055 | verifiedEvent.source += 1; |
| 3056 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3057 | |
| 3058 | verifiedEvent.eventTimeNanos += 1; |
| 3059 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3060 | |
| 3061 | verifiedEvent.displayId += 1; |
| 3062 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3063 | |
| 3064 | verifiedEvent.action += 1; |
| 3065 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3066 | |
| 3067 | verifiedEvent.downTimeNanos += 1; |
| 3068 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3069 | |
| 3070 | verifiedEvent.flags += 1; |
| 3071 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3072 | |
| 3073 | verifiedEvent.keyCode += 1; |
| 3074 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3075 | |
| 3076 | verifiedEvent.scanCode += 1; |
| 3077 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3078 | |
| 3079 | verifiedEvent.metaState += 1; |
| 3080 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3081 | |
| 3082 | verifiedEvent.repeatCount += 1; |
| 3083 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3084 | } |
| 3085 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3086 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 3087 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3088 | sp<FakeWindowHandle> windowTop = |
| 3089 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 3090 | sp<FakeWindowHandle> windowSecond = |
| 3091 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 3092 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3093 | |
| 3094 | // Top window is also focusable but is not granted focus. |
| 3095 | windowTop->setFocusable(true); |
| 3096 | windowSecond->setFocusable(true); |
| 3097 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 3098 | setFocusedWindow(windowSecond); |
| 3099 | |
| 3100 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3101 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3102 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3103 | |
| 3104 | // Focused window should receive event. |
| 3105 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 3106 | windowTop->assertNoEvents(); |
| 3107 | } |
| 3108 | |
| 3109 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 3110 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3111 | sp<FakeWindowHandle> window = |
| 3112 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 3113 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3114 | |
| 3115 | window->setFocusable(true); |
| 3116 | // Release channel for window is no longer valid. |
| 3117 | window->releaseChannel(); |
| 3118 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3119 | setFocusedWindow(window); |
| 3120 | |
| 3121 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3122 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3123 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3124 | |
| 3125 | // window channel is invalid, so it should not receive any input event. |
| 3126 | window->assertNoEvents(); |
| 3127 | } |
| 3128 | |
| 3129 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 3130 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3131 | sp<FakeWindowHandle> window = |
| 3132 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 3133 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3134 | |
| 3135 | // Window is not focusable. |
| 3136 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3137 | setFocusedWindow(window); |
| 3138 | |
| 3139 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3140 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3141 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3142 | |
| 3143 | // window is invalid, so it should not receive any input event. |
| 3144 | window->assertNoEvents(); |
| 3145 | } |
| 3146 | |
| 3147 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 3148 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3149 | sp<FakeWindowHandle> windowTop = |
| 3150 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 3151 | sp<FakeWindowHandle> windowSecond = |
| 3152 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 3153 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3154 | |
| 3155 | windowTop->setFocusable(true); |
| 3156 | windowSecond->setFocusable(true); |
| 3157 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 3158 | setFocusedWindow(windowTop); |
| 3159 | windowTop->consumeFocusEvent(true); |
| 3160 | |
| 3161 | setFocusedWindow(windowSecond, windowTop); |
| 3162 | windowSecond->consumeFocusEvent(true); |
| 3163 | windowTop->consumeFocusEvent(false); |
| 3164 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3165 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3166 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3167 | |
| 3168 | // Focused window should receive event. |
| 3169 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 3170 | } |
| 3171 | |
| 3172 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { |
| 3173 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3174 | sp<FakeWindowHandle> windowTop = |
| 3175 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 3176 | sp<FakeWindowHandle> windowSecond = |
| 3177 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 3178 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3179 | |
| 3180 | windowTop->setFocusable(true); |
| 3181 | windowSecond->setFocusable(true); |
| 3182 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 3183 | setFocusedWindow(windowSecond, windowTop); |
| 3184 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3185 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3186 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3187 | |
| 3188 | // Event should be dropped. |
| 3189 | windowTop->assertNoEvents(); |
| 3190 | windowSecond->assertNoEvents(); |
| 3191 | } |
| 3192 | |
| 3193 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 3194 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3195 | sp<FakeWindowHandle> window = |
| 3196 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 3197 | sp<FakeWindowHandle> previousFocusedWindow = |
| 3198 | new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow", |
| 3199 | ADISPLAY_ID_DEFAULT); |
| 3200 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3201 | |
| 3202 | window->setFocusable(true); |
| 3203 | previousFocusedWindow->setFocusable(true); |
| 3204 | window->setVisible(false); |
| 3205 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 3206 | setFocusedWindow(previousFocusedWindow); |
| 3207 | previousFocusedWindow->consumeFocusEvent(true); |
| 3208 | |
| 3209 | // Requesting focus on invisible window takes focus from currently focused window. |
| 3210 | setFocusedWindow(window); |
| 3211 | previousFocusedWindow->consumeFocusEvent(false); |
| 3212 | |
| 3213 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3214 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3215 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3216 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3217 | |
| 3218 | // Window does not get focus event or key down. |
| 3219 | window->assertNoEvents(); |
| 3220 | |
| 3221 | // Window becomes visible. |
| 3222 | window->setVisible(true); |
| 3223 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3224 | |
| 3225 | // Window receives focus event. |
| 3226 | window->consumeFocusEvent(true); |
| 3227 | // Focused window receives key down. |
| 3228 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3229 | } |
| 3230 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 3231 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 3232 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3233 | sp<FakeWindowHandle> window = |
| 3234 | new FakeWindowHandle(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
| 3235 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3236 | |
| 3237 | // window is granted focus. |
| 3238 | window->setFocusable(true); |
| 3239 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3240 | setFocusedWindow(window); |
| 3241 | window->consumeFocusEvent(true); |
| 3242 | |
| 3243 | // When a display is removed window loses focus. |
| 3244 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 3245 | window->consumeFocusEvent(false); |
| 3246 | } |
| 3247 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3248 | /** |
| 3249 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 3250 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 3251 | * of the 'slipperyEnterWindow'. |
| 3252 | * |
| 3253 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 3254 | * a way so that the touched location is no longer covered by the top window. |
| 3255 | * |
| 3256 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 3257 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 3258 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 3259 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 3260 | * with ACTION_DOWN). |
| 3261 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 3262 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 3263 | * |
| 3264 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 3265 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 3266 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 3267 | * |
| 3268 | * In this test, we ensure that the event received by the bottom window has |
| 3269 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 3270 | */ |
| 3271 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
| 3272 | constexpr int32_t SLIPPERY_PID = INJECTOR_PID + 1; |
| 3273 | constexpr int32_t SLIPPERY_UID = INJECTOR_UID + 1; |
| 3274 | |
| 3275 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3276 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3277 | |
| 3278 | sp<FakeWindowHandle> slipperyExitWindow = |
| 3279 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3280 | slipperyExitWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SLIPPERY); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3281 | // Make sure this one overlaps the bottom window |
| 3282 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 3283 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 3284 | // one. Windows with the same owner are not considered to be occluding each other. |
| 3285 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 3286 | |
| 3287 | sp<FakeWindowHandle> slipperyEnterWindow = |
| 3288 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 3289 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3290 | |
| 3291 | mDispatcher->setInputWindows( |
| 3292 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 3293 | |
| 3294 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
| 3295 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3296 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 3297 | mDispatcher->notifyMotion(&args); |
| 3298 | slipperyExitWindow->consumeMotionDown(); |
| 3299 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 3300 | mDispatcher->setInputWindows( |
| 3301 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 3302 | |
| 3303 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3304 | ADISPLAY_ID_DEFAULT, {{51, 51}}); |
| 3305 | mDispatcher->notifyMotion(&args); |
| 3306 | |
| 3307 | slipperyExitWindow->consumeMotionCancel(); |
| 3308 | |
| 3309 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 3310 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 3311 | } |
| 3312 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3313 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 3314 | protected: |
| 3315 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 3316 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 3317 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3318 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3319 | sp<FakeWindowHandle> mWindow; |
| 3320 | |
| 3321 | virtual void SetUp() override { |
| 3322 | mFakePolicy = new FakeInputDispatcherPolicy(); |
| 3323 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3324 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3325 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 3326 | ASSERT_EQ(OK, mDispatcher->start()); |
| 3327 | |
| 3328 | setUpWindow(); |
| 3329 | } |
| 3330 | |
| 3331 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3332 | mApp = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3333 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3334 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3335 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3336 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3337 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3338 | mWindow->consumeFocusEvent(true); |
| 3339 | } |
| 3340 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3341 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3342 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3343 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3344 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
| 3345 | mDispatcher->notifyKey(&keyArgs); |
| 3346 | |
| 3347 | // Window should receive key down event. |
| 3348 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3349 | } |
| 3350 | |
| 3351 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 3352 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 3353 | InputEvent* repeatEvent = mWindow->consume(); |
| 3354 | ASSERT_NE(nullptr, repeatEvent); |
| 3355 | |
| 3356 | uint32_t eventType = repeatEvent->getType(); |
| 3357 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); |
| 3358 | |
| 3359 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 3360 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 3361 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 3362 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 3363 | } |
| 3364 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3365 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3366 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3367 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3368 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
| 3369 | mDispatcher->notifyKey(&keyArgs); |
| 3370 | |
| 3371 | // Window should receive key down event. |
| 3372 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 3373 | 0 /*expectedFlags*/); |
| 3374 | } |
| 3375 | }; |
| 3376 | |
| 3377 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3378 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 3379 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 3380 | expectKeyRepeatOnce(repeatCount); |
| 3381 | } |
| 3382 | } |
| 3383 | |
| 3384 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
| 3385 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 3386 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 3387 | expectKeyRepeatOnce(repeatCount); |
| 3388 | } |
| 3389 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 3390 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3391 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 3392 | expectKeyRepeatOnce(repeatCount); |
| 3393 | } |
| 3394 | } |
| 3395 | |
| 3396 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3397 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3398 | expectKeyRepeatOnce(1 /*repeatCount*/); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3399 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 3400 | mWindow->assertNoEvents(); |
| 3401 | } |
| 3402 | |
| 3403 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
| 3404 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 3405 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 3406 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 3407 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 3408 | // Stale key up from device 1. |
| 3409 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 3410 | // Device 2 is still down, keep repeating |
| 3411 | expectKeyRepeatOnce(2 /*repeatCount*/); |
| 3412 | expectKeyRepeatOnce(3 /*repeatCount*/); |
| 3413 | // Device 2 key up |
| 3414 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 3415 | mWindow->assertNoEvents(); |
| 3416 | } |
| 3417 | |
| 3418 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
| 3419 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 3420 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 3421 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 3422 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 3423 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. |
| 3424 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 3425 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3426 | mWindow->assertNoEvents(); |
| 3427 | } |
| 3428 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 3429 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 3430 | sendAndConsumeKeyDown(DEVICE_ID); |
| 3431 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 3432 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
| 3433 | mDispatcher->notifyDeviceReset(&args); |
| 3434 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 3435 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 3436 | mWindow->assertNoEvents(); |
| 3437 | } |
| 3438 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3439 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3440 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3441 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 3442 | InputEvent* repeatEvent = mWindow->consume(); |
| 3443 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 3444 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 3445 | IdGenerator::getSource(repeatEvent->getId())); |
| 3446 | } |
| 3447 | } |
| 3448 | |
| 3449 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3450 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3451 | |
| 3452 | std::unordered_set<int32_t> idSet; |
| 3453 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 3454 | InputEvent* repeatEvent = mWindow->consume(); |
| 3455 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 3456 | int32_t id = repeatEvent->getId(); |
| 3457 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 3458 | idSet.insert(id); |
| 3459 | } |
| 3460 | } |
| 3461 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3462 | /* Test InputDispatcher for MultiDisplay */ |
| 3463 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 3464 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3465 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3466 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3467 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3468 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3469 | windowInPrimary = |
| 3470 | new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3471 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3472 | // Set focus window for primary display, but focused display would be second one. |
| 3473 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3474 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3475 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3476 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3477 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3478 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3479 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3480 | windowInSecondary = |
| 3481 | new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3482 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3483 | // Set focus display to second one. |
| 3484 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 3485 | // Set focus window for second display. |
| 3486 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3487 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3488 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3489 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3490 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3491 | } |
| 3492 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3493 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3494 | InputDispatcherTest::TearDown(); |
| 3495 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3496 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3497 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3498 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3499 | windowInSecondary.clear(); |
| 3500 | } |
| 3501 | |
| 3502 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3503 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3504 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3505 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3506 | sp<FakeWindowHandle> windowInSecondary; |
| 3507 | }; |
| 3508 | |
| 3509 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 3510 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3511 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3512 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 3513 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3514 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3515 | windowInSecondary->assertNoEvents(); |
| 3516 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3517 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3518 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3519 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 3520 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3521 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3522 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3523 | } |
| 3524 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3525 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3526 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3527 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3528 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3529 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3530 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3531 | windowInSecondary->assertNoEvents(); |
| 3532 | |
| 3533 | // Test inject a key down without display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3534 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3535 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3536 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3537 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3538 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3539 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3540 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3541 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3542 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3543 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
| 3544 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3545 | |
| 3546 | // Test inject a key down, should timeout because of no target window. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3547 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3548 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3549 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3550 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3551 | windowInSecondary->assertNoEvents(); |
| 3552 | } |
| 3553 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3554 | // Test per-display input monitors for motion event. |
| 3555 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3556 | FakeMonitorReceiver monitorInPrimary = |
| 3557 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 3558 | FakeMonitorReceiver monitorInSecondary = |
| 3559 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3560 | |
| 3561 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3562 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3563 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 3564 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3565 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3566 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3567 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3568 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3569 | |
| 3570 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3571 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3572 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 3573 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3574 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3575 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3576 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3577 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3578 | |
| 3579 | // Test inject a non-pointer motion event. |
| 3580 | // If specific a display, it will dispatch to the focused window of particular display, |
| 3581 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3582 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3583 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 3584 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3585 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3586 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3587 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3588 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3589 | } |
| 3590 | |
| 3591 | // Test per-display input monitors for key event. |
| 3592 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3593 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3594 | FakeMonitorReceiver monitorInPrimary = |
| 3595 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 3596 | FakeMonitorReceiver monitorInSecondary = |
| 3597 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3598 | |
| 3599 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3600 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3601 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3602 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3603 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3604 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3605 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3606 | } |
| 3607 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3608 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 3609 | sp<FakeWindowHandle> secondWindowInPrimary = |
| 3610 | new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
| 3611 | secondWindowInPrimary->setFocusable(true); |
| 3612 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 3613 | setFocusedWindow(secondWindowInPrimary); |
| 3614 | windowInPrimary->consumeFocusEvent(false); |
| 3615 | secondWindowInPrimary->consumeFocusEvent(true); |
| 3616 | |
| 3617 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3618 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 3619 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3620 | windowInPrimary->assertNoEvents(); |
| 3621 | windowInSecondary->assertNoEvents(); |
| 3622 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3623 | } |
| 3624 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3625 | class InputFilterTest : public InputDispatcherTest { |
| 3626 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3627 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 3628 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3629 | NotifyMotionArgs motionArgs; |
| 3630 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3631 | motionArgs = |
| 3632 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3633 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3634 | motionArgs = |
| 3635 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3636 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3637 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3638 | if (expectToBeFiltered) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3639 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); |
| 3640 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3641 | } else { |
| 3642 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 3643 | } |
| 3644 | } |
| 3645 | |
| 3646 | void testNotifyKey(bool expectToBeFiltered) { |
| 3647 | NotifyKeyArgs keyArgs; |
| 3648 | |
| 3649 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 3650 | mDispatcher->notifyKey(&keyArgs); |
| 3651 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 3652 | mDispatcher->notifyKey(&keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3653 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3654 | |
| 3655 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 3656 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3657 | } else { |
| 3658 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 3659 | } |
| 3660 | } |
| 3661 | }; |
| 3662 | |
| 3663 | // Test InputFilter for MotionEvent |
| 3664 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 3665 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 3666 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 3667 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 3668 | |
| 3669 | // Enable InputFilter |
| 3670 | mDispatcher->setInputFilterEnabled(true); |
| 3671 | // Test touch on both primary and second display, and check if both events are filtered. |
| 3672 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 3673 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 3674 | |
| 3675 | // Disable InputFilter |
| 3676 | mDispatcher->setInputFilterEnabled(false); |
| 3677 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 3678 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 3679 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 3680 | } |
| 3681 | |
| 3682 | // Test InputFilter for KeyEvent |
| 3683 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 3684 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 3685 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 3686 | |
| 3687 | // Enable InputFilter |
| 3688 | mDispatcher->setInputFilterEnabled(true); |
| 3689 | // Send a key event, and check if it is filtered. |
| 3690 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 3691 | |
| 3692 | // Disable InputFilter |
| 3693 | mDispatcher->setInputFilterEnabled(false); |
| 3694 | // Send a key event, and check if it isn't filtered. |
| 3695 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 3696 | } |
| 3697 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3698 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 3699 | // logical display coordinate space. |
| 3700 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 3701 | ui::Transform firstDisplayTransform; |
| 3702 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 3703 | ui::Transform secondDisplayTransform; |
| 3704 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 3705 | |
| 3706 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 3707 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 3708 | displayInfos[0].transform = firstDisplayTransform; |
| 3709 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 3710 | displayInfos[1].transform = secondDisplayTransform; |
| 3711 | |
| 3712 | mDispatcher->onWindowInfosChanged({}, displayInfos); |
| 3713 | |
| 3714 | // Enable InputFilter |
| 3715 | mDispatcher->setInputFilterEnabled(true); |
| 3716 | |
| 3717 | // Ensure the correct transforms are used for the displays. |
| 3718 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); |
| 3719 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); |
| 3720 | } |
| 3721 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3722 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 3723 | protected: |
| 3724 | virtual void SetUp() override { |
| 3725 | InputDispatcherTest::SetUp(); |
| 3726 | |
| 3727 | /** |
| 3728 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 3729 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 3730 | * on. |
| 3731 | */ |
| 3732 | mDispatcher->setInputFilterEnabled(true); |
| 3733 | |
| 3734 | std::shared_ptr<InputApplicationHandle> application = |
| 3735 | std::make_shared<FakeApplicationHandle>(); |
| 3736 | mWindow = |
| 3737 | new FakeWindowHandle(application, mDispatcher, "Test Window", ADISPLAY_ID_DEFAULT); |
| 3738 | |
| 3739 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3740 | mWindow->setFocusable(true); |
| 3741 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3742 | setFocusedWindow(mWindow); |
| 3743 | mWindow->consumeFocusEvent(true); |
| 3744 | } |
| 3745 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3746 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 3747 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3748 | KeyEvent event; |
| 3749 | |
| 3750 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 3751 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 3752 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
| 3753 | KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime); |
| 3754 | const int32_t additionalPolicyFlags = |
| 3755 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 3756 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3757 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
| 3758 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 3759 | policyFlags | additionalPolicyFlags)); |
| 3760 | |
| 3761 | InputEvent* received = mWindow->consume(); |
| 3762 | ASSERT_NE(nullptr, received); |
| 3763 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3764 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY); |
| 3765 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); |
| 3766 | ASSERT_EQ(flags, keyEvent.getFlags()); |
| 3767 | } |
| 3768 | |
| 3769 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 3770 | int32_t flags) { |
| 3771 | MotionEvent event; |
| 3772 | PointerProperties pointerProperties[1]; |
| 3773 | PointerCoords pointerCoords[1]; |
| 3774 | pointerProperties[0].clear(); |
| 3775 | pointerProperties[0].id = 0; |
| 3776 | pointerCoords[0].clear(); |
| 3777 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 3778 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 3779 | |
| 3780 | ui::Transform identityTransform; |
| 3781 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 3782 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 3783 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 3784 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 3785 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3786 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 3787 | eventTime, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3788 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 3789 | |
| 3790 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 3791 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3792 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
| 3793 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 3794 | policyFlags | additionalPolicyFlags)); |
| 3795 | |
| 3796 | InputEvent* received = mWindow->consume(); |
| 3797 | ASSERT_NE(nullptr, received); |
| 3798 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
| 3799 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 3800 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); |
| 3801 | ASSERT_EQ(flags, motionEvent.getFlags()); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3802 | } |
| 3803 | |
| 3804 | private: |
| 3805 | sp<FakeWindowHandle> mWindow; |
| 3806 | }; |
| 3807 | |
| 3808 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3809 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 3810 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 3811 | // injected device id will be overwritten. |
| 3812 | testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 3813 | 0 /*flags*/); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3814 | } |
| 3815 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3816 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3817 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3818 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 3819 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 3820 | } |
| 3821 | |
| 3822 | TEST_F(InputFilterInjectionPolicyTest, |
| 3823 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 3824 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
| 3825 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 3826 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
| 3830 | testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3831 | VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3834 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3835 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3836 | InputDispatcherTest::SetUp(); |
| 3837 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3838 | std::shared_ptr<FakeApplicationHandle> application = |
| 3839 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3840 | mUnfocusedWindow = |
| 3841 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3842 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
| 3843 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 3844 | // window. |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3845 | mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3846 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3847 | mFocusedWindow = |
| 3848 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 3849 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3850 | mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3851 | |
| 3852 | // Set focused application. |
| 3853 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3854 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3855 | |
| 3856 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3857 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3858 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3859 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3860 | } |
| 3861 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3862 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3863 | InputDispatcherTest::TearDown(); |
| 3864 | |
| 3865 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3866 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3867 | } |
| 3868 | |
| 3869 | protected: |
| 3870 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3871 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3872 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3873 | }; |
| 3874 | |
| 3875 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 3876 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 3877 | // the onPointerDownOutsideFocus callback. |
| 3878 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3879 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3880 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3881 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3882 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3883 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3884 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3885 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3886 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 3887 | } |
| 3888 | |
| 3889 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 3890 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 3891 | // onPointerDownOutsideFocus callback. |
| 3892 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3893 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3894 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3895 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3896 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3897 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3898 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3899 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3900 | } |
| 3901 | |
| 3902 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 3903 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 3904 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3905 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3906 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3907 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3908 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3909 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3910 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3911 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3912 | } |
| 3913 | |
| 3914 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 3915 | // DOWN on the window that already has focus. Ensure no window received the |
| 3916 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3917 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3918 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3919 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 3920 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3921 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 3922 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3923 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3924 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3925 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3926 | } |
| 3927 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3928 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 3929 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 3930 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 3931 | const MotionEvent event = |
| 3932 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3933 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 3934 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20)) |
| 3935 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 3936 | .build(); |
| 3937 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) |
| 3938 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3939 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 3940 | |
| 3941 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3942 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 3943 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 3944 | mUnfocusedWindow->assertNoEvents(); |
| 3945 | } |
| 3946 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3947 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 3948 | // windows that point to the same client token. |
| 3949 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 3950 | virtual void SetUp() override { |
| 3951 | InputDispatcherTest::SetUp(); |
| 3952 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3953 | std::shared_ptr<FakeApplicationHandle> application = |
| 3954 | std::make_shared<FakeApplicationHandle>(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3955 | mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1", |
| 3956 | ADISPLAY_ID_DEFAULT); |
| 3957 | // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window. |
| 3958 | // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows. |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3959 | mWindow1->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3960 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 3961 | |
| 3962 | mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2", |
| 3963 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3964 | mWindow2->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3965 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 3966 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3967 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3968 | } |
| 3969 | |
| 3970 | protected: |
| 3971 | sp<FakeWindowHandle> mWindow1; |
| 3972 | sp<FakeWindowHandle> mWindow2; |
| 3973 | |
| 3974 | // Helper function to convert the point from screen coordinates into the window's space |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3975 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3976 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 3977 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3978 | } |
| 3979 | |
| 3980 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 3981 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3982 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3983 | InputEvent* event = window->consume(); |
| 3984 | |
| 3985 | ASSERT_NE(nullptr, event) << name.c_str() |
| 3986 | << ": consumer should have returned non-NULL event."; |
| 3987 | |
| 3988 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 3989 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 3990 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 3991 | |
| 3992 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3993 | assertMotionAction(expectedAction, motionEvent.getAction()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 3994 | |
| 3995 | for (size_t i = 0; i < points.size(); i++) { |
| 3996 | float expectedX = points[i].x; |
| 3997 | float expectedY = points[i].y; |
| 3998 | |
| 3999 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 4000 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 4001 | << ", got " << motionEvent.getX(i); |
| 4002 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 4003 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 4004 | << ", got " << motionEvent.getY(i); |
| 4005 | } |
| 4006 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4007 | |
| 4008 | void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints, |
| 4009 | std::vector<PointF> expectedPoints) { |
| 4010 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 4011 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 4012 | mDispatcher->notifyMotion(&motionArgs); |
| 4013 | |
| 4014 | // Always consume from window1 since it's the window that has the InputReceiver |
| 4015 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 4016 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4017 | }; |
| 4018 | |
| 4019 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 4020 | // Touch Window 1 |
| 4021 | PointF touchedPoint = {10, 10}; |
| 4022 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4023 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4024 | |
| 4025 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4026 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4027 | |
| 4028 | // Touch Window 2 |
| 4029 | touchedPoint = {150, 150}; |
| 4030 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4031 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4032 | } |
| 4033 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4034 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 4035 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4036 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4037 | |
| 4038 | // Touch Window 1 |
| 4039 | PointF touchedPoint = {10, 10}; |
| 4040 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4041 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4042 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4043 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4044 | |
| 4045 | // Touch Window 2 |
| 4046 | touchedPoint = {150, 150}; |
| 4047 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4048 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 4049 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4050 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4051 | // Update the transform so rotation is set |
| 4052 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4053 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 4054 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4055 | } |
| 4056 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4057 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4058 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4059 | |
| 4060 | // Touch Window 1 |
| 4061 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4062 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4063 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4064 | |
| 4065 | // Touch Window 2 |
| 4066 | int32_t actionPointerDown = |
| 4067 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4068 | touchedPoints.push_back(PointF{150, 150}); |
| 4069 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 4070 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4071 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4072 | // Release Window 2 |
| 4073 | int32_t actionPointerUp = |
| 4074 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 4075 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); |
| 4076 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4077 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4078 | // Update the transform so rotation is set for Window 2 |
| 4079 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4080 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 4081 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4084 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4085 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4086 | |
| 4087 | // Touch Window 1 |
| 4088 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4089 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4090 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4091 | |
| 4092 | // Touch Window 2 |
| 4093 | int32_t actionPointerDown = |
| 4094 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4095 | touchedPoints.push_back(PointF{150, 150}); |
| 4096 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4097 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4098 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4099 | |
| 4100 | // Move both windows |
| 4101 | touchedPoints = {{20, 20}, {175, 175}}; |
| 4102 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 4103 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 4104 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4105 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4106 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4107 | // Release Window 2 |
| 4108 | int32_t actionPointerUp = |
| 4109 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 4110 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); |
| 4111 | expectedPoints.pop_back(); |
| 4112 | |
| 4113 | // Touch Window 2 |
| 4114 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4115 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 4116 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
| 4117 | |
| 4118 | // Move both windows |
| 4119 | touchedPoints = {{20, 20}, {175, 175}}; |
| 4120 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 4121 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 4122 | |
| 4123 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
| 4126 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 4127 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 4128 | |
| 4129 | // Touch Window 1 |
| 4130 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4131 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4132 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4133 | |
| 4134 | // Touch Window 2 |
| 4135 | int32_t actionPointerDown = |
| 4136 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4137 | touchedPoints.push_back(PointF{150, 150}); |
| 4138 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4139 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4140 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4141 | |
| 4142 | // Move both windows |
| 4143 | touchedPoints = {{20, 20}, {175, 175}}; |
| 4144 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 4145 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 4146 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4147 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4148 | } |
| 4149 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4150 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 4151 | virtual void SetUp() override { |
| 4152 | InputDispatcherTest::SetUp(); |
| 4153 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4154 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4155 | mApplication->setDispatchingTimeout(20ms); |
| 4156 | mWindow = |
| 4157 | new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 4158 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 4159 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4160 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4161 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 4162 | // window. |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4163 | mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4164 | |
| 4165 | // Set focused application. |
| 4166 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 4167 | |
| 4168 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4169 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4170 | mWindow->consumeFocusEvent(true); |
| 4171 | } |
| 4172 | |
| 4173 | virtual void TearDown() override { |
| 4174 | InputDispatcherTest::TearDown(); |
| 4175 | mWindow.clear(); |
| 4176 | } |
| 4177 | |
| 4178 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4179 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4180 | sp<FakeWindowHandle> mWindow; |
| 4181 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 4182 | |
| 4183 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4184 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4185 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4186 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4187 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4188 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4189 | WINDOW_LOCATION)); |
| 4190 | } |
| 4191 | }; |
| 4192 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4193 | // Send a tap and respond, which should not cause an ANR. |
| 4194 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 4195 | tapOnWindow(); |
| 4196 | mWindow->consumeMotionDown(); |
| 4197 | mWindow->consumeMotionUp(); |
| 4198 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4199 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4200 | } |
| 4201 | |
| 4202 | // Send a regular key and respond, which should not cause an ANR. |
| 4203 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4204 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4205 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 4206 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4207 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4208 | } |
| 4209 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4210 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 4211 | mWindow->setFocusable(false); |
| 4212 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4213 | mWindow->consumeFocusEvent(false); |
| 4214 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4215 | InputEventInjectionResult result = |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4216 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4217 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, |
| 4218 | false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4219 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4220 | // Key will not go to window because we have no focused window. |
| 4221 | // The 'no focused window' ANR timer should start instead. |
| 4222 | |
| 4223 | // Now, the focused application goes away. |
| 4224 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 4225 | // The key should get dropped and there should be no ANR. |
| 4226 | |
| 4227 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4228 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4229 | } |
| 4230 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4231 | // 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] | 4232 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 4233 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4234 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4235 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4236 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4237 | WINDOW_LOCATION)); |
| 4238 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4239 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 4240 | ASSERT_TRUE(sequenceNum); |
| 4241 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4242 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4243 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4244 | mWindow->finishEvent(*sequenceNum); |
| 4245 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 4246 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4247 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4248 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4249 | } |
| 4250 | |
| 4251 | // Send a key to the app and have the app not respond right away. |
| 4252 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 4253 | // Inject a key, and don't respond - expect that ANR is called. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4254 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4255 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 4256 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4257 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4258 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4259 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4260 | } |
| 4261 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4262 | // We have a focused application, but no focused window |
| 4263 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4264 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4265 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4266 | mWindow->consumeFocusEvent(false); |
| 4267 | |
| 4268 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4269 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4270 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4271 | WINDOW_LOCATION)); |
| 4272 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 4273 | mDispatcher->waitForIdle(); |
| 4274 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4275 | |
| 4276 | // Once a focused event arrives, we get an ANR for this application |
| 4277 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 4278 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4279 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4280 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4281 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4282 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4283 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4284 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4285 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4286 | } |
| 4287 | |
| 4288 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4289 | // Make sure that we don't notify policy twice about the same ANR. |
| 4290 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4291 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4292 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4293 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4294 | |
| 4295 | // Once a focused event arrives, we get an ANR for this application |
| 4296 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 4297 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4298 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4299 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4300 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4301 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4302 | const std::chrono::duration appTimeout = |
| 4303 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4304 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4305 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4306 | std::this_thread::sleep_for(appTimeout); |
| 4307 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 4308 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4309 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4310 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 4311 | // '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] | 4312 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4313 | } |
| 4314 | |
| 4315 | // We have a focused application, but no focused window |
| 4316 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4317 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4318 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4319 | mWindow->consumeFocusEvent(false); |
| 4320 | |
| 4321 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4322 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4323 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4324 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 4325 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4326 | |
| 4327 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4328 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4329 | |
| 4330 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4331 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4332 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4333 | mWindow->assertNoEvents(); |
| 4334 | } |
| 4335 | |
| 4336 | /** |
| 4337 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 4338 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 4339 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 4340 | * the ANR mechanism should still work. |
| 4341 | * |
| 4342 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 4343 | * DOWN event, while not responding on the second one. |
| 4344 | */ |
| 4345 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 4346 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4347 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4348 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 4349 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 4350 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4351 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4352 | |
| 4353 | // Now send ACTION_UP, with identical timestamp |
| 4354 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4355 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 4356 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 4357 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4358 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4359 | |
| 4360 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 4361 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4362 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4363 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | // If an app is not responding to a key event, gesture monitors should continue to receive |
| 4367 | // new motion events |
| 4368 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) { |
| 4369 | FakeMonitorReceiver monitor = |
| 4370 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, |
| 4371 | true /*isGestureMonitor*/); |
| 4372 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4373 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4374 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4375 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4376 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4377 | |
| 4378 | // Stuck on the ACTION_UP |
| 4379 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4380 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4381 | |
| 4382 | // New tap will go to the gesture monitor, but not to the window |
| 4383 | tapOnWindow(); |
| 4384 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4385 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 4386 | |
| 4387 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 4388 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4389 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4390 | mWindow->assertNoEvents(); |
| 4391 | monitor.assertNoEvents(); |
| 4392 | } |
| 4393 | |
| 4394 | // If an app is not responding to a motion event, gesture monitors should continue to receive |
| 4395 | // new motion events |
| 4396 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) { |
| 4397 | FakeMonitorReceiver monitor = |
| 4398 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, |
| 4399 | true /*isGestureMonitor*/); |
| 4400 | |
| 4401 | tapOnWindow(); |
| 4402 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4403 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 4404 | |
| 4405 | mWindow->consumeMotionDown(); |
| 4406 | // Stuck on the ACTION_UP |
| 4407 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4408 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4409 | |
| 4410 | // New tap will go to the gesture monitor, but not to the window |
| 4411 | tapOnWindow(); |
| 4412 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4413 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 4414 | |
| 4415 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 4416 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4417 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4418 | mWindow->assertNoEvents(); |
| 4419 | monitor.assertNoEvents(); |
| 4420 | } |
| 4421 | |
| 4422 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 4423 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 4424 | // get an ANR again. |
| 4425 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 4426 | // 2. consume all pending events (= queue becomes healthy again) |
| 4427 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 4428 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 4429 | tapOnWindow(); |
| 4430 | |
| 4431 | mWindow->consumeMotionDown(); |
| 4432 | // Block on ACTION_UP |
| 4433 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4434 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4435 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 4436 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4437 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4438 | mWindow->assertNoEvents(); |
| 4439 | |
| 4440 | tapOnWindow(); |
| 4441 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4442 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4443 | mWindow->consumeMotionUp(); |
| 4444 | |
| 4445 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4446 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4447 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4448 | mWindow->assertNoEvents(); |
| 4449 | } |
| 4450 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4451 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 4452 | // it. |
| 4453 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4454 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4455 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4456 | WINDOW_LOCATION)); |
| 4457 | |
| 4458 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4459 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4460 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4461 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 4462 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4463 | // 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] | 4464 | mWindow->consumeMotionDown(); |
| 4465 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 4466 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 4467 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4468 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4469 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4470 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4471 | } |
| 4472 | |
| 4473 | /** |
| 4474 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 4475 | * not to to the focused window until the motion is processed. |
| 4476 | * |
| 4477 | * Warning!!! |
| 4478 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 4479 | * and the injection timeout that we specify when injecting the key. |
| 4480 | * We must have the injection timeout (10ms) be smaller than |
| 4481 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 4482 | * |
| 4483 | * If that value changes, this test should also change. |
| 4484 | */ |
| 4485 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 4486 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 4487 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4488 | |
| 4489 | tapOnWindow(); |
| 4490 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 4491 | ASSERT_TRUE(downSequenceNum); |
| 4492 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 4493 | ASSERT_TRUE(upSequenceNum); |
| 4494 | // Don't finish the events yet, and send a key |
| 4495 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 4496 | // window even if motions are still being processed. But because the injection timeout is short, |
| 4497 | // we will receive INJECTION_TIMED_OUT as the result. |
| 4498 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4499 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4500 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4501 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 4502 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4503 | // Key will not be sent to the window, yet, because the window is still processing events |
| 4504 | // and the key remains pending, waiting for the touch events to be processed |
| 4505 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 4506 | ASSERT_FALSE(keySequenceNum); |
| 4507 | |
| 4508 | std::this_thread::sleep_for(500ms); |
| 4509 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 4510 | // to the focused window, even though we have not yet finished the motion event |
| 4511 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4512 | mWindow->finishEvent(*downSequenceNum); |
| 4513 | mWindow->finishEvent(*upSequenceNum); |
| 4514 | } |
| 4515 | |
| 4516 | /** |
| 4517 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 4518 | * not go to the focused window until the motion is processed. |
| 4519 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 4520 | * focused window right away. |
| 4521 | */ |
| 4522 | TEST_F(InputDispatcherSingleWindowAnr, |
| 4523 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 4524 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 4525 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4526 | |
| 4527 | tapOnWindow(); |
| 4528 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 4529 | ASSERT_TRUE(downSequenceNum); |
| 4530 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 4531 | ASSERT_TRUE(upSequenceNum); |
| 4532 | // Don't finish the events yet, and send a key |
| 4533 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4534 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4535 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4536 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4537 | // At this point, key is still pending, and should not be sent to the application yet. |
| 4538 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 4539 | ASSERT_FALSE(keySequenceNum); |
| 4540 | |
| 4541 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 4542 | tapOnWindow(); |
| 4543 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 4544 | // the other events yet. We can finish events in any order. |
| 4545 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 4546 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 4547 | mWindow->consumeMotionDown(); |
| 4548 | mWindow->consumeMotionUp(); |
| 4549 | mWindow->assertNoEvents(); |
| 4550 | } |
| 4551 | |
| 4552 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 4553 | virtual void SetUp() override { |
| 4554 | InputDispatcherTest::SetUp(); |
| 4555 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4556 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4557 | mApplication->setDispatchingTimeout(10ms); |
| 4558 | mUnfocusedWindow = |
| 4559 | new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT); |
| 4560 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
| 4561 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 4562 | // window. |
| 4563 | // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4564 | mUnfocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | |
| 4565 | WindowInfo::Flag::WATCH_OUTSIDE_TOUCH | |
| 4566 | WindowInfo::Flag::SPLIT_TOUCH); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4567 | |
| 4568 | mFocusedWindow = |
| 4569 | new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 4570 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4571 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4572 | mFocusedWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL | WindowInfo::Flag::SPLIT_TOUCH); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4573 | |
| 4574 | // Set focused application. |
| 4575 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4576 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4577 | |
| 4578 | // Expect one focus window exist in display. |
| 4579 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4580 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4581 | mFocusedWindow->consumeFocusEvent(true); |
| 4582 | } |
| 4583 | |
| 4584 | virtual void TearDown() override { |
| 4585 | InputDispatcherTest::TearDown(); |
| 4586 | |
| 4587 | mUnfocusedWindow.clear(); |
| 4588 | mFocusedWindow.clear(); |
| 4589 | } |
| 4590 | |
| 4591 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4592 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4593 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 4594 | sp<FakeWindowHandle> mFocusedWindow; |
| 4595 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 4596 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 4597 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 4598 | |
| 4599 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 4600 | |
| 4601 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 4602 | |
| 4603 | private: |
| 4604 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4605 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4606 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4607 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4608 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4609 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4610 | location)); |
| 4611 | } |
| 4612 | }; |
| 4613 | |
| 4614 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 4615 | // should be ANR'd first. |
| 4616 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4617 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4618 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4619 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4620 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4621 | mFocusedWindow->consumeMotionDown(); |
| 4622 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 4623 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 4624 | // We consumed all events, so no ANR |
| 4625 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4626 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4627 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4628 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4629 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4630 | FOCUSED_WINDOW_LOCATION)); |
| 4631 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 4632 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4633 | |
| 4634 | const std::chrono::duration timeout = |
| 4635 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4636 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4637 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 4638 | // sequence to make it consistent |
| 4639 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4640 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4641 | mFocusedWindow->consumeMotionDown(); |
| 4642 | // This cancel is generated because the connection was unresponsive |
| 4643 | mFocusedWindow->consumeMotionCancel(); |
| 4644 | mFocusedWindow->assertNoEvents(); |
| 4645 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4646 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4647 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4648 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4649 | } |
| 4650 | |
| 4651 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 4652 | // it doesn't matter which order they should have ANR. |
| 4653 | // But we should receive ANR for both. |
| 4654 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 4655 | // Set the timeout for unfocused window to match the focused window |
| 4656 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 4657 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 4658 | |
| 4659 | tapOnFocusedWindow(); |
| 4660 | // 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] | 4661 | sp<IBinder> anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms); |
| 4662 | sp<IBinder> anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4663 | |
| 4664 | // 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] | 4665 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 4666 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 4667 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 4668 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4669 | |
| 4670 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4671 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4672 | |
| 4673 | mFocusedWindow->consumeMotionDown(); |
| 4674 | mFocusedWindow->consumeMotionUp(); |
| 4675 | mUnfocusedWindow->consumeMotionOutside(); |
| 4676 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4677 | sp<IBinder> responsiveToken1 = mFakePolicy->getResponsiveWindowToken(); |
| 4678 | sp<IBinder> responsiveToken2 = mFakePolicy->getResponsiveWindowToken(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4679 | |
| 4680 | // Both applications should be marked as responsive, in any order |
| 4681 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 4682 | mFocusedWindow->getToken() == responsiveToken2); |
| 4683 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 4684 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 4685 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4686 | } |
| 4687 | |
| 4688 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 4689 | // We should also log an error to account for the dropped event (not tested here). |
| 4690 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 4691 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 4692 | tapOnFocusedWindow(); |
| 4693 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 4694 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 4695 | // Receive the events, but don't respond |
| 4696 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 4697 | ASSERT_TRUE(downEventSequenceNum); |
| 4698 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 4699 | ASSERT_TRUE(upEventSequenceNum); |
| 4700 | const std::chrono::duration timeout = |
| 4701 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4702 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4703 | |
| 4704 | // Tap once again |
| 4705 | // 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] | 4706 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4707 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4708 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4709 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4710 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4711 | FOCUSED_WINDOW_LOCATION)); |
| 4712 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 4713 | // valid touch target |
| 4714 | mUnfocusedWindow->assertNoEvents(); |
| 4715 | |
| 4716 | // Consume the first tap |
| 4717 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 4718 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 4719 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4720 | // The second tap did not go to the focused window |
| 4721 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4722 | // Since all events are finished, connection should be deemed healthy again |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4723 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4724 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4725 | } |
| 4726 | |
| 4727 | // If you tap outside of all windows, there will not be ANR |
| 4728 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4729 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4730 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4731 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 4732 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4733 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4734 | } |
| 4735 | |
| 4736 | // Since the focused window is paused, tapping on it should not produce any events |
| 4737 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 4738 | mFocusedWindow->setPaused(true); |
| 4739 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 4740 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4741 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4742 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4743 | FOCUSED_WINDOW_LOCATION)); |
| 4744 | |
| 4745 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 4746 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4747 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 4748 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4749 | |
| 4750 | mFocusedWindow->assertNoEvents(); |
| 4751 | mUnfocusedWindow->assertNoEvents(); |
| 4752 | } |
| 4753 | |
| 4754 | /** |
| 4755 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 4756 | * not to to the focused window until the motion is processed. |
| 4757 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 4758 | * |
| 4759 | * Warning!!! |
| 4760 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 4761 | * and the injection timeout that we specify when injecting the key. |
| 4762 | * We must have the injection timeout (10ms) be smaller than |
| 4763 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 4764 | * |
| 4765 | * If that value changes, this test should also change. |
| 4766 | */ |
| 4767 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 4768 | // Set a long ANR timeout to prevent it from triggering |
| 4769 | mFocusedWindow->setDispatchingTimeout(2s); |
| 4770 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 4771 | |
| 4772 | tapOnUnfocusedWindow(); |
| 4773 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 4774 | ASSERT_TRUE(downSequenceNum); |
| 4775 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 4776 | ASSERT_TRUE(upSequenceNum); |
| 4777 | // Don't finish the events yet, and send a key |
| 4778 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 4779 | // window even if motions are still being processed. |
| 4780 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4781 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4782 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4783 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 4784 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4785 | // Key will not be sent to the window, yet, because the window is still processing events |
| 4786 | // and the key remains pending, waiting for the touch events to be processed |
| 4787 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 4788 | ASSERT_FALSE(keySequenceNum); |
| 4789 | |
| 4790 | // 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] | 4791 | mFocusedWindow->setFocusable(false); |
| 4792 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4793 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4794 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4795 | |
| 4796 | // Focus events should precede the key events |
| 4797 | mUnfocusedWindow->consumeFocusEvent(true); |
| 4798 | mFocusedWindow->consumeFocusEvent(false); |
| 4799 | |
| 4800 | // Finish the tap events, which should unblock dispatcher |
| 4801 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 4802 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 4803 | |
| 4804 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 4805 | // can finally go to the newly focused "mUnfocusedWindow". |
| 4806 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4807 | mFocusedWindow->assertNoEvents(); |
| 4808 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4809 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 4813 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 4814 | // The other window should not be affected by that. |
| 4815 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 4816 | // Touch Window 1 |
| 4817 | NotifyMotionArgs motionArgs = |
| 4818 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4819 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); |
| 4820 | mDispatcher->notifyMotion(&motionArgs); |
| 4821 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 4822 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 4823 | |
| 4824 | // Touch Window 2 |
| 4825 | int32_t actionPointerDown = |
| 4826 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 4827 | |
| 4828 | motionArgs = |
| 4829 | generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4830 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); |
| 4831 | mDispatcher->notifyMotion(&motionArgs); |
| 4832 | |
| 4833 | const std::chrono::duration timeout = |
| 4834 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4835 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4836 | |
| 4837 | mUnfocusedWindow->consumeMotionDown(); |
| 4838 | mFocusedWindow->consumeMotionDown(); |
| 4839 | // Focused window may or may not receive ACTION_MOVE |
| 4840 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 4841 | InputEvent* event; |
| 4842 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 4843 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 4844 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 4845 | ASSERT_NE(nullptr, event); |
| 4846 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 4847 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 4848 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 4849 | mFocusedWindow->consumeMotionCancel(); |
| 4850 | } else { |
| 4851 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 4852 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4853 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 4854 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4855 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4856 | mUnfocusedWindow->assertNoEvents(); |
| 4857 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4858 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4859 | } |
| 4860 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 4861 | /** |
| 4862 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 4863 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 4864 | * |
| 4865 | * If the user touches another application during this time, the key should be dropped. |
| 4866 | * Next, if a new focused window comes in, without toggling the focused application, |
| 4867 | * then no ANR should occur. |
| 4868 | * |
| 4869 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 4870 | * but in some cases the policy may not update the focused application. |
| 4871 | */ |
| 4872 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 4873 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 4874 | std::make_shared<FakeApplicationHandle>(); |
| 4875 | focusedApplication->setDispatchingTimeout(60ms); |
| 4876 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 4877 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 4878 | mFocusedWindow->setFocusable(false); |
| 4879 | |
| 4880 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 4881 | mFocusedWindow->consumeFocusEvent(false); |
| 4882 | |
| 4883 | // Send a key. The ANR timer should start because there is no focused window. |
| 4884 | // 'focusedApplication' will get blamed if this timer completes. |
| 4885 | // 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] | 4886 | InputEventInjectionResult result = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 4887 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4888 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, |
| 4889 | false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4890 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 4891 | |
| 4892 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 4893 | // then the injected touches won't cause the focused event to get dropped. |
| 4894 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 4895 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 4896 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 4897 | // For this test, it means that the key would get delivered to the window once it becomes |
| 4898 | // focused. |
| 4899 | std::this_thread::sleep_for(10ms); |
| 4900 | |
| 4901 | // Touch unfocused window. This should force the pending key to get dropped. |
| 4902 | NotifyMotionArgs motionArgs = |
| 4903 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4904 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); |
| 4905 | mDispatcher->notifyMotion(&motionArgs); |
| 4906 | |
| 4907 | // We do not consume the motion right away, because that would require dispatcher to first |
| 4908 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 4909 | // Set the focused window first. |
| 4910 | mFocusedWindow->setFocusable(true); |
| 4911 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 4912 | setFocusedWindow(mFocusedWindow); |
| 4913 | mFocusedWindow->consumeFocusEvent(true); |
| 4914 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 4915 | // to another application. This could be a bug / behaviour in the policy. |
| 4916 | |
| 4917 | mUnfocusedWindow->consumeMotionDown(); |
| 4918 | |
| 4919 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4920 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 4921 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 4922 | } |
| 4923 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4924 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 4925 | // that has feature NO_INPUT_CHANNEL. |
| 4926 | // Layout: |
| 4927 | // Top (closest to user) |
| 4928 | // mNoInputWindow (above all windows) |
| 4929 | // mBottomWindow |
| 4930 | // Bottom (furthest from user) |
| 4931 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 4932 | virtual void SetUp() override { |
| 4933 | InputDispatcherTest::SetUp(); |
| 4934 | |
| 4935 | mApplication = std::make_shared<FakeApplicationHandle>(); |
| 4936 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, |
| 4937 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
| 4938 | std::make_optional<sp<IBinder>>(nullptr) /*token*/); |
| 4939 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4940 | mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4941 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4942 | // It's perfectly valid for this window to not have an associated input channel |
| 4943 | |
| 4944 | mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window", |
| 4945 | ADISPLAY_ID_DEFAULT); |
| 4946 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4947 | |
| 4948 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 4949 | } |
| 4950 | |
| 4951 | protected: |
| 4952 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 4953 | sp<FakeWindowHandle> mNoInputWindow; |
| 4954 | sp<FakeWindowHandle> mBottomWindow; |
| 4955 | }; |
| 4956 | |
| 4957 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 4958 | PointF touchedPoint = {10, 10}; |
| 4959 | |
| 4960 | NotifyMotionArgs motionArgs = |
| 4961 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4962 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 4963 | mDispatcher->notifyMotion(&motionArgs); |
| 4964 | |
| 4965 | mNoInputWindow->assertNoEvents(); |
| 4966 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 4967 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 4968 | // and therefore should prevent mBottomWindow from receiving touches |
| 4969 | mBottomWindow->assertNoEvents(); |
| 4970 | } |
| 4971 | |
| 4972 | /** |
| 4973 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 4974 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 4975 | */ |
| 4976 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 4977 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
| 4978 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, |
| 4979 | "Window with input channel and NO_INPUT_CHANNEL", |
| 4980 | ADISPLAY_ID_DEFAULT); |
| 4981 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4982 | mNoInputWindow->setInputFeatures(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4983 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4984 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 4985 | |
| 4986 | PointF touchedPoint = {10, 10}; |
| 4987 | |
| 4988 | NotifyMotionArgs motionArgs = |
| 4989 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4990 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 4991 | mDispatcher->notifyMotion(&motionArgs); |
| 4992 | |
| 4993 | mNoInputWindow->assertNoEvents(); |
| 4994 | mBottomWindow->assertNoEvents(); |
| 4995 | } |
| 4996 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4997 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 4998 | protected: |
| 4999 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 5000 | sp<FakeWindowHandle> mWindow; |
| 5001 | sp<FakeWindowHandle> mMirror; |
| 5002 | |
| 5003 | virtual void SetUp() override { |
| 5004 | InputDispatcherTest::SetUp(); |
| 5005 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 5006 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 5007 | mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT, |
| 5008 | mWindow->getToken()); |
| 5009 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 5010 | mWindow->setFocusable(true); |
| 5011 | mMirror->setFocusable(true); |
| 5012 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5013 | } |
| 5014 | }; |
| 5015 | |
| 5016 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 5017 | // Request focus on a mirrored window |
| 5018 | setFocusedWindow(mMirror); |
| 5019 | |
| 5020 | // window gets focused |
| 5021 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5022 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5023 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5024 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5025 | } |
| 5026 | |
| 5027 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 5028 | // focusable. |
| 5029 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 5030 | setFocusedWindow(mMirror); |
| 5031 | |
| 5032 | // window gets focused |
| 5033 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5034 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5035 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5036 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5037 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5038 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5039 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5040 | |
| 5041 | mMirror->setFocusable(false); |
| 5042 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5043 | |
| 5044 | // window loses focus since one of the windows associated with the token in not focusable |
| 5045 | mWindow->consumeFocusEvent(false); |
| 5046 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5047 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5048 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5049 | mWindow->assertNoEvents(); |
| 5050 | } |
| 5051 | |
| 5052 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 5053 | // invisible. |
| 5054 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 5055 | setFocusedWindow(mMirror); |
| 5056 | |
| 5057 | // window gets focused |
| 5058 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5059 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5060 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5061 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5062 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5063 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5064 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5065 | |
| 5066 | mMirror->setVisible(false); |
| 5067 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5068 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5069 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5070 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5071 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5072 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5073 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5074 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5075 | |
| 5076 | mWindow->setVisible(false); |
| 5077 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5078 | |
| 5079 | // window loses focus only after all windows associated with the token become invisible. |
| 5080 | mWindow->consumeFocusEvent(false); |
| 5081 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5082 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5083 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5084 | mWindow->assertNoEvents(); |
| 5085 | } |
| 5086 | |
| 5087 | // A focused & mirrored window remains focused until both windows are removed. |
| 5088 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 5089 | setFocusedWindow(mMirror); |
| 5090 | |
| 5091 | // window gets focused |
| 5092 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5093 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5094 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5095 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5096 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5097 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5098 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5099 | |
| 5100 | // single window is removed but the window token remains focused |
| 5101 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 5102 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5103 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5104 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5105 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5106 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5107 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5108 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5109 | |
| 5110 | // Both windows are removed |
| 5111 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 5112 | mWindow->consumeFocusEvent(false); |
| 5113 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5114 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5115 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5116 | mWindow->assertNoEvents(); |
| 5117 | } |
| 5118 | |
| 5119 | // Focus request can be pending until one window becomes visible. |
| 5120 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 5121 | // Request focus on an invisible mirror. |
| 5122 | mWindow->setVisible(false); |
| 5123 | mMirror->setVisible(false); |
| 5124 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5125 | setFocusedWindow(mMirror); |
| 5126 | |
| 5127 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5128 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5129 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5130 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5131 | |
| 5132 | mMirror->setVisible(true); |
| 5133 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5134 | |
| 5135 | // window gets focused |
| 5136 | mWindow->consumeFocusEvent(true); |
| 5137 | // window gets the pending key event |
| 5138 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5139 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5140 | |
| 5141 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 5142 | protected: |
| 5143 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 5144 | sp<FakeWindowHandle> mWindow; |
| 5145 | sp<FakeWindowHandle> mSecondWindow; |
| 5146 | |
| 5147 | void SetUp() override { |
| 5148 | InputDispatcherTest::SetUp(); |
| 5149 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 5150 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 5151 | mWindow->setFocusable(true); |
| 5152 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
| 5153 | mSecondWindow->setFocusable(true); |
| 5154 | |
| 5155 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 5156 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 5157 | |
| 5158 | setFocusedWindow(mWindow); |
| 5159 | mWindow->consumeFocusEvent(true); |
| 5160 | } |
| 5161 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5162 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
| 5163 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5164 | mDispatcher->notifyPointerCaptureChanged(&args); |
| 5165 | } |
| 5166 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5167 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 5168 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5169 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5170 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 5171 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5172 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5173 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5174 | } |
| 5175 | }; |
| 5176 | |
| 5177 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 5178 | // Ensure that capture cannot be obtained for unfocused windows. |
| 5179 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 5180 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 5181 | mSecondWindow->assertNoEvents(); |
| 5182 | |
| 5183 | // Ensure that capture can be enabled from the focus window. |
| 5184 | requestAndVerifyPointerCapture(mWindow, true); |
| 5185 | |
| 5186 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 5187 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 5188 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 5189 | |
| 5190 | // Ensure that capture can be disabled from the window with capture. |
| 5191 | requestAndVerifyPointerCapture(mWindow, false); |
| 5192 | } |
| 5193 | |
| 5194 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5195 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5196 | |
| 5197 | setFocusedWindow(mSecondWindow); |
| 5198 | |
| 5199 | // Ensure that the capture disabled event was sent first. |
| 5200 | mWindow->consumeCaptureEvent(false); |
| 5201 | mWindow->consumeFocusEvent(false); |
| 5202 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5203 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5204 | |
| 5205 | // Ensure that additional state changes from InputReader are not sent to the window. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5206 | notifyPointerCaptureChanged({}); |
| 5207 | notifyPointerCaptureChanged(request); |
| 5208 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5209 | mWindow->assertNoEvents(); |
| 5210 | mSecondWindow->assertNoEvents(); |
| 5211 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 5212 | } |
| 5213 | |
| 5214 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5215 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5216 | |
| 5217 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5218 | notifyPointerCaptureChanged({}); |
| 5219 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5220 | |
| 5221 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5222 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5223 | mWindow->consumeCaptureEvent(false); |
| 5224 | mWindow->assertNoEvents(); |
| 5225 | } |
| 5226 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 5227 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 5228 | requestAndVerifyPointerCapture(mWindow, true); |
| 5229 | |
| 5230 | // The first window loses focus. |
| 5231 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5232 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 5233 | mWindow->consumeCaptureEvent(false); |
| 5234 | |
| 5235 | // Request Pointer Capture from the second window before the notification from InputReader |
| 5236 | // arrives. |
| 5237 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5238 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 5239 | |
| 5240 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5241 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 5242 | |
| 5243 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5244 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 5245 | |
| 5246 | mSecondWindow->consumeFocusEvent(true); |
| 5247 | mSecondWindow->consumeCaptureEvent(true); |
| 5248 | } |
| 5249 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5250 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 5251 | // App repeatedly enables and disables capture. |
| 5252 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 5253 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 5254 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 5255 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 5256 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 5257 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 5258 | |
| 5259 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 5260 | // first request is now stale, this should do nothing. |
| 5261 | notifyPointerCaptureChanged(firstRequest); |
| 5262 | mWindow->assertNoEvents(); |
| 5263 | |
| 5264 | // InputReader notifies that the second request was enabled. |
| 5265 | notifyPointerCaptureChanged(secondRequest); |
| 5266 | mWindow->consumeCaptureEvent(true); |
| 5267 | } |
| 5268 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5269 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 5270 | protected: |
| 5271 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5272 | |
| 5273 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 5274 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 5275 | |
| 5276 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 5277 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 5278 | |
| 5279 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 5280 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 5281 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 5282 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 5283 | MAXIMUM_OBSCURING_OPACITY); |
| 5284 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5285 | static const int32_t TOUCHED_APP_UID = 10001; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5286 | static const int32_t APP_B_UID = 10002; |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5287 | static const int32_t APP_C_UID = 10003; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5288 | |
| 5289 | sp<FakeWindowHandle> mTouchWindow; |
| 5290 | |
| 5291 | virtual void SetUp() override { |
| 5292 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5293 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5294 | mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK); |
| 5295 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 5296 | } |
| 5297 | |
| 5298 | virtual void TearDown() override { |
| 5299 | InputDispatcherTest::TearDown(); |
| 5300 | mTouchWindow.clear(); |
| 5301 | } |
| 5302 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5303 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, |
| 5304 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5305 | sp<FakeWindowHandle> window = getWindow(uid, name); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5306 | window->setFlags(WindowInfo::Flag::NOT_TOUCHABLE); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5307 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5308 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5309 | return window; |
| 5310 | } |
| 5311 | |
| 5312 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { |
| 5313 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 5314 | sp<FakeWindowHandle> window = |
| 5315 | new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
| 5316 | // Generate an arbitrary PID based on the UID |
| 5317 | window->setOwnerInfo(1777 + (uid % 10000), uid); |
| 5318 | return window; |
| 5319 | } |
| 5320 | |
| 5321 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
| 5322 | NotifyMotionArgs args = |
| 5323 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5324 | ADISPLAY_ID_DEFAULT, points); |
| 5325 | mDispatcher->notifyMotion(&args); |
| 5326 | } |
| 5327 | }; |
| 5328 | |
| 5329 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5330 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5331 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5332 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5333 | |
| 5334 | touch(); |
| 5335 | |
| 5336 | mTouchWindow->assertNoEvents(); |
| 5337 | } |
| 5338 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5339 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5340 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 5341 | const sp<FakeWindowHandle>& w = |
| 5342 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 5343 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5344 | |
| 5345 | touch(); |
| 5346 | |
| 5347 | mTouchWindow->assertNoEvents(); |
| 5348 | } |
| 5349 | |
| 5350 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5351 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 5352 | const sp<FakeWindowHandle>& w = |
| 5353 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 5354 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5355 | |
| 5356 | touch(); |
| 5357 | |
| 5358 | w->assertNoEvents(); |
| 5359 | } |
| 5360 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5361 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5362 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 5363 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5364 | |
| 5365 | touch(); |
| 5366 | |
| 5367 | mTouchWindow->consumeAnyMotionDown(); |
| 5368 | } |
| 5369 | |
| 5370 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5371 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5372 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5373 | w->setFrame(Rect(0, 0, 50, 50)); |
| 5374 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5375 | |
| 5376 | touch({PointF{100, 100}}); |
| 5377 | |
| 5378 | mTouchWindow->consumeAnyMotionDown(); |
| 5379 | } |
| 5380 | |
| 5381 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5382 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5383 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5384 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5385 | |
| 5386 | touch(); |
| 5387 | |
| 5388 | mTouchWindow->consumeAnyMotionDown(); |
| 5389 | } |
| 5390 | |
| 5391 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 5392 | const sp<FakeWindowHandle>& w = |
| 5393 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 5394 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5395 | |
| 5396 | touch(); |
| 5397 | |
| 5398 | mTouchWindow->consumeAnyMotionDown(); |
| 5399 | } |
| 5400 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5401 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 5402 | const sp<FakeWindowHandle>& w = |
| 5403 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 5404 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5405 | |
| 5406 | touch(); |
| 5407 | |
| 5408 | w->assertNoEvents(); |
| 5409 | } |
| 5410 | |
| 5411 | /** |
| 5412 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 5413 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 5414 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 5415 | */ |
| 5416 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5417 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 5418 | const sp<FakeWindowHandle>& w = |
| 5419 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5420 | w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5421 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5422 | |
| 5423 | touch(); |
| 5424 | |
| 5425 | w->consumeMotionOutside(); |
| 5426 | } |
| 5427 | |
| 5428 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 5429 | const sp<FakeWindowHandle>& w = |
| 5430 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5431 | w->addFlags(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5432 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5433 | |
| 5434 | touch(); |
| 5435 | |
| 5436 | InputEvent* event = w->consume(); |
| 5437 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()); |
| 5438 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 5439 | EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getX()); |
| 5440 | EXPECT_EQ(0.0f, motionEvent.getRawPointerCoords(0)->getY()); |
| 5441 | } |
| 5442 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5443 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5444 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5445 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5446 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5447 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5448 | |
| 5449 | touch(); |
| 5450 | |
| 5451 | mTouchWindow->consumeAnyMotionDown(); |
| 5452 | } |
| 5453 | |
| 5454 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 5455 | const sp<FakeWindowHandle>& w = |
| 5456 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5457 | MAXIMUM_OBSCURING_OPACITY); |
| 5458 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5459 | |
| 5460 | touch(); |
| 5461 | |
| 5462 | mTouchWindow->consumeAnyMotionDown(); |
| 5463 | } |
| 5464 | |
| 5465 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5466 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5467 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5468 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5469 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5470 | |
| 5471 | touch(); |
| 5472 | |
| 5473 | mTouchWindow->assertNoEvents(); |
| 5474 | } |
| 5475 | |
| 5476 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 5477 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 5478 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5479 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 5480 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5481 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5482 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 5483 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5484 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 5485 | |
| 5486 | touch(); |
| 5487 | |
| 5488 | mTouchWindow->assertNoEvents(); |
| 5489 | } |
| 5490 | |
| 5491 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 5492 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 5493 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5494 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 5495 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5496 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5497 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 5498 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5499 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 5500 | |
| 5501 | touch(); |
| 5502 | |
| 5503 | mTouchWindow->consumeAnyMotionDown(); |
| 5504 | } |
| 5505 | |
| 5506 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5507 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 5508 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5509 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5510 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5511 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5512 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 5513 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5514 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 5515 | |
| 5516 | touch(); |
| 5517 | |
| 5518 | mTouchWindow->consumeAnyMotionDown(); |
| 5519 | } |
| 5520 | |
| 5521 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 5522 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5523 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5524 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5525 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5526 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 5527 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5528 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5529 | |
| 5530 | touch(); |
| 5531 | |
| 5532 | mTouchWindow->assertNoEvents(); |
| 5533 | } |
| 5534 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5535 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5536 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 5537 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5538 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 5539 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5540 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5541 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5542 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5543 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 5544 | |
| 5545 | touch(); |
| 5546 | |
| 5547 | mTouchWindow->assertNoEvents(); |
| 5548 | } |
| 5549 | |
| 5550 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5551 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 5552 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5553 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 5554 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5555 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5556 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5557 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5558 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 5559 | |
| 5560 | touch(); |
| 5561 | |
| 5562 | mTouchWindow->consumeAnyMotionDown(); |
| 5563 | } |
| 5564 | |
| 5565 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 5566 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5567 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 5568 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5569 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5570 | |
| 5571 | touch(); |
| 5572 | |
| 5573 | mTouchWindow->consumeAnyMotionDown(); |
| 5574 | } |
| 5575 | |
| 5576 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 5577 | const sp<FakeWindowHandle>& w = |
| 5578 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 5579 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5580 | |
| 5581 | touch(); |
| 5582 | |
| 5583 | mTouchWindow->consumeAnyMotionDown(); |
| 5584 | } |
| 5585 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 5586 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5587 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 5588 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 5589 | const sp<FakeWindowHandle>& w = |
| 5590 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 5591 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5592 | |
| 5593 | touch(); |
| 5594 | |
| 5595 | mTouchWindow->assertNoEvents(); |
| 5596 | } |
| 5597 | |
| 5598 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 5599 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 5600 | const sp<FakeWindowHandle>& w = |
| 5601 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 5602 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5603 | |
| 5604 | touch(); |
| 5605 | |
| 5606 | mTouchWindow->consumeAnyMotionDown(); |
| 5607 | } |
| 5608 | |
| 5609 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5610 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 5611 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 5612 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5613 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5614 | OPACITY_ABOVE_THRESHOLD); |
| 5615 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5616 | |
| 5617 | touch(); |
| 5618 | |
| 5619 | mTouchWindow->consumeAnyMotionDown(); |
| 5620 | } |
| 5621 | |
| 5622 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5623 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 5624 | const sp<FakeWindowHandle>& w1 = |
| 5625 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 5626 | OPACITY_BELOW_THRESHOLD); |
| 5627 | const sp<FakeWindowHandle>& w2 = |
| 5628 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5629 | OPACITY_BELOW_THRESHOLD); |
| 5630 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 5631 | |
| 5632 | touch(); |
| 5633 | |
| 5634 | mTouchWindow->assertNoEvents(); |
| 5635 | } |
| 5636 | |
| 5637 | /** |
| 5638 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 5639 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 5640 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 5641 | */ |
| 5642 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5643 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 5644 | const sp<FakeWindowHandle>& wB = |
| 5645 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 5646 | OPACITY_BELOW_THRESHOLD); |
| 5647 | const sp<FakeWindowHandle>& wC = |
| 5648 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 5649 | OPACITY_BELOW_THRESHOLD); |
| 5650 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 5651 | |
| 5652 | touch(); |
| 5653 | |
| 5654 | mTouchWindow->assertNoEvents(); |
| 5655 | } |
| 5656 | |
| 5657 | /** |
| 5658 | * This test is testing that a window from a different UID but with same application token doesn't |
| 5659 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 5660 | */ |
| 5661 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5662 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 5663 | const sp<FakeWindowHandle>& w = |
| 5664 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 5665 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 5666 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5667 | |
| 5668 | touch(); |
| 5669 | |
| 5670 | mTouchWindow->consumeAnyMotionDown(); |
| 5671 | } |
| 5672 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5673 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 5674 | protected: |
| 5675 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 5676 | sp<FakeWindowHandle> mWindow; |
| 5677 | sp<FakeWindowHandle> mSecondWindow; |
| 5678 | sp<FakeWindowHandle> mDragWindow; |
| 5679 | |
| 5680 | void SetUp() override { |
| 5681 | InputDispatcherTest::SetUp(); |
| 5682 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 5683 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 5684 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5685 | mWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5686 | |
| 5687 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
| 5688 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5689 | mSecondWindow->setFlags(WindowInfo::Flag::NOT_TOUCH_MODAL); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5690 | |
| 5691 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 5692 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 5693 | } |
| 5694 | |
| 5695 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 5696 | void performDrag() { |
| 5697 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5698 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5699 | {50, 50})) |
| 5700 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5701 | |
| 5702 | // Window should receive motion event. |
| 5703 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5704 | |
| 5705 | // The drag window covers the entire display |
| 5706 | mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
| 5707 | mDispatcher->setInputWindows( |
| 5708 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 5709 | |
| 5710 | // Transfer touch focus to the drag window |
| 5711 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
| 5712 | true /* isDragDrop */); |
| 5713 | mWindow->consumeMotionCancel(); |
| 5714 | mDragWindow->consumeMotionDown(); |
| 5715 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5716 | |
| 5717 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 5718 | void performStylusDrag() { |
| 5719 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5720 | injectMotionEvent(mDispatcher, |
| 5721 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 5722 | AINPUT_SOURCE_STYLUS) |
| 5723 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 5724 | .pointer(PointerBuilder(0, |
| 5725 | AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 5726 | .x(50) |
| 5727 | .y(50)) |
| 5728 | .build())); |
| 5729 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5730 | |
| 5731 | // The drag window covers the entire display |
| 5732 | mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
| 5733 | mDispatcher->setInputWindows( |
| 5734 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 5735 | |
| 5736 | // Transfer touch focus to the drag window |
| 5737 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
| 5738 | true /* isDragDrop */); |
| 5739 | mWindow->consumeMotionCancel(); |
| 5740 | mDragWindow->consumeMotionDown(); |
| 5741 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5742 | }; |
| 5743 | |
| 5744 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
| 5745 | performDrag(); |
| 5746 | |
| 5747 | // Move on window. |
| 5748 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5749 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5750 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 5751 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5752 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5753 | mWindow->consumeDragEvent(false, 50, 50); |
| 5754 | mSecondWindow->assertNoEvents(); |
| 5755 | |
| 5756 | // Move to another window. |
| 5757 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5758 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5759 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 5760 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5761 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5762 | mWindow->consumeDragEvent(true, 150, 50); |
| 5763 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 5764 | |
| 5765 | // Move back to original window. |
| 5766 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5767 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5768 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 5769 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5770 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5771 | mWindow->consumeDragEvent(false, 50, 50); |
| 5772 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 5773 | |
| 5774 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5775 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 5776 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5777 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 5778 | mWindow->assertNoEvents(); |
| 5779 | mSecondWindow->assertNoEvents(); |
| 5780 | } |
| 5781 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5782 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
| 5783 | performDrag(); |
| 5784 | |
| 5785 | // Move on window. |
| 5786 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5787 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5788 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 5789 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5790 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5791 | mWindow->consumeDragEvent(false, 50, 50); |
| 5792 | mSecondWindow->assertNoEvents(); |
| 5793 | |
| 5794 | // Move to another window. |
| 5795 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5796 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5797 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 5798 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5799 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5800 | mWindow->consumeDragEvent(true, 150, 50); |
| 5801 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 5802 | |
| 5803 | // drop to another window. |
| 5804 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5805 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5806 | {150, 50})) |
| 5807 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5808 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 5809 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 5810 | mWindow->assertNoEvents(); |
| 5811 | mSecondWindow->assertNoEvents(); |
| 5812 | } |
| 5813 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5814 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
| 5815 | performStylusDrag(); |
| 5816 | |
| 5817 | // Move on window and keep button pressed. |
| 5818 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5819 | injectMotionEvent(mDispatcher, |
| 5820 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 5821 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 5822 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 5823 | .x(50) |
| 5824 | .y(50)) |
| 5825 | .build())) |
| 5826 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5827 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5828 | mWindow->consumeDragEvent(false, 50, 50); |
| 5829 | mSecondWindow->assertNoEvents(); |
| 5830 | |
| 5831 | // Move to another window and release button, expect to drop item. |
| 5832 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5833 | injectMotionEvent(mDispatcher, |
| 5834 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 5835 | .buttonState(0) |
| 5836 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 5837 | .x(150) |
| 5838 | .y(50)) |
| 5839 | .build())) |
| 5840 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5841 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5842 | mWindow->assertNoEvents(); |
| 5843 | mSecondWindow->assertNoEvents(); |
| 5844 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 5845 | |
| 5846 | // nothing to the window. |
| 5847 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5848 | injectMotionEvent(mDispatcher, |
| 5849 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 5850 | .buttonState(0) |
| 5851 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 5852 | .x(150) |
| 5853 | .y(50)) |
| 5854 | .build())) |
| 5855 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5856 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 5857 | mWindow->assertNoEvents(); |
| 5858 | mSecondWindow->assertNoEvents(); |
| 5859 | } |
| 5860 | |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 5861 | TEST_F(InputDispatcherDragTests, DragAndDrop_InvalidWindow) { |
| 5862 | performDrag(); |
| 5863 | |
| 5864 | // Set second window invisible. |
| 5865 | mSecondWindow->setVisible(false); |
| 5866 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 5867 | |
| 5868 | // Move on window. |
| 5869 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5870 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5871 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 5872 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5873 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5874 | mWindow->consumeDragEvent(false, 50, 50); |
| 5875 | mSecondWindow->assertNoEvents(); |
| 5876 | |
| 5877 | // Move to another window. |
| 5878 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5879 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5880 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 5881 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5882 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5883 | mWindow->consumeDragEvent(true, 150, 50); |
| 5884 | mSecondWindow->assertNoEvents(); |
| 5885 | |
| 5886 | // drop to another window. |
| 5887 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5888 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5889 | {150, 50})) |
| 5890 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5891 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 5892 | mFakePolicy->assertDropTargetEquals(nullptr); |
| 5893 | mWindow->assertNoEvents(); |
| 5894 | mSecondWindow->assertNoEvents(); |
| 5895 | } |
| 5896 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 5897 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 5898 | |
| 5899 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 5900 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5901 | sp<FakeWindowHandle> window = |
| 5902 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 5903 | window->setInputFeatures(WindowInfo::Feature::DROP_INPUT); |
| 5904 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5905 | window->setFocusable(true); |
| 5906 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5907 | setFocusedWindow(window); |
| 5908 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 5909 | |
| 5910 | // With the flag set, window should not get any input |
| 5911 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 5912 | mDispatcher->notifyKey(&keyArgs); |
| 5913 | window->assertNoEvents(); |
| 5914 | |
| 5915 | NotifyMotionArgs motionArgs = |
| 5916 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5917 | ADISPLAY_ID_DEFAULT); |
| 5918 | mDispatcher->notifyMotion(&motionArgs); |
| 5919 | window->assertNoEvents(); |
| 5920 | |
| 5921 | // With the flag cleared, the window should get input |
| 5922 | window->setInputFeatures({}); |
| 5923 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5924 | |
| 5925 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 5926 | mDispatcher->notifyKey(&keyArgs); |
| 5927 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 5928 | |
| 5929 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5930 | ADISPLAY_ID_DEFAULT); |
| 5931 | mDispatcher->notifyMotion(&motionArgs); |
| 5932 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5933 | window->assertNoEvents(); |
| 5934 | } |
| 5935 | |
| 5936 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 5937 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 5938 | std::make_shared<FakeApplicationHandle>(); |
| 5939 | sp<FakeWindowHandle> obscuringWindow = |
| 5940 | new FakeWindowHandle(obscuringApplication, mDispatcher, "obscuringWindow", |
| 5941 | ADISPLAY_ID_DEFAULT); |
| 5942 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 5943 | obscuringWindow->setOwnerInfo(111, 111); |
| 5944 | obscuringWindow->setFlags(WindowInfo::Flag::NOT_TOUCHABLE); |
| 5945 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5946 | sp<FakeWindowHandle> window = |
| 5947 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 5948 | window->setInputFeatures(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED); |
| 5949 | window->setOwnerInfo(222, 222); |
| 5950 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5951 | window->setFocusable(true); |
| 5952 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 5953 | setFocusedWindow(window); |
| 5954 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 5955 | |
| 5956 | // With the flag set, window should not get any input |
| 5957 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 5958 | mDispatcher->notifyKey(&keyArgs); |
| 5959 | window->assertNoEvents(); |
| 5960 | |
| 5961 | NotifyMotionArgs motionArgs = |
| 5962 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5963 | ADISPLAY_ID_DEFAULT); |
| 5964 | mDispatcher->notifyMotion(&motionArgs); |
| 5965 | window->assertNoEvents(); |
| 5966 | |
| 5967 | // With the flag cleared, the window should get input |
| 5968 | window->setInputFeatures({}); |
| 5969 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 5970 | |
| 5971 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 5972 | mDispatcher->notifyKey(&keyArgs); |
| 5973 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 5974 | |
| 5975 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5976 | ADISPLAY_ID_DEFAULT); |
| 5977 | mDispatcher->notifyMotion(&motionArgs); |
| 5978 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 5979 | window->assertNoEvents(); |
| 5980 | } |
| 5981 | |
| 5982 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 5983 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 5984 | std::make_shared<FakeApplicationHandle>(); |
| 5985 | sp<FakeWindowHandle> obscuringWindow = |
| 5986 | new FakeWindowHandle(obscuringApplication, mDispatcher, "obscuringWindow", |
| 5987 | ADISPLAY_ID_DEFAULT); |
| 5988 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 5989 | obscuringWindow->setOwnerInfo(111, 111); |
| 5990 | obscuringWindow->setFlags(WindowInfo::Flag::NOT_TOUCHABLE); |
| 5991 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5992 | sp<FakeWindowHandle> window = |
| 5993 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 5994 | window->setInputFeatures(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED); |
| 5995 | window->setOwnerInfo(222, 222); |
| 5996 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5997 | window->setFocusable(true); |
| 5998 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 5999 | setFocusedWindow(window); |
| 6000 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 6001 | |
| 6002 | // With the flag set, window should not get any input |
| 6003 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 6004 | mDispatcher->notifyKey(&keyArgs); |
| 6005 | window->assertNoEvents(); |
| 6006 | |
| 6007 | NotifyMotionArgs motionArgs = |
| 6008 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6009 | ADISPLAY_ID_DEFAULT); |
| 6010 | mDispatcher->notifyMotion(&motionArgs); |
| 6011 | window->assertNoEvents(); |
| 6012 | |
| 6013 | // When the window is no longer obscured because it went on top, it should get input |
| 6014 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 6015 | |
| 6016 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 6017 | mDispatcher->notifyKey(&keyArgs); |
| 6018 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 6019 | |
| 6020 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6021 | ADISPLAY_ID_DEFAULT); |
| 6022 | mDispatcher->notifyMotion(&motionArgs); |
| 6023 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6024 | window->assertNoEvents(); |
| 6025 | } |
| 6026 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6027 | } // namespace android::inputdispatcher |