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> |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 20 | #include <android-base/silent_death_test.h> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 21 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 22 | #include <android-base/thread_annotations.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 23 | #include <binder/Binder.h> |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 24 | #include <fcntl.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 26 | #include <input/Input.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 27 | #include <linux/input.h> |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 28 | #include <sys/epoll.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 29 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 30 | #include <cinttypes> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 31 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 32 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 33 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 34 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 35 | using android::base::StringPrintf; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 36 | using android::gui::FocusRequest; |
| 37 | using android::gui::TouchOcclusionMode; |
| 38 | using android::gui::WindowInfo; |
| 39 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 40 | using android::os::InputEventInjectionResult; |
| 41 | using android::os::InputEventInjectionSync; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 42 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 43 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 44 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 45 | using namespace ftl::flag_operators; |
| 46 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 47 | // An arbitrary time value. |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 48 | static constexpr nsecs_t ARBITRARY_TIME = 1234; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 49 | |
| 50 | // An arbitrary device id. |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 51 | static constexpr int32_t DEVICE_ID = 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 52 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 53 | // An arbitrary display id. |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 54 | static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
| 55 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 56 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 57 | static constexpr int32_t POINTER_1_DOWN = |
| 58 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 59 | static constexpr int32_t POINTER_2_DOWN = |
| 60 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 61 | static constexpr int32_t POINTER_1_UP = |
| 62 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 63 | |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 64 | // The default pid and uid for windows created by the test. |
| 65 | static constexpr int32_t WINDOW_PID = 999; |
| 66 | static constexpr int32_t WINDOW_UID = 1001; |
| 67 | |
| 68 | // The default policy flags to use for event injection by tests. |
| 69 | static constexpr uint32_t DEFAULT_POLICY_FLAGS = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 70 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 71 | // An arbitrary pid of the gesture monitor window |
| 72 | static constexpr int32_t MONITOR_PID = 2001; |
| 73 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 74 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 75 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 76 | struct PointF { |
| 77 | float x; |
| 78 | float y; |
| 79 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 80 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 81 | /** |
| 82 | * Return a DOWN key event with KEYCODE_A. |
| 83 | */ |
| 84 | static KeyEvent getTestKeyEvent() { |
| 85 | KeyEvent event; |
| 86 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 87 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 88 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 89 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 90 | return event; |
| 91 | } |
| 92 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 93 | static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) { |
| 94 | ASSERT_EQ(expectedAction, receivedAction) |
| 95 | << "expected " << MotionEvent::actionToString(expectedAction) << ", got " |
| 96 | << MotionEvent::actionToString(receivedAction); |
| 97 | } |
| 98 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 99 | // --- FakeInputDispatcherPolicy --- |
| 100 | |
| 101 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 102 | InputDispatcherConfiguration mConfig; |
| 103 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 104 | using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>; |
| 105 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 106 | protected: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 107 | virtual ~FakeInputDispatcherPolicy() {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 108 | |
| 109 | public: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 110 | FakeInputDispatcherPolicy() {} |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 111 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 112 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 113 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
| 114 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_KEY); |
| 115 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 116 | |
| 117 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 118 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 119 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 120 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 121 | } |
| 122 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 123 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 124 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
| 125 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_MOTION); |
| 126 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 127 | |
| 128 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 129 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 130 | EXPECT_EQ(motionEvent.getAction(), args.action); |
| 131 | EXPECT_EQ(motionEvent.getX(0), point.x); |
| 132 | EXPECT_EQ(motionEvent.getY(0), point.y); |
| 133 | EXPECT_EQ(motionEvent.getRawX(0), point.x); |
| 134 | EXPECT_EQ(motionEvent.getRawY(0), point.y); |
| 135 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 136 | } |
| 137 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 138 | void assertFilterInputEventWasNotCalled() { |
| 139 | std::scoped_lock lock(mLock); |
| 140 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 141 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 142 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 143 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 144 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 145 | ASSERT_TRUE(mConfigurationChangedTime) |
| 146 | << "Timed out waiting for configuration changed call"; |
| 147 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 148 | mConfigurationChangedTime = std::nullopt; |
| 149 | } |
| 150 | |
| 151 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 152 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 153 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 154 | // 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] | 155 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 156 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 157 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 158 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 159 | mLastNotifySwitch = std::nullopt; |
| 160 | } |
| 161 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 162 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 163 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 164 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 165 | mOnPointerDownToken.clear(); |
| 166 | } |
| 167 | |
| 168 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 169 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 170 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 171 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 174 | // This function must be called soon after the expected ANR timer starts, |
| 175 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 176 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 177 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 178 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 179 | std::unique_lock lock(mLock); |
| 180 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 181 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 182 | ASSERT_NO_FATAL_FAILURE( |
| 183 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 184 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 187 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 188 | const sp<WindowInfoHandle>& window) { |
| 189 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 190 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 191 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 192 | } |
| 193 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 194 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 195 | const sp<IBinder>& expectedToken, |
| 196 | int32_t expectedPid) { |
| 197 | std::unique_lock lock(mLock); |
| 198 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 199 | AnrResult result; |
| 200 | ASSERT_NO_FATAL_FAILURE(result = |
| 201 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
| 202 | const auto& [token, pid] = result; |
| 203 | ASSERT_EQ(expectedToken, token); |
| 204 | ASSERT_EQ(expectedPid, pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 205 | } |
| 206 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 207 | /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 208 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 209 | std::unique_lock lock(mLock); |
| 210 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 211 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 212 | const auto& [token, _] = result; |
| 213 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 216 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
| 217 | int32_t expectedPid) { |
| 218 | std::unique_lock lock(mLock); |
| 219 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 220 | AnrResult result; |
| 221 | ASSERT_NO_FATAL_FAILURE( |
| 222 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
| 223 | const auto& [token, pid] = result; |
| 224 | ASSERT_EQ(expectedToken, token); |
| 225 | ASSERT_EQ(expectedPid, pid); |
| 226 | } |
| 227 | |
| 228 | /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 229 | sp<IBinder> getResponsiveWindowToken() { |
| 230 | std::unique_lock lock(mLock); |
| 231 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 232 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 233 | const auto& [token, _] = result; |
| 234 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void assertNotifyAnrWasNotCalled() { |
| 238 | std::scoped_lock lock(mLock); |
| 239 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 240 | ASSERT_TRUE(mAnrWindows.empty()); |
| 241 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 242 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 243 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 246 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 247 | mConfig.keyRepeatTimeout = timeout; |
| 248 | mConfig.keyRepeatDelay = delay; |
| 249 | } |
| 250 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 251 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 252 | std::unique_lock lock(mLock); |
| 253 | base::ScopedLockAssertion assumeLocked(mLock); |
| 254 | |
| 255 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 256 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 257 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 258 | enabled; |
| 259 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 260 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 261 | << ") to be called."; |
| 262 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 263 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 264 | auto request = *mPointerCaptureRequest; |
| 265 | mPointerCaptureRequest.reset(); |
| 266 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void assertSetPointerCaptureNotCalled() { |
| 270 | std::unique_lock lock(mLock); |
| 271 | base::ScopedLockAssertion assumeLocked(mLock); |
| 272 | |
| 273 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 274 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 275 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 276 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 277 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 278 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 279 | } |
| 280 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 281 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { |
| 282 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 283 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 284 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 285 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 286 | } |
| 287 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 288 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 289 | std::unique_lock lock(mLock); |
| 290 | base::ScopedLockAssertion assumeLocked(mLock); |
| 291 | std::optional<sp<IBinder>> receivedToken = |
| 292 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 293 | mNotifyInputChannelBroken); |
| 294 | ASSERT_TRUE(receivedToken.has_value()); |
| 295 | ASSERT_EQ(token, *receivedToken); |
| 296 | } |
| 297 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 298 | /** |
| 299 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 300 | */ |
| 301 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 302 | mInterceptKeyTimeout = timeout; |
| 303 | } |
| 304 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 305 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 306 | std::mutex mLock; |
| 307 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 308 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 309 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 310 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 311 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 312 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 313 | |
| 314 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 315 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 316 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 317 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 318 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 319 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 320 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 321 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 322 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 323 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 324 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 325 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 326 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 327 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 328 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 329 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 330 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 331 | // first entry from the container and erase it. |
| 332 | template <class T> |
| 333 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 334 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 335 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 336 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 337 | // before checking if ANR was called. |
| 338 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 339 | // to provide it some time to act. 100ms seems reasonable. |
| 340 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 341 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 342 | std::optional<T> token = |
| 343 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 344 | if (!token.has_value()) { |
| 345 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 346 | return {}; |
| 347 | } |
| 348 | |
| 349 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 350 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 351 | // the dispatcher started counting before this function was called |
| 352 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 353 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 354 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 355 | << "ms, but waited " |
| 356 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 357 | << "ms instead"; |
| 358 | } |
| 359 | return *token; |
| 360 | } |
| 361 | |
| 362 | template <class T> |
| 363 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 364 | std::queue<T>& storage, |
| 365 | std::unique_lock<std::mutex>& lock, |
| 366 | std::condition_variable& condition) |
| 367 | REQUIRES(mLock) { |
| 368 | condition.wait_for(lock, timeout, |
| 369 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 370 | if (storage.empty()) { |
| 371 | ADD_FAILURE() << "Did not receive the expected callback"; |
| 372 | return std::nullopt; |
| 373 | } |
| 374 | T item = storage.front(); |
| 375 | storage.pop(); |
| 376 | return std::make_optional(item); |
| 377 | } |
| 378 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 379 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 380 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 381 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 384 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid, |
| 385 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 386 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 387 | ASSERT_TRUE(pid.has_value()); |
| 388 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 389 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 390 | } |
| 391 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 392 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
| 393 | std::optional<int32_t> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 394 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 395 | ASSERT_TRUE(pid.has_value()); |
| 396 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 397 | mNotifyAnr.notify_all(); |
| 398 | } |
| 399 | |
| 400 | void notifyNoFocusedWindowAnr( |
| 401 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 402 | std::scoped_lock lock(mLock); |
| 403 | mAnrApplications.push(applicationHandle); |
| 404 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 407 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 408 | std::scoped_lock lock(mLock); |
| 409 | mBrokenInputChannels.push(connectionToken); |
| 410 | mNotifyInputChannelBroken.notify_all(); |
| 411 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 412 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 413 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 414 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 415 | void notifyUntrustedTouch(const std::string& obscuringPackage) override {} |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 416 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 417 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 418 | const std::vector<float>& values) override {} |
| 419 | |
| 420 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 421 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 422 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 423 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 424 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 425 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 426 | *outConfig = mConfig; |
| 427 | } |
| 428 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 429 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 430 | std::scoped_lock lock(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 431 | switch (inputEvent->getType()) { |
| 432 | case AINPUT_EVENT_TYPE_KEY: { |
| 433 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 434 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 435 | break; |
| 436 | } |
| 437 | |
| 438 | case AINPUT_EVENT_TYPE_MOTION: { |
| 439 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 440 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 441 | break; |
| 442 | } |
| 443 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 444 | return true; |
| 445 | } |
| 446 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 447 | void interceptKeyBeforeQueueing(const KeyEvent* inputEvent, uint32_t&) override { |
| 448 | if (inputEvent->getAction() == AKEY_EVENT_ACTION_UP) { |
| 449 | // Clear intercept state when we handled the event. |
| 450 | mInterceptKeyTimeout = 0ms; |
| 451 | } |
| 452 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 453 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 454 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 455 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 456 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 457 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 458 | // Clear intercept state so we could dispatch the event in next wake. |
| 459 | mInterceptKeyTimeout = 0ms; |
| 460 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 463 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 464 | return false; |
| 465 | } |
| 466 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 467 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 468 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 469 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 470 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 471 | * essentially a passthrough for notifySwitch. |
| 472 | */ |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 473 | mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 474 | } |
| 475 | |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 476 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 477 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 478 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 479 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 480 | mOnPointerDownToken = newToken; |
| 481 | } |
| 482 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 483 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 484 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 485 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 486 | mPointerCaptureChangedCondition.notify_all(); |
| 487 | } |
| 488 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 489 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 490 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 491 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 492 | mDropTargetWindowToken = token; |
| 493 | } |
| 494 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 495 | void assertFilterInputEventWasCalledInternal( |
| 496 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 497 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 498 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 499 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 500 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 501 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 502 | }; |
| 503 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 504 | // --- InputDispatcherTest --- |
| 505 | |
| 506 | class InputDispatcherTest : public testing::Test { |
| 507 | protected: |
| 508 | sp<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 509 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 510 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 511 | void SetUp() override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 512 | mFakePolicy = new FakeInputDispatcherPolicy(); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 513 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy, STALE_EVENT_TIMEOUT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 514 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 515 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 516 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 519 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 520 | ASSERT_EQ(OK, mDispatcher->stop()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 521 | mFakePolicy.clear(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 522 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 523 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 524 | |
| 525 | /** |
| 526 | * Used for debugging when writing the test |
| 527 | */ |
| 528 | void dumpDispatcherState() { |
| 529 | std::string dump; |
| 530 | mDispatcher->dump(dump); |
| 531 | std::stringstream ss(dump); |
| 532 | std::string to; |
| 533 | |
| 534 | while (std::getline(ss, to, '\n')) { |
| 535 | ALOGE("%s", to.c_str()); |
| 536 | } |
| 537 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 538 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 539 | void setFocusedWindow(const sp<WindowInfoHandle>& window, |
| 540 | const sp<WindowInfoHandle>& focusedWindow = nullptr) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 541 | FocusRequest request; |
| 542 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 543 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 544 | if (focusedWindow) { |
| 545 | request.focusedToken = focusedWindow->getToken(); |
| 546 | } |
| 547 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 548 | request.displayId = window->getInfo()->displayId; |
| 549 | mDispatcher->setFocusedWindow(request); |
| 550 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 551 | }; |
| 552 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 553 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 554 | KeyEvent event; |
| 555 | |
| 556 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 557 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 558 | INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 559 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 560 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 561 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 562 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 563 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 564 | << "Should reject key events with undefined action."; |
| 565 | |
| 566 | // 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] | 567 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 568 | 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] | 569 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 570 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 571 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 572 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 573 | << "Should reject key events with ACTION_MULTIPLE."; |
| 574 | } |
| 575 | |
| 576 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 577 | MotionEvent event; |
| 578 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 579 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 580 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 581 | pointerProperties[i].clear(); |
| 582 | pointerProperties[i].id = i; |
| 583 | pointerCoords[i].clear(); |
| 584 | } |
| 585 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 586 | // Some constants commonly used below |
| 587 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 588 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 589 | constexpr int32_t metaState = AMETA_NONE; |
| 590 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 591 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 592 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 593 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 594 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 595 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, |
| 596 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 597 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 598 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 599 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 600 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 601 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 602 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 603 | << "Should reject motion events with undefined action."; |
| 604 | |
| 605 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 606 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 607 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 608 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 609 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 610 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 611 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 612 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 613 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 614 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 615 | << "Should reject motion events with pointer down index too large."; |
| 616 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 617 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 618 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 619 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 620 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 621 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 622 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 623 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 624 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 625 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 626 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 627 | << "Should reject motion events with pointer down index too small."; |
| 628 | |
| 629 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 630 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 631 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 632 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 633 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 634 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 635 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 636 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 637 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 638 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 639 | << "Should reject motion events with pointer up index too large."; |
| 640 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 641 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 642 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 643 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 644 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 645 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 646 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 647 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 648 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 649 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 650 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 651 | << "Should reject motion events with pointer up index too small."; |
| 652 | |
| 653 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 654 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 655 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 656 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 657 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 658 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 659 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 660 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 661 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 662 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 663 | << "Should reject motion events with 0 pointers."; |
| 664 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 665 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 666 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 667 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 668 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 669 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 670 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 671 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 672 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 673 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 674 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 675 | |
| 676 | // Rejects motion events with invalid pointer ids. |
| 677 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 678 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 679 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 680 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 681 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 682 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 683 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 684 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 685 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 686 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 687 | << "Should reject motion events with pointer ids less than 0."; |
| 688 | |
| 689 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 690 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 691 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 692 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 693 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 694 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 695 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 696 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 697 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 698 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 699 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 700 | |
| 701 | // Rejects motion events with duplicate pointer ids. |
| 702 | pointerProperties[0].id = 1; |
| 703 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 704 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 705 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 706 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 707 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 708 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 709 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 710 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 711 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 712 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 713 | << "Should reject motion events with duplicate pointer ids."; |
| 714 | } |
| 715 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 716 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 717 | |
| 718 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 719 | constexpr nsecs_t eventTime = 20; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 720 | NotifyConfigurationChangedArgs args(10 /*id*/, eventTime); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 721 | mDispatcher->notifyConfigurationChanged(&args); |
| 722 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 723 | |
| 724 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 725 | } |
| 726 | |
| 727 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 728 | NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/, |
| 729 | 2 /*switchMask*/); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 730 | mDispatcher->notifySwitch(&args); |
| 731 | |
| 732 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 733 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 734 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 735 | } |
| 736 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 737 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 738 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 739 | // Default input dispatching timeout if there is no focused application or paused window |
| 740 | // from which to determine an appropriate dispatching timeout. |
| 741 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 742 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 743 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 744 | |
| 745 | class FakeApplicationHandle : public InputApplicationHandle { |
| 746 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 747 | FakeApplicationHandle() { |
| 748 | mInfo.name = "Fake Application"; |
| 749 | mInfo.token = new BBinder(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 750 | mInfo.dispatchingTimeoutMillis = |
| 751 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 752 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 753 | virtual ~FakeApplicationHandle() {} |
| 754 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 755 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 756 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 757 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 758 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 759 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 760 | }; |
| 761 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 762 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 763 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 764 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 765 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 766 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 767 | } |
| 768 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 769 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 770 | InputEvent* event; |
| 771 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 772 | if (!consumeSeq) { |
| 773 | return nullptr; |
| 774 | } |
| 775 | finishEvent(*consumeSeq); |
| 776 | return event; |
| 777 | } |
| 778 | |
| 779 | /** |
| 780 | * Receive an event without acknowledging it. |
| 781 | * Return the sequence number that could later be used to send finished signal. |
| 782 | */ |
| 783 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 784 | uint32_t consumeSeq; |
| 785 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 786 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 787 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 788 | status_t status = WOULD_BLOCK; |
| 789 | while (status == WOULD_BLOCK) { |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 790 | status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 791 | &event); |
| 792 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 793 | if (elapsed > 100ms) { |
| 794 | break; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | if (status == WOULD_BLOCK) { |
| 799 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 800 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | if (status != OK) { |
| 804 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 805 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 806 | } |
| 807 | if (event == nullptr) { |
| 808 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 809 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 810 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 811 | if (outEvent != nullptr) { |
| 812 | *outEvent = event; |
| 813 | } |
| 814 | return consumeSeq; |
| 815 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 816 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 817 | /** |
| 818 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 819 | */ |
| 820 | void finishEvent(uint32_t consumeSeq) { |
| 821 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 822 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 823 | } |
| 824 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 825 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 826 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); |
| 827 | ASSERT_EQ(OK, status); |
| 828 | } |
| 829 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 830 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 831 | std::optional<int32_t> expectedDisplayId, |
| 832 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 833 | InputEvent* event = consume(); |
| 834 | |
| 835 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 836 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 837 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 838 | << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType) |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 839 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 840 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 841 | if (expectedDisplayId.has_value()) { |
| 842 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 843 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 844 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 845 | switch (expectedEventType) { |
| 846 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 847 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 848 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 849 | if (expectedFlags.has_value()) { |
| 850 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 851 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 852 | break; |
| 853 | } |
| 854 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 855 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 856 | assertMotionAction(expectedAction, motionEvent.getAction()); |
| 857 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 858 | if (expectedFlags.has_value()) { |
| 859 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 860 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 861 | break; |
| 862 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 863 | case AINPUT_EVENT_TYPE_FOCUS: { |
| 864 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 865 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 866 | case AINPUT_EVENT_TYPE_CAPTURE: { |
| 867 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 868 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 869 | case AINPUT_EVENT_TYPE_TOUCH_MODE: { |
| 870 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 871 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 872 | case AINPUT_EVENT_TYPE_DRAG: { |
| 873 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 874 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 875 | default: { |
| 876 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; |
| 877 | } |
| 878 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 879 | } |
| 880 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 881 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 882 | InputEvent* event = consume(); |
| 883 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 884 | << ": consumer should have returned non-NULL event."; |
| 885 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType()) |
| 886 | << "Got " << inputEventTypeToString(event->getType()) |
| 887 | << " event instead of FOCUS event"; |
| 888 | |
| 889 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 890 | << mName.c_str() << ": event displayId should always be NONE."; |
| 891 | |
| 892 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 893 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 894 | } |
| 895 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 896 | void consumeCaptureEvent(bool hasCapture) { |
| 897 | const InputEvent* event = consume(); |
| 898 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 899 | << ": consumer should have returned non-NULL event."; |
| 900 | ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType()) |
| 901 | << "Got " << inputEventTypeToString(event->getType()) |
| 902 | << " event instead of CAPTURE event"; |
| 903 | |
| 904 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 905 | << mName.c_str() << ": event displayId should always be NONE."; |
| 906 | |
| 907 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 908 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 909 | } |
| 910 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 911 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 912 | const InputEvent* event = consume(); |
| 913 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 914 | << ": consumer should have returned non-NULL event."; |
| 915 | ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType()) |
| 916 | << "Got " << inputEventTypeToString(event->getType()) |
| 917 | << " event instead of DRAG event"; |
| 918 | |
| 919 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 920 | << mName.c_str() << ": event displayId should always be NONE."; |
| 921 | |
| 922 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 923 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 924 | EXPECT_EQ(x, dragEvent.getX()); |
| 925 | EXPECT_EQ(y, dragEvent.getY()); |
| 926 | } |
| 927 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 928 | void consumeTouchModeEvent(bool inTouchMode) { |
| 929 | const InputEvent* event = consume(); |
| 930 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 931 | << ": consumer should have returned non-NULL event."; |
| 932 | ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType()) |
| 933 | << "Got " << inputEventTypeToString(event->getType()) |
| 934 | << " event instead of TOUCH_MODE event"; |
| 935 | |
| 936 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 937 | << mName.c_str() << ": event displayId should always be NONE."; |
| 938 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 939 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 940 | } |
| 941 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 942 | void assertNoEvents() { |
| 943 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 944 | if (event == nullptr) { |
| 945 | return; |
| 946 | } |
| 947 | if (event->getType() == AINPUT_EVENT_TYPE_KEY) { |
| 948 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 949 | ADD_FAILURE() << "Received key event " |
| 950 | << KeyEvent::actionToString(keyEvent.getAction()); |
| 951 | } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) { |
| 952 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 953 | ADD_FAILURE() << "Received motion event " |
| 954 | << MotionEvent::actionToString(motionEvent.getAction()); |
| 955 | } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) { |
| 956 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 957 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 958 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 959 | } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) { |
| 960 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 961 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 962 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 963 | } else if (event->getType() == AINPUT_EVENT_TYPE_TOUCH_MODE) { |
| 964 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 965 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 966 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 967 | } |
| 968 | FAIL() << mName.c_str() |
| 969 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 973 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 974 | int getChannelFd() { return mConsumer->getChannel()->getFd().get(); } |
| 975 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 976 | protected: |
| 977 | std::unique_ptr<InputConsumer> mConsumer; |
| 978 | PreallocatedInputEventFactory mEventFactory; |
| 979 | |
| 980 | std::string mName; |
| 981 | }; |
| 982 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 983 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 984 | public: |
| 985 | static const int32_t WIDTH = 600; |
| 986 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 987 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 988 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 989 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 990 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 991 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 992 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 993 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 994 | dispatcher->createInputChannel(name); |
| 995 | token = (*channel)->getConnectionToken(); |
| 996 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | inputApplicationHandle->updateInfo(); |
| 1000 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1001 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1002 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1003 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1004 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1005 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1006 | mInfo.alpha = 1.0; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1007 | mInfo.frameLeft = 0; |
| 1008 | mInfo.frameTop = 0; |
| 1009 | mInfo.frameRight = WIDTH; |
| 1010 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1011 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1012 | mInfo.globalScaleFactor = 1.0; |
| 1013 | mInfo.touchableRegion.clear(); |
| 1014 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1015 | mInfo.ownerPid = WINDOW_PID; |
| 1016 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1017 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1018 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1019 | } |
| 1020 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1021 | sp<FakeWindowHandle> clone( |
| 1022 | const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1023 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1024 | sp<FakeWindowHandle> handle = |
| 1025 | new FakeWindowHandle(inputApplicationHandle, dispatcher, mInfo.name + "(Mirror)", |
| 1026 | displayId, mInfo.token); |
| 1027 | return handle; |
| 1028 | } |
| 1029 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1030 | void setTouchable(bool touchable) { |
| 1031 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1032 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1033 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1034 | void setFocusable(bool focusable) { |
| 1035 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1036 | } |
| 1037 | |
| 1038 | void setVisible(bool visible) { |
| 1039 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1040 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1041 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1042 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1043 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1046 | void setPaused(bool paused) { |
| 1047 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1048 | } |
| 1049 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1050 | void setPreventSplitting(bool preventSplitting) { |
| 1051 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | void setSlippery(bool slippery) { |
| 1055 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1056 | } |
| 1057 | |
| 1058 | void setWatchOutsideTouch(bool watchOutside) { |
| 1059 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1060 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1061 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1062 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1063 | |
| 1064 | void setInterceptsStylus(bool interceptsStylus) { |
| 1065 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1066 | } |
| 1067 | |
| 1068 | void setDropInput(bool dropInput) { |
| 1069 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1070 | } |
| 1071 | |
| 1072 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1073 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1074 | } |
| 1075 | |
| 1076 | void setNoInputChannel(bool noInputChannel) { |
| 1077 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1078 | } |
| 1079 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1080 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1081 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1082 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1083 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1084 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1085 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1086 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1087 | mInfo.frameLeft = frame.left; |
| 1088 | mInfo.frameTop = frame.top; |
| 1089 | mInfo.frameRight = frame.right; |
| 1090 | mInfo.frameBottom = frame.bottom; |
| 1091 | mInfo.touchableRegion.clear(); |
| 1092 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1093 | |
| 1094 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1095 | ui::Transform translate; |
| 1096 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1097 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1098 | } |
| 1099 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1100 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1101 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1102 | void setIsWallpaper(bool isWallpaper) { |
| 1103 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1104 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1105 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1106 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1107 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1108 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1109 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1110 | void setTrustedOverlay(bool trustedOverlay) { |
| 1111 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1112 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1113 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1114 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1115 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1116 | } |
| 1117 | |
| 1118 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1119 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1120 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1121 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1122 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1123 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1124 | expectedFlags); |
| 1125 | } |
| 1126 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1127 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1128 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
| 1129 | } |
| 1130 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1131 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1132 | int32_t expectedFlags = 0) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1133 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId, |
| 1134 | expectedFlags); |
| 1135 | } |
| 1136 | |
| 1137 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1138 | int32_t expectedFlags = 0) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1139 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId, |
| 1140 | expectedFlags); |
| 1141 | } |
| 1142 | |
| 1143 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1144 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1145 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1146 | } |
| 1147 | |
| 1148 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1149 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1150 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1151 | expectedFlags); |
| 1152 | } |
| 1153 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1154 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1155 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1156 | int32_t expectedFlags = 0) { |
| 1157 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1158 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1159 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1160 | } |
| 1161 | |
| 1162 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1163 | int32_t expectedFlags = 0) { |
| 1164 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1165 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1166 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1167 | } |
| 1168 | |
| 1169 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1170 | int32_t expectedFlags = 0) { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1171 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
| 1172 | expectedFlags); |
| 1173 | } |
| 1174 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1175 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1176 | int32_t expectedFlags = 0) { |
| 1177 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
| 1178 | expectedFlags); |
| 1179 | } |
| 1180 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1181 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1182 | int32_t expectedFlags = 0) { |
| 1183 | InputEvent* event = consume(); |
| 1184 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()); |
| 1185 | const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1186 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked()); |
| 1187 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX()); |
| 1188 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY()); |
| 1189 | } |
| 1190 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1191 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1192 | ASSERT_NE(mInputReceiver, nullptr) |
| 1193 | << "Cannot consume events from a window with no receiver"; |
| 1194 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1195 | } |
| 1196 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1197 | void consumeCaptureEvent(bool hasCapture) { |
| 1198 | ASSERT_NE(mInputReceiver, nullptr) |
| 1199 | << "Cannot consume events from a window with no receiver"; |
| 1200 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1201 | } |
| 1202 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1203 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 1204 | std::optional<int32_t> expectedDisplayId, |
| 1205 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1206 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1207 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1208 | expectedFlags); |
| 1209 | } |
| 1210 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1211 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1212 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1213 | } |
| 1214 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1215 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1216 | ASSERT_NE(mInputReceiver, nullptr) |
| 1217 | << "Cannot consume events from a window with no receiver"; |
| 1218 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1219 | } |
| 1220 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1221 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1222 | if (mInputReceiver == nullptr) { |
| 1223 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1224 | return std::nullopt; |
| 1225 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1226 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | void finishEvent(uint32_t sequenceNum) { |
| 1230 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1231 | mInputReceiver->finishEvent(sequenceNum); |
| 1232 | } |
| 1233 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1234 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1235 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1236 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1237 | } |
| 1238 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1239 | InputEvent* consume() { |
| 1240 | if (mInputReceiver == nullptr) { |
| 1241 | return nullptr; |
| 1242 | } |
| 1243 | return mInputReceiver->consume(); |
| 1244 | } |
| 1245 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1246 | MotionEvent* consumeMotion() { |
| 1247 | InputEvent* event = consume(); |
| 1248 | if (event == nullptr) { |
| 1249 | ADD_FAILURE() << "Consume failed : no event"; |
| 1250 | return nullptr; |
| 1251 | } |
| 1252 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 1253 | ADD_FAILURE() << "Instead of motion event, got " |
| 1254 | << inputEventTypeToString(event->getType()); |
| 1255 | return nullptr; |
| 1256 | } |
| 1257 | return static_cast<MotionEvent*>(event); |
| 1258 | } |
| 1259 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1260 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1261 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1262 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1263 | return; // Can't receive events if the window does not have input channel |
| 1264 | } |
| 1265 | ASSERT_NE(nullptr, mInputReceiver) |
| 1266 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1267 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1268 | } |
| 1269 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1270 | sp<IBinder> getToken() { return mInfo.token; } |
| 1271 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1272 | const std::string& getName() { return mName; } |
| 1273 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1274 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { |
| 1275 | mInfo.ownerPid = ownerPid; |
| 1276 | mInfo.ownerUid = ownerUid; |
| 1277 | } |
| 1278 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1279 | int32_t getPid() const { return mInfo.ownerPid; } |
| 1280 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1281 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1282 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1283 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1284 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1285 | private: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1286 | const std::string mName; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1287 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1288 | 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] | 1289 | }; |
| 1290 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1291 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1292 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1293 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1294 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1295 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1296 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1297 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1298 | bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {}, |
| 1299 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1300 | KeyEvent event; |
| 1301 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1302 | |
| 1303 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1304 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1305 | INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE, |
| 1306 | repeatCount, currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1307 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1308 | if (!allowKeyRepeat) { |
| 1309 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1310 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1311 | // Inject event until dispatch out. |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1312 | return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1313 | } |
| 1314 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1315 | static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1316 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1317 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId); |
| 1318 | } |
| 1319 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1320 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1321 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1322 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1323 | static InputEventInjectionResult injectKeyDownNoRepeat( |
| 1324 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1325 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId, |
| 1326 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
| 1327 | /* allowKeyRepeat */ false); |
| 1328 | } |
| 1329 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1330 | static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1331 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1332 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId); |
| 1333 | } |
| 1334 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1335 | class PointerBuilder { |
| 1336 | public: |
| 1337 | PointerBuilder(int32_t id, int32_t toolType) { |
| 1338 | mProperties.clear(); |
| 1339 | mProperties.id = id; |
| 1340 | mProperties.toolType = toolType; |
| 1341 | mCoords.clear(); |
| 1342 | } |
| 1343 | |
| 1344 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } |
| 1345 | |
| 1346 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } |
| 1347 | |
| 1348 | PointerBuilder& axis(int32_t axis, float value) { |
| 1349 | mCoords.setAxisValue(axis, value); |
| 1350 | return *this; |
| 1351 | } |
| 1352 | |
| 1353 | PointerProperties buildProperties() const { return mProperties; } |
| 1354 | |
| 1355 | PointerCoords buildCoords() const { return mCoords; } |
| 1356 | |
| 1357 | private: |
| 1358 | PointerProperties mProperties; |
| 1359 | PointerCoords mCoords; |
| 1360 | }; |
| 1361 | |
| 1362 | class MotionEventBuilder { |
| 1363 | public: |
| 1364 | MotionEventBuilder(int32_t action, int32_t source) { |
| 1365 | mAction = action; |
| 1366 | mSource = source; |
| 1367 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1368 | } |
| 1369 | |
| 1370 | MotionEventBuilder& eventTime(nsecs_t eventTime) { |
| 1371 | mEventTime = eventTime; |
| 1372 | return *this; |
| 1373 | } |
| 1374 | |
| 1375 | MotionEventBuilder& displayId(int32_t displayId) { |
| 1376 | mDisplayId = displayId; |
| 1377 | return *this; |
| 1378 | } |
| 1379 | |
| 1380 | MotionEventBuilder& actionButton(int32_t actionButton) { |
| 1381 | mActionButton = actionButton; |
| 1382 | return *this; |
| 1383 | } |
| 1384 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1385 | MotionEventBuilder& buttonState(int32_t buttonState) { |
| 1386 | mButtonState = buttonState; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1387 | return *this; |
| 1388 | } |
| 1389 | |
| 1390 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 1391 | mRawXCursorPosition = rawXCursorPosition; |
| 1392 | return *this; |
| 1393 | } |
| 1394 | |
| 1395 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 1396 | mRawYCursorPosition = rawYCursorPosition; |
| 1397 | return *this; |
| 1398 | } |
| 1399 | |
| 1400 | MotionEventBuilder& pointer(PointerBuilder pointer) { |
| 1401 | mPointers.push_back(pointer); |
| 1402 | return *this; |
| 1403 | } |
| 1404 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1405 | MotionEventBuilder& addFlag(uint32_t flags) { |
| 1406 | mFlags |= flags; |
| 1407 | return *this; |
| 1408 | } |
| 1409 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1410 | MotionEvent build() { |
| 1411 | std::vector<PointerProperties> pointerProperties; |
| 1412 | std::vector<PointerCoords> pointerCoords; |
| 1413 | for (const PointerBuilder& pointer : mPointers) { |
| 1414 | pointerProperties.push_back(pointer.buildProperties()); |
| 1415 | pointerCoords.push_back(pointer.buildCoords()); |
| 1416 | } |
| 1417 | |
| 1418 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 1419 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 1420 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 1421 | mPointers.size() == 1) { |
| 1422 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 1423 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 1424 | } |
| 1425 | |
| 1426 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1427 | ui::Transform identityTransform; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1428 | event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC, |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1429 | mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1430 | mButtonState, MotionClassification::NONE, identityTransform, |
| 1431 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 1432 | mRawYCursorPosition, identityTransform, mEventTime, mEventTime, |
| 1433 | mPointers.size(), pointerProperties.data(), pointerCoords.data()); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1434 | |
| 1435 | return event; |
| 1436 | } |
| 1437 | |
| 1438 | private: |
| 1439 | int32_t mAction; |
| 1440 | int32_t mSource; |
| 1441 | nsecs_t mEventTime; |
| 1442 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1443 | int32_t mActionButton{0}; |
| 1444 | int32_t mButtonState{0}; |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1445 | int32_t mFlags{0}; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1446 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1447 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1448 | |
| 1449 | std::vector<PointerBuilder> mPointers; |
| 1450 | }; |
| 1451 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1452 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1453 | const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1454 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1455 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
| 1456 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
| 1457 | return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1458 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1461 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1462 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1463 | int32_t displayId, const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1464 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1465 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1466 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1467 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1468 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
| 1469 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1470 | MotionEvent event = MotionEventBuilder(action, source) |
| 1471 | .displayId(displayId) |
| 1472 | .eventTime(eventTime) |
| 1473 | .rawXCursorPosition(cursorPosition.x) |
| 1474 | .rawYCursorPosition(cursorPosition.y) |
| 1475 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1476 | .x(position.x) |
| 1477 | .y(position.y)) |
| 1478 | .build(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1479 | |
| 1480 | // Inject event until dispatch out. |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1481 | return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid, |
| 1482 | policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1483 | } |
| 1484 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1485 | static InputEventInjectionResult injectMotionDown( |
| 1486 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId, |
| 1487 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1488 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1489 | } |
| 1490 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1491 | static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1492 | int32_t source, int32_t displayId, |
| 1493 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1494 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1497 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1498 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1499 | // Define a valid key event. |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1500 | NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 1501 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, |
| 1502 | KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1503 | |
| 1504 | return args; |
| 1505 | } |
| 1506 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1507 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, |
| 1508 | const std::vector<PointF>& points) { |
| 1509 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1510 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1511 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1512 | } |
| 1513 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1514 | PointerProperties pointerProperties[pointerCount]; |
| 1515 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1516 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1517 | for (size_t i = 0; i < pointerCount; i++) { |
| 1518 | pointerProperties[i].clear(); |
| 1519 | pointerProperties[i].id = i; |
| 1520 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1521 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1522 | pointerCoords[i].clear(); |
| 1523 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1524 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1525 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1526 | |
| 1527 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1528 | // Define a valid motion event. |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1529 | NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1530 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 1531 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1532 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 1533 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1534 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 1535 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1536 | |
| 1537 | return args; |
| 1538 | } |
| 1539 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1540 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1541 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1542 | } |
| 1543 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1544 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1545 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1546 | } |
| 1547 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1548 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1549 | const PointerCaptureRequest& request) { |
| 1550 | return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1551 | } |
| 1552 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1553 | /** |
| 1554 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1555 | * broken channel. |
| 1556 | */ |
| 1557 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1558 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1559 | sp<FakeWindowHandle> window = |
| 1560 | new FakeWindowHandle(application, mDispatcher, "Window that breaks its input channel", |
| 1561 | ADISPLAY_ID_DEFAULT); |
| 1562 | |
| 1563 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1564 | |
| 1565 | // Window closes its channel, but the window remains. |
| 1566 | window->destroyReceiver(); |
| 1567 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1568 | } |
| 1569 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1570 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1571 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1572 | sp<FakeWindowHandle> window = |
| 1573 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1574 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1575 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1576 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1577 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1578 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1579 | |
| 1580 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1581 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1582 | } |
| 1583 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1584 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1585 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1586 | sp<FakeWindowHandle> window = |
| 1587 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1588 | |
| 1589 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1590 | // Inject a MotionEvent to an unknown display. |
| 1591 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1592 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
| 1593 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1594 | |
| 1595 | // Window should receive motion event. |
| 1596 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1597 | } |
| 1598 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1599 | /** |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1600 | * Calling setInputWindows once should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1601 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1602 | * called twice. |
| 1603 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1604 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1605 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1606 | sp<FakeWindowHandle> window = |
| 1607 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1608 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1609 | |
| 1610 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1611 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1612 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1613 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1614 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1615 | |
| 1616 | // Window should receive motion event. |
| 1617 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1618 | } |
| 1619 | |
| 1620 | /** |
| 1621 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1622 | */ |
| 1623 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1624 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1625 | sp<FakeWindowHandle> window = |
| 1626 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1627 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1628 | |
| 1629 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1630 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1631 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1632 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1633 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1634 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1635 | |
| 1636 | // Window should receive motion event. |
| 1637 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1638 | } |
| 1639 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1640 | // The foreground window should receive the first touch down event. |
| 1641 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1642 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1643 | sp<FakeWindowHandle> windowTop = |
| 1644 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 1645 | sp<FakeWindowHandle> windowSecond = |
| 1646 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1647 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1648 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1649 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1650 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1651 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1652 | |
| 1653 | // 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] | 1654 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1655 | windowSecond->assertNoEvents(); |
| 1656 | } |
| 1657 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1658 | /** |
| 1659 | * Two windows: A top window, and a wallpaper behind the window. |
| 1660 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1661 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1662 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1663 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1664 | */ |
| 1665 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1666 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1667 | sp<FakeWindowHandle> foregroundWindow = |
| 1668 | new FakeWindowHandle(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1669 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1670 | sp<FakeWindowHandle> wallpaperWindow = |
| 1671 | new FakeWindowHandle(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1672 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1673 | constexpr int expectedWallpaperFlags = |
| 1674 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1675 | |
| 1676 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1677 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1678 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1679 | {100, 200})) |
| 1680 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1681 | |
| 1682 | // Both foreground window and its wallpaper should receive the touch down |
| 1683 | foregroundWindow->consumeMotionDown(); |
| 1684 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1685 | |
| 1686 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1687 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1688 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1689 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1690 | |
| 1691 | foregroundWindow->consumeMotionMove(); |
| 1692 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1693 | |
| 1694 | // Now the foreground window goes away, but the wallpaper stays |
| 1695 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1696 | foregroundWindow->consumeMotionCancel(); |
| 1697 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1698 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1699 | } |
| 1700 | |
| 1701 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1702 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1703 | * with the following differences: |
| 1704 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1705 | * clean up the connection. |
| 1706 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1707 | * Ensure that there's no crash in the dispatcher. |
| 1708 | */ |
| 1709 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1710 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1711 | sp<FakeWindowHandle> foregroundWindow = |
| 1712 | new FakeWindowHandle(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1713 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1714 | sp<FakeWindowHandle> wallpaperWindow = |
| 1715 | new FakeWindowHandle(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1716 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1717 | constexpr int expectedWallpaperFlags = |
| 1718 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1719 | |
| 1720 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1721 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1722 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1723 | {100, 200})) |
| 1724 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1725 | |
| 1726 | // Both foreground window and its wallpaper should receive the touch down |
| 1727 | foregroundWindow->consumeMotionDown(); |
| 1728 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1729 | |
| 1730 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1731 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1732 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1733 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1734 | |
| 1735 | foregroundWindow->consumeMotionMove(); |
| 1736 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1737 | |
| 1738 | // Wallpaper closes its channel, but the window remains. |
| 1739 | wallpaperWindow->destroyReceiver(); |
| 1740 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1741 | |
| 1742 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1743 | // is no longer valid. |
| 1744 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1745 | foregroundWindow->consumeMotionCancel(); |
| 1746 | } |
| 1747 | |
| 1748 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1749 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1750 | * The top window gets a multitouch gesture. |
| 1751 | * Ensure that wallpaper gets the same gesture. |
| 1752 | */ |
| 1753 | TEST_F(InputDispatcherTest, WallpaperWindow_ReceivesMultiTouch) { |
| 1754 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1755 | sp<FakeWindowHandle> window = |
| 1756 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1757 | window->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1758 | |
| 1759 | sp<FakeWindowHandle> wallpaperWindow = |
| 1760 | new FakeWindowHandle(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1761 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1762 | constexpr int expectedWallpaperFlags = |
| 1763 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1764 | |
| 1765 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, wallpaperWindow}}}); |
| 1766 | |
| 1767 | // Touch down on top window |
| 1768 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1769 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1770 | {100, 100})) |
| 1771 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1772 | |
| 1773 | // Both top window and its wallpaper should receive the touch down |
| 1774 | window->consumeMotionDown(); |
| 1775 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1776 | |
| 1777 | // Second finger down on the top window |
| 1778 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1779 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1780 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1781 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1782 | .x(100) |
| 1783 | .y(100)) |
| 1784 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1785 | .x(150) |
| 1786 | .y(150)) |
| 1787 | .build(); |
| 1788 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1789 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1790 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1791 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1792 | |
| 1793 | window->consumeMotionPointerDown(1 /* pointerIndex */); |
| 1794 | wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT, |
| 1795 | expectedWallpaperFlags); |
| 1796 | window->assertNoEvents(); |
| 1797 | wallpaperWindow->assertNoEvents(); |
| 1798 | } |
| 1799 | |
| 1800 | /** |
| 1801 | * Two windows: a window on the left and window on the right. |
| 1802 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1803 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1804 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1805 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1806 | * ACTION_POINTER_DOWN(1). |
| 1807 | */ |
| 1808 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1809 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1810 | sp<FakeWindowHandle> leftWindow = |
| 1811 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1812 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1813 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1814 | |
| 1815 | sp<FakeWindowHandle> rightWindow = |
| 1816 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1817 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1818 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1819 | |
| 1820 | sp<FakeWindowHandle> wallpaperWindow = |
| 1821 | new FakeWindowHandle(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 1822 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1823 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1824 | constexpr int expectedWallpaperFlags = |
| 1825 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1826 | |
| 1827 | mDispatcher->setInputWindows( |
| 1828 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
| 1829 | |
| 1830 | // Touch down on left window |
| 1831 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1832 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1833 | {100, 100})) |
| 1834 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1835 | |
| 1836 | // Both foreground window and its wallpaper should receive the touch down |
| 1837 | leftWindow->consumeMotionDown(); |
| 1838 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1839 | |
| 1840 | // Second finger down on the right window |
| 1841 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1842 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1843 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1844 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1845 | .x(100) |
| 1846 | .y(100)) |
| 1847 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1848 | .x(300) |
| 1849 | .y(100)) |
| 1850 | .build(); |
| 1851 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1852 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1853 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1854 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1855 | |
| 1856 | leftWindow->consumeMotionMove(); |
| 1857 | // Since the touch is split, right window gets ACTION_DOWN |
| 1858 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1859 | wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT, |
| 1860 | expectedWallpaperFlags); |
| 1861 | |
| 1862 | // Now, leftWindow, which received the first finger, disappears. |
| 1863 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); |
| 1864 | leftWindow->consumeMotionCancel(); |
| 1865 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1866 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1867 | |
| 1868 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 1869 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 1870 | const MotionEvent secondFingerMoveEvent = |
| 1871 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1872 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1873 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1874 | .x(100) |
| 1875 | .y(100)) |
| 1876 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1877 | .x(310) |
| 1878 | .y(110)) |
| 1879 | .build(); |
| 1880 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1881 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 1882 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 1883 | rightWindow->consumeMotionMove(); |
| 1884 | |
| 1885 | leftWindow->assertNoEvents(); |
| 1886 | rightWindow->assertNoEvents(); |
| 1887 | wallpaperWindow->assertNoEvents(); |
| 1888 | } |
| 1889 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1890 | /** |
| 1891 | * On the display, have a single window, and also an area where there's no window. |
| 1892 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 1893 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 1894 | */ |
| 1895 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 1896 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1897 | sp<FakeWindowHandle> window = |
| 1898 | new FakeWindowHandle(application, mDispatcher, "Window", DISPLAY_ID); |
| 1899 | |
| 1900 | mDispatcher->setInputWindows({{DISPLAY_ID, {window}}}); |
| 1901 | NotifyMotionArgs args; |
| 1902 | |
| 1903 | // Touch down on the empty space |
| 1904 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}))); |
| 1905 | |
| 1906 | mDispatcher->waitForIdle(); |
| 1907 | window->assertNoEvents(); |
| 1908 | |
| 1909 | // Now touch down on the window with another pointer |
| 1910 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}))); |
| 1911 | mDispatcher->waitForIdle(); |
| 1912 | window->consumeMotionDown(); |
| 1913 | } |
| 1914 | |
| 1915 | /** |
| 1916 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 1917 | * non-touchable window. |
| 1918 | */ |
| 1919 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 1920 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1921 | sp<FakeWindowHandle> window1 = |
| 1922 | new FakeWindowHandle(application, mDispatcher, "Window1", DISPLAY_ID); |
| 1923 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 1924 | window1->setTouchable(false); |
| 1925 | sp<FakeWindowHandle> window2 = |
| 1926 | new FakeWindowHandle(application, mDispatcher, "Window2", DISPLAY_ID); |
| 1927 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 1928 | |
| 1929 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 1930 | |
| 1931 | NotifyMotionArgs args; |
| 1932 | // Touch down on the non-touchable window |
| 1933 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); |
| 1934 | |
| 1935 | mDispatcher->waitForIdle(); |
| 1936 | window1->assertNoEvents(); |
| 1937 | window2->assertNoEvents(); |
| 1938 | |
| 1939 | // Now touch down on the window with another pointer |
| 1940 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); |
| 1941 | mDispatcher->waitForIdle(); |
| 1942 | window2->consumeMotionDown(); |
| 1943 | } |
| 1944 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1945 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1946 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1947 | sp<FakeWindowHandle> windowLeft = |
| 1948 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1949 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1950 | sp<FakeWindowHandle> windowRight = |
| 1951 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1952 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1953 | |
| 1954 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1955 | |
| 1956 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 1957 | |
| 1958 | // 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] | 1959 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1960 | injectMotionEvent(mDispatcher, |
| 1961 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1962 | AINPUT_SOURCE_MOUSE) |
| 1963 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1964 | .x(900) |
| 1965 | .y(400)) |
| 1966 | .build())); |
| 1967 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1968 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1969 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1970 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1971 | |
| 1972 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1973 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1974 | injectMotionEvent(mDispatcher, |
| 1975 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1976 | AINPUT_SOURCE_MOUSE) |
| 1977 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1978 | .x(300) |
| 1979 | .y(400)) |
| 1980 | .build())); |
| 1981 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1982 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1983 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1984 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1985 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1986 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1987 | |
| 1988 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1989 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1990 | injectMotionEvent(mDispatcher, |
| 1991 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 1992 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1993 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1994 | .x(300) |
| 1995 | .y(400)) |
| 1996 | .build())); |
| 1997 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1998 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1999 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2000 | injectMotionEvent(mDispatcher, |
| 2001 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 2002 | AINPUT_SOURCE_MOUSE) |
| 2003 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2004 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2005 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2006 | .x(300) |
| 2007 | .y(400)) |
| 2008 | .build())); |
| 2009 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 2010 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 2011 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2012 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2013 | injectMotionEvent(mDispatcher, |
| 2014 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2015 | AINPUT_SOURCE_MOUSE) |
| 2016 | .buttonState(0) |
| 2017 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2018 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2019 | .x(300) |
| 2020 | .y(400)) |
| 2021 | .build())); |
| 2022 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2023 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 2024 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2025 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2026 | injectMotionEvent(mDispatcher, |
| 2027 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 2028 | .buttonState(0) |
| 2029 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2030 | .x(300) |
| 2031 | .y(400)) |
| 2032 | .build())); |
| 2033 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2034 | |
| 2035 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2036 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2037 | injectMotionEvent(mDispatcher, |
| 2038 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2039 | AINPUT_SOURCE_MOUSE) |
| 2040 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2041 | .x(900) |
| 2042 | .y(400)) |
| 2043 | .build())); |
| 2044 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 2045 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 2046 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2047 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 2048 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2049 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 2050 | } |
| 2051 | |
| 2052 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 2053 | // directly in this test. |
| 2054 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2055 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2056 | sp<FakeWindowHandle> window = |
| 2057 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2058 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2059 | |
| 2060 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2061 | |
| 2062 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2063 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2064 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2065 | injectMotionEvent(mDispatcher, |
| 2066 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2067 | AINPUT_SOURCE_MOUSE) |
| 2068 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2069 | .x(300) |
| 2070 | .y(400)) |
| 2071 | .build())); |
| 2072 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2073 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 2074 | |
| 2075 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2076 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2077 | injectMotionEvent(mDispatcher, |
| 2078 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2079 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2080 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2081 | .x(300) |
| 2082 | .y(400)) |
| 2083 | .build())); |
| 2084 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2085 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2086 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2087 | injectMotionEvent(mDispatcher, |
| 2088 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 2089 | AINPUT_SOURCE_MOUSE) |
| 2090 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2091 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2092 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2093 | .x(300) |
| 2094 | .y(400)) |
| 2095 | .build())); |
| 2096 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 2097 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 2098 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2099 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2100 | injectMotionEvent(mDispatcher, |
| 2101 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2102 | AINPUT_SOURCE_MOUSE) |
| 2103 | .buttonState(0) |
| 2104 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2105 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2106 | .x(300) |
| 2107 | .y(400)) |
| 2108 | .build())); |
| 2109 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2110 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 2111 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2112 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2113 | injectMotionEvent(mDispatcher, |
| 2114 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 2115 | .buttonState(0) |
| 2116 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2117 | .x(300) |
| 2118 | .y(400)) |
| 2119 | .build())); |
| 2120 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2121 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2122 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2123 | injectMotionEvent(mDispatcher, |
| 2124 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 2125 | AINPUT_SOURCE_MOUSE) |
| 2126 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2127 | .x(300) |
| 2128 | .y(400)) |
| 2129 | .build())); |
| 2130 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 2131 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 2132 | } |
| 2133 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2134 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2135 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2136 | |
| 2137 | sp<FakeWindowHandle> windowLeft = |
| 2138 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2139 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2140 | sp<FakeWindowHandle> windowRight = |
| 2141 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2142 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2143 | |
| 2144 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2145 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2146 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2147 | |
| 2148 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 2149 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2150 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2151 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2152 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2153 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2154 | windowRight->assertNoEvents(); |
| 2155 | } |
| 2156 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2157 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2158 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2159 | sp<FakeWindowHandle> window = |
| 2160 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2161 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2162 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2163 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2164 | setFocusedWindow(window); |
| 2165 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2166 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2167 | |
| 2168 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2169 | mDispatcher->notifyKey(&keyArgs); |
| 2170 | |
| 2171 | // Window should receive key down event. |
| 2172 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2173 | |
| 2174 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 2175 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2176 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2177 | mDispatcher->notifyDeviceReset(&args); |
| 2178 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 2179 | AKEY_EVENT_FLAG_CANCELED); |
| 2180 | } |
| 2181 | |
| 2182 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2183 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2184 | sp<FakeWindowHandle> window = |
| 2185 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2186 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2187 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2188 | |
| 2189 | NotifyMotionArgs motionArgs = |
| 2190 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2191 | ADISPLAY_ID_DEFAULT); |
| 2192 | mDispatcher->notifyMotion(&motionArgs); |
| 2193 | |
| 2194 | // Window should receive motion down event. |
| 2195 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2196 | |
| 2197 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 2198 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2199 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2200 | mDispatcher->notifyDeviceReset(&args); |
| 2201 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, |
| 2202 | 0 /*expectedFlags*/); |
| 2203 | } |
| 2204 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2205 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 2206 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2207 | sp<FakeWindowHandle> window = |
| 2208 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2209 | window->setFocusable(true); |
| 2210 | |
| 2211 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2212 | setFocusedWindow(window); |
| 2213 | |
| 2214 | window->consumeFocusEvent(true); |
| 2215 | |
| 2216 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2217 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 2218 | const nsecs_t injectTime = keyArgs.eventTime; |
| 2219 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
| 2220 | mDispatcher->notifyKey(&keyArgs); |
| 2221 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 2222 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2223 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 2224 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 2225 | } |
| 2226 | |
| 2227 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 2228 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2229 | sp<FakeWindowHandle> window = |
| 2230 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2231 | window->setFocusable(true); |
| 2232 | |
| 2233 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2234 | setFocusedWindow(window); |
| 2235 | |
| 2236 | window->consumeFocusEvent(true); |
| 2237 | |
| 2238 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2239 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 2240 | mFakePolicy->setInterceptKeyTimeout(150ms); |
| 2241 | mDispatcher->notifyKey(&keyDown); |
| 2242 | mDispatcher->notifyKey(&keyUp); |
| 2243 | |
| 2244 | // Window should receive key event immediately when same key up. |
| 2245 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2246 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 2247 | } |
| 2248 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2249 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2250 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 2251 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 2252 | * ACTION_OUTSIDE event is sent per gesture. |
| 2253 | */ |
| 2254 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 2255 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 2256 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2257 | sp<FakeWindowHandle> window = |
| 2258 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 2259 | window->setWatchOutsideTouch(true); |
| 2260 | window->setFrame(Rect{0, 0, 100, 100}); |
| 2261 | sp<FakeWindowHandle> secondWindow = |
| 2262 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
| 2263 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 2264 | sp<FakeWindowHandle> thirdWindow = |
| 2265 | new FakeWindowHandle(application, mDispatcher, "Third Window", ADISPLAY_ID_DEFAULT); |
| 2266 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
| 2267 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}}); |
| 2268 | |
| 2269 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
| 2270 | NotifyMotionArgs motionArgs = |
| 2271 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2272 | ADISPLAY_ID_DEFAULT, {PointF{-10, -10}}); |
| 2273 | mDispatcher->notifyMotion(&motionArgs); |
| 2274 | window->assertNoEvents(); |
| 2275 | secondWindow->assertNoEvents(); |
| 2276 | |
| 2277 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 2278 | // Now, `window` should get ACTION_OUTSIDE. |
| 2279 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2280 | {PointF{-10, -10}, PointF{105, 105}}); |
| 2281 | mDispatcher->notifyMotion(&motionArgs); |
| 2282 | window->consumeMotionOutside(); |
| 2283 | secondWindow->consumeMotionDown(); |
| 2284 | thirdWindow->assertNoEvents(); |
| 2285 | |
| 2286 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 2287 | // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture. |
| 2288 | motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2289 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}); |
| 2290 | mDispatcher->notifyMotion(&motionArgs); |
| 2291 | window->assertNoEvents(); |
| 2292 | secondWindow->consumeMotionMove(); |
| 2293 | thirdWindow->consumeMotionDown(); |
| 2294 | } |
| 2295 | |
| 2296 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2297 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 2298 | * |
| 2299 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 2300 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 2301 | * space. |
| 2302 | */ |
| 2303 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 2304 | public: |
| 2305 | void SetUp() override { |
| 2306 | InputDispatcherTest::SetUp(); |
| 2307 | mDisplayInfos.clear(); |
| 2308 | mWindowInfos.clear(); |
| 2309 | } |
| 2310 | |
| 2311 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 2312 | gui::DisplayInfo info; |
| 2313 | info.displayId = displayId; |
| 2314 | info.transform = transform; |
| 2315 | mDisplayInfos.push_back(std::move(info)); |
| 2316 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 2317 | } |
| 2318 | |
| 2319 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 2320 | mWindowInfos.push_back(*windowHandle->getInfo()); |
| 2321 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 2322 | } |
| 2323 | |
| 2324 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 2325 | // on the display. |
| 2326 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 2327 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 2328 | // respectively. |
| 2329 | ui::Transform displayTransform; |
| 2330 | displayTransform.set(2, 0, 0, 4); |
| 2331 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 2332 | |
| 2333 | std::shared_ptr<FakeApplicationHandle> application = |
| 2334 | std::make_shared<FakeApplicationHandle>(); |
| 2335 | |
| 2336 | // Add two windows to the display. Their frames are represented in the display space. |
| 2337 | sp<FakeWindowHandle> firstWindow = |
| 2338 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2339 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 2340 | addWindow(firstWindow); |
| 2341 | |
| 2342 | sp<FakeWindowHandle> secondWindow = |
| 2343 | new FakeWindowHandle(application, mDispatcher, "Second Window", |
| 2344 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2345 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 2346 | addWindow(secondWindow); |
| 2347 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 2348 | } |
| 2349 | |
| 2350 | private: |
| 2351 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 2352 | std::vector<gui::WindowInfo> mWindowInfos; |
| 2353 | }; |
| 2354 | |
| 2355 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) { |
| 2356 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2357 | // Send down to the first window. The point is represented in the display space. The point is |
| 2358 | // selected so that if the hit test was done with the transform applied to it, then it would |
| 2359 | // end up in the incorrect window. |
| 2360 | NotifyMotionArgs downMotionArgs = |
| 2361 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2362 | ADISPLAY_ID_DEFAULT, {PointF{75, 55}}); |
| 2363 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2364 | |
| 2365 | firstWindow->consumeMotionDown(); |
| 2366 | secondWindow->assertNoEvents(); |
| 2367 | } |
| 2368 | |
| 2369 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 2370 | // the event should be treated as being in the logical display space. |
| 2371 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 2372 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2373 | // Send down to the first window. The point is represented in the logical display space. The |
| 2374 | // point is selected so that if the hit test was done in logical display space, then it would |
| 2375 | // end up in the incorrect window. |
| 2376 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2377 | PointF{75 * 2, 55 * 4}); |
| 2378 | |
| 2379 | firstWindow->consumeMotionDown(); |
| 2380 | secondWindow->assertNoEvents(); |
| 2381 | } |
| 2382 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 2383 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 2384 | // event should be treated as being in the logical display space. |
| 2385 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 2386 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2387 | |
| 2388 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 2389 | ui::Transform injectedEventTransform; |
| 2390 | injectedEventTransform.set(matrix); |
| 2391 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 2392 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 2393 | |
| 2394 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2395 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2396 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 2397 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2398 | .x(untransformedPoint.x) |
| 2399 | .y(untransformedPoint.y)) |
| 2400 | .build(); |
| 2401 | event.transform(matrix); |
| 2402 | |
| 2403 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, |
| 2404 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 2405 | |
| 2406 | firstWindow->consumeMotionDown(); |
| 2407 | secondWindow->assertNoEvents(); |
| 2408 | } |
| 2409 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2410 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 2411 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2412 | |
| 2413 | // Send down to the second window. |
| 2414 | NotifyMotionArgs downMotionArgs = |
| 2415 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2416 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}}); |
| 2417 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2418 | |
| 2419 | firstWindow->assertNoEvents(); |
| 2420 | const MotionEvent* event = secondWindow->consumeMotion(); |
| 2421 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 2422 | |
| 2423 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 2424 | EXPECT_EQ(300, event->getRawX(0)); |
| 2425 | EXPECT_EQ(880, event->getRawY(0)); |
| 2426 | |
| 2427 | // Ensure that the x and y values are in the window's coordinate space. |
| 2428 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 2429 | // the logical display space. This will be the origin of the window space. |
| 2430 | EXPECT_EQ(100, event->getX(0)); |
| 2431 | EXPECT_EQ(80, event->getY(0)); |
| 2432 | } |
| 2433 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2434 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 2435 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2436 | |
| 2437 | class TransferTouchFixture : public InputDispatcherTest, |
| 2438 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 2439 | |
| 2440 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2441 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2442 | |
| 2443 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2444 | sp<FakeWindowHandle> firstWindow = |
| 2445 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 2446 | sp<FakeWindowHandle> secondWindow = |
| 2447 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2448 | |
| 2449 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2450 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2451 | |
| 2452 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2453 | NotifyMotionArgs downMotionArgs = |
| 2454 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2455 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2456 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2457 | // Only the first window should get the down event |
| 2458 | firstWindow->consumeMotionDown(); |
| 2459 | secondWindow->assertNoEvents(); |
| 2460 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2461 | // Transfer touch to the second window |
| 2462 | TransferFunction f = GetParam(); |
| 2463 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 2464 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2465 | // The first window gets cancel and the second gets down |
| 2466 | firstWindow->consumeMotionCancel(); |
| 2467 | secondWindow->consumeMotionDown(); |
| 2468 | |
| 2469 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2470 | NotifyMotionArgs upMotionArgs = |
| 2471 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2472 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2473 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2474 | // The first window gets no events and the second gets up |
| 2475 | firstWindow->assertNoEvents(); |
| 2476 | secondWindow->consumeMotionUp(); |
| 2477 | } |
| 2478 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2479 | /** |
| 2480 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 2481 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 2482 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 2483 | * natural to the user. |
| 2484 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 2485 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 2486 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 2487 | * the first window to the second. |
| 2488 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 2489 | * the other API, as well. |
| 2490 | */ |
| 2491 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 2492 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2493 | |
| 2494 | // Create a couple of windows + a spy window |
| 2495 | sp<FakeWindowHandle> spyWindow = |
| 2496 | new FakeWindowHandle(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2497 | spyWindow->setTrustedOverlay(true); |
| 2498 | spyWindow->setSpy(true); |
| 2499 | sp<FakeWindowHandle> firstWindow = |
| 2500 | new FakeWindowHandle(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
| 2501 | sp<FakeWindowHandle> secondWindow = |
| 2502 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2503 | |
| 2504 | // Add the windows to the dispatcher |
| 2505 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}}); |
| 2506 | |
| 2507 | // Send down to the first window |
| 2508 | NotifyMotionArgs downMotionArgs = |
| 2509 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2510 | ADISPLAY_ID_DEFAULT); |
| 2511 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2512 | // Only the first window and spy should get the down event |
| 2513 | spyWindow->consumeMotionDown(); |
| 2514 | firstWindow->consumeMotionDown(); |
| 2515 | |
| 2516 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 2517 | // if f === 'transferTouch'. |
| 2518 | TransferFunction f = GetParam(); |
| 2519 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 2520 | ASSERT_TRUE(success); |
| 2521 | // The first window gets cancel and the second gets down |
| 2522 | firstWindow->consumeMotionCancel(); |
| 2523 | secondWindow->consumeMotionDown(); |
| 2524 | |
| 2525 | // Send up event to the second window |
| 2526 | NotifyMotionArgs upMotionArgs = |
| 2527 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2528 | ADISPLAY_ID_DEFAULT); |
| 2529 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2530 | // The first window gets no events and the second+spy get up |
| 2531 | firstWindow->assertNoEvents(); |
| 2532 | spyWindow->consumeMotionUp(); |
| 2533 | secondWindow->consumeMotionUp(); |
| 2534 | } |
| 2535 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2536 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2537 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2538 | |
| 2539 | PointF touchPoint = {10, 10}; |
| 2540 | |
| 2541 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2542 | sp<FakeWindowHandle> firstWindow = |
| 2543 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 2544 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2545 | sp<FakeWindowHandle> secondWindow = |
| 2546 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 2547 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2548 | |
| 2549 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2550 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2551 | |
| 2552 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2553 | NotifyMotionArgs downMotionArgs = |
| 2554 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2555 | ADISPLAY_ID_DEFAULT, {touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2556 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2557 | // Only the first window should get the down event |
| 2558 | firstWindow->consumeMotionDown(); |
| 2559 | secondWindow->assertNoEvents(); |
| 2560 | |
| 2561 | // Send pointer down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2562 | NotifyMotionArgs pointerDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2563 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2564 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2565 | mDispatcher->notifyMotion(&pointerDownMotionArgs); |
| 2566 | // Only the first window should get the pointer down event |
| 2567 | firstWindow->consumeMotionPointerDown(1); |
| 2568 | secondWindow->assertNoEvents(); |
| 2569 | |
| 2570 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2571 | TransferFunction f = GetParam(); |
| 2572 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 2573 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2574 | // The first window gets cancel and the second gets down and pointer down |
| 2575 | firstWindow->consumeMotionCancel(); |
| 2576 | secondWindow->consumeMotionDown(); |
| 2577 | secondWindow->consumeMotionPointerDown(1); |
| 2578 | |
| 2579 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2580 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2581 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2582 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2583 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 2584 | // The first window gets nothing and the second gets pointer up |
| 2585 | firstWindow->assertNoEvents(); |
| 2586 | secondWindow->consumeMotionPointerUp(1); |
| 2587 | |
| 2588 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2589 | NotifyMotionArgs upMotionArgs = |
| 2590 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2591 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2592 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2593 | // The first window gets nothing and the second gets up |
| 2594 | firstWindow->assertNoEvents(); |
| 2595 | secondWindow->consumeMotionUp(); |
| 2596 | } |
| 2597 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2598 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 2599 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 2600 | // for the case where there are multiple pointers split across several windows. |
| 2601 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 2602 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2603 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 2604 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2605 | return dispatcher->transferTouch(destChannelToken, |
| 2606 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2607 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2608 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 2609 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2610 | return dispatcher->transferTouchFocus(from, to, |
| 2611 | false /*isDragAndDrop*/); |
| 2612 | })); |
| 2613 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2614 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2615 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2616 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2617 | sp<FakeWindowHandle> firstWindow = |
| 2618 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2619 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2620 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2621 | sp<FakeWindowHandle> secondWindow = |
| 2622 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2623 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2624 | |
| 2625 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2626 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2627 | |
| 2628 | PointF pointInFirst = {300, 200}; |
| 2629 | PointF pointInSecond = {300, 600}; |
| 2630 | |
| 2631 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2632 | NotifyMotionArgs firstDownMotionArgs = |
| 2633 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2634 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2635 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 2636 | // Only the first window should get the down event |
| 2637 | firstWindow->consumeMotionDown(); |
| 2638 | secondWindow->assertNoEvents(); |
| 2639 | |
| 2640 | // Send down to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2641 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2642 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2643 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2644 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 2645 | // The first window gets a move and the second a down |
| 2646 | firstWindow->consumeMotionMove(); |
| 2647 | secondWindow->consumeMotionDown(); |
| 2648 | |
| 2649 | // Transfer touch focus to the second window |
| 2650 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 2651 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 2652 | firstWindow->consumeMotionCancel(); |
| 2653 | secondWindow->consumeMotionPointerDown(1); |
| 2654 | |
| 2655 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2656 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2657 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2658 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2659 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 2660 | // The first window gets nothing and the second gets pointer up |
| 2661 | firstWindow->assertNoEvents(); |
| 2662 | secondWindow->consumeMotionPointerUp(1); |
| 2663 | |
| 2664 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2665 | NotifyMotionArgs upMotionArgs = |
| 2666 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2667 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2668 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2669 | // The first window gets nothing and the second gets up |
| 2670 | firstWindow->assertNoEvents(); |
| 2671 | secondWindow->consumeMotionUp(); |
| 2672 | } |
| 2673 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2674 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 2675 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 2676 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 2677 | // window should get nothing. |
| 2678 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 2679 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2680 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2681 | sp<FakeWindowHandle> firstWindow = |
| 2682 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 2683 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2684 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2685 | sp<FakeWindowHandle> secondWindow = |
| 2686 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
| 2687 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2688 | |
| 2689 | // Add the windows to the dispatcher |
| 2690 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 2691 | |
| 2692 | PointF pointInFirst = {300, 200}; |
| 2693 | PointF pointInSecond = {300, 600}; |
| 2694 | |
| 2695 | // Send down to the first window |
| 2696 | NotifyMotionArgs firstDownMotionArgs = |
| 2697 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2698 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 2699 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 2700 | // Only the first window should get the down event |
| 2701 | firstWindow->consumeMotionDown(); |
| 2702 | secondWindow->assertNoEvents(); |
| 2703 | |
| 2704 | // Send down to the second window |
| 2705 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2706 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2707 | {pointInFirst, pointInSecond}); |
| 2708 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 2709 | // The first window gets a move and the second a down |
| 2710 | firstWindow->consumeMotionMove(); |
| 2711 | secondWindow->consumeMotionDown(); |
| 2712 | |
| 2713 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2714 | const bool transferred = |
| 2715 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2716 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 2717 | ASSERT_FALSE(transferred); |
| 2718 | firstWindow->assertNoEvents(); |
| 2719 | secondWindow->assertNoEvents(); |
| 2720 | |
| 2721 | // The rest of the dispatch should proceed as normal |
| 2722 | // Send pointer up to the second window |
| 2723 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2724 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2725 | {pointInFirst, pointInSecond}); |
| 2726 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 2727 | // The first window gets MOVE and the second gets pointer up |
| 2728 | firstWindow->consumeMotionMove(); |
| 2729 | secondWindow->consumeMotionUp(); |
| 2730 | |
| 2731 | // Send up event to the first window |
| 2732 | NotifyMotionArgs upMotionArgs = |
| 2733 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2734 | ADISPLAY_ID_DEFAULT); |
| 2735 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2736 | // The first window gets nothing and the second gets up |
| 2737 | firstWindow->consumeMotionUp(); |
| 2738 | secondWindow->assertNoEvents(); |
| 2739 | } |
| 2740 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2741 | // This case will create two windows and one mirrored window on the default display and mirror |
| 2742 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 2743 | // the windows info of second display before default display. |
| 2744 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 2745 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2746 | sp<FakeWindowHandle> firstWindowInPrimary = |
| 2747 | new FakeWindowHandle(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
| 2748 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2749 | sp<FakeWindowHandle> secondWindowInPrimary = |
| 2750 | new FakeWindowHandle(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
| 2751 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2752 | |
| 2753 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 2754 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 2755 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2756 | |
| 2757 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 2758 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 2759 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2760 | |
| 2761 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 2762 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 2763 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2764 | |
| 2765 | // Update window info, let it find window handle of second display first. |
| 2766 | mDispatcher->setInputWindows( |
| 2767 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 2768 | {ADISPLAY_ID_DEFAULT, |
| 2769 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 2770 | |
| 2771 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2772 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2773 | {50, 50})) |
| 2774 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2775 | |
| 2776 | // Window should receive motion event. |
| 2777 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2778 | |
| 2779 | // Transfer touch focus |
| 2780 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 2781 | secondWindowInPrimary->getToken())); |
| 2782 | // The first window gets cancel. |
| 2783 | firstWindowInPrimary->consumeMotionCancel(); |
| 2784 | secondWindowInPrimary->consumeMotionDown(); |
| 2785 | |
| 2786 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2787 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2788 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 2789 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2790 | firstWindowInPrimary->assertNoEvents(); |
| 2791 | secondWindowInPrimary->consumeMotionMove(); |
| 2792 | |
| 2793 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2794 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2795 | {150, 50})) |
| 2796 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2797 | firstWindowInPrimary->assertNoEvents(); |
| 2798 | secondWindowInPrimary->consumeMotionUp(); |
| 2799 | } |
| 2800 | |
| 2801 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 2802 | // 'transferTouch' api. |
| 2803 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 2804 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2805 | sp<FakeWindowHandle> firstWindowInPrimary = |
| 2806 | new FakeWindowHandle(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
| 2807 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2808 | sp<FakeWindowHandle> secondWindowInPrimary = |
| 2809 | new FakeWindowHandle(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
| 2810 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2811 | |
| 2812 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 2813 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 2814 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2815 | |
| 2816 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 2817 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 2818 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2819 | |
| 2820 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 2821 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 2822 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2823 | |
| 2824 | // Update window info, let it find window handle of second display first. |
| 2825 | mDispatcher->setInputWindows( |
| 2826 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 2827 | {ADISPLAY_ID_DEFAULT, |
| 2828 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 2829 | |
| 2830 | // Touch on second display. |
| 2831 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2832 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) |
| 2833 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2834 | |
| 2835 | // Window should receive motion event. |
| 2836 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 2837 | |
| 2838 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2839 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 2840 | |
| 2841 | // The first window gets cancel. |
| 2842 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 2843 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 2844 | |
| 2845 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2846 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2847 | SECOND_DISPLAY_ID, {150, 50})) |
| 2848 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2849 | firstWindowInPrimary->assertNoEvents(); |
| 2850 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 2851 | |
| 2852 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2853 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
| 2854 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2855 | firstWindowInPrimary->assertNoEvents(); |
| 2856 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 2857 | } |
| 2858 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2859 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2860 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2861 | sp<FakeWindowHandle> window = |
| 2862 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2863 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2864 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2865 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2866 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2867 | |
| 2868 | window->consumeFocusEvent(true); |
| 2869 | |
| 2870 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2871 | mDispatcher->notifyKey(&keyArgs); |
| 2872 | |
| 2873 | // Window should receive key down event. |
| 2874 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2875 | } |
| 2876 | |
| 2877 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2878 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2879 | sp<FakeWindowHandle> window = |
| 2880 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2881 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2882 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2883 | |
| 2884 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2885 | mDispatcher->notifyKey(&keyArgs); |
| 2886 | mDispatcher->waitForIdle(); |
| 2887 | |
| 2888 | window->assertNoEvents(); |
| 2889 | } |
| 2890 | |
| 2891 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 2892 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2893 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2894 | sp<FakeWindowHandle> window = |
| 2895 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2896 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2897 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2898 | |
| 2899 | // Send key |
| 2900 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2901 | mDispatcher->notifyKey(&keyArgs); |
| 2902 | // Send motion |
| 2903 | NotifyMotionArgs motionArgs = |
| 2904 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2905 | ADISPLAY_ID_DEFAULT); |
| 2906 | mDispatcher->notifyMotion(&motionArgs); |
| 2907 | |
| 2908 | // Window should receive only the motion event |
| 2909 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2910 | window->assertNoEvents(); // Key event or focus event will not be received |
| 2911 | } |
| 2912 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2913 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 2914 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2915 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2916 | sp<FakeWindowHandle> firstWindow = |
| 2917 | new FakeWindowHandle(application, mDispatcher, "First Window", ADISPLAY_ID_DEFAULT); |
| 2918 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2919 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2920 | sp<FakeWindowHandle> secondWindow = |
| 2921 | new FakeWindowHandle(application, mDispatcher, "Second Window", ADISPLAY_ID_DEFAULT); |
| 2922 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2923 | |
| 2924 | // Add the windows to the dispatcher |
| 2925 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 2926 | |
| 2927 | PointF pointInFirst = {300, 200}; |
| 2928 | PointF pointInSecond = {300, 600}; |
| 2929 | |
| 2930 | // Send down to the first window |
| 2931 | NotifyMotionArgs firstDownMotionArgs = |
| 2932 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2933 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 2934 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 2935 | // Only the first window should get the down event |
| 2936 | firstWindow->consumeMotionDown(); |
| 2937 | secondWindow->assertNoEvents(); |
| 2938 | |
| 2939 | // Send down to the second window |
| 2940 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2941 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2942 | {pointInFirst, pointInSecond}); |
| 2943 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 2944 | // The first window gets a move and the second a down |
| 2945 | firstWindow->consumeMotionMove(); |
| 2946 | secondWindow->consumeMotionDown(); |
| 2947 | |
| 2948 | // Send pointer cancel to the second window |
| 2949 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2950 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 2951 | {pointInFirst, pointInSecond}); |
| 2952 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 2953 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 2954 | // The first window gets move and the second gets cancel. |
| 2955 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 2956 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 2957 | |
| 2958 | // Send up event. |
| 2959 | NotifyMotionArgs upMotionArgs = |
| 2960 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2961 | ADISPLAY_ID_DEFAULT); |
| 2962 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2963 | // The first window gets up and the second gets nothing. |
| 2964 | firstWindow->consumeMotionUp(); |
| 2965 | secondWindow->assertNoEvents(); |
| 2966 | } |
| 2967 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 2968 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 2969 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2970 | |
| 2971 | sp<FakeWindowHandle> window = |
| 2972 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2973 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2974 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 2975 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 2976 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 2977 | |
| 2978 | window->sendTimeline(1 /*inputEventId*/, graphicsTimeline); |
| 2979 | window->assertNoEvents(); |
| 2980 | mDispatcher->waitForIdle(); |
| 2981 | } |
| 2982 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2983 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2984 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2985 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 2986 | int32_t displayId) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 2987 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2988 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 2989 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2992 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 2993 | |
| 2994 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 2995 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, |
| 2996 | expectedDisplayId, expectedFlags); |
| 2997 | } |
| 2998 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2999 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 3000 | |
| 3001 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 3002 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3003 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3004 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 3005 | expectedDisplayId, expectedFlags); |
| 3006 | } |
| 3007 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3008 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3009 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 3010 | expectedDisplayId, expectedFlags); |
| 3011 | } |
| 3012 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3013 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3014 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, |
| 3015 | expectedDisplayId, expectedFlags); |
| 3016 | } |
| 3017 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3018 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3019 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 3020 | expectedDisplayId, expectedFlags); |
| 3021 | } |
| 3022 | |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 3023 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 3024 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 3025 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 3026 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT, |
| 3027 | 0 /*expectedFlags*/); |
| 3028 | } |
| 3029 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3030 | MotionEvent* consumeMotion() { |
| 3031 | InputEvent* event = mInputReceiver->consume(); |
| 3032 | if (!event) { |
| 3033 | ADD_FAILURE() << "No event was produced"; |
| 3034 | return nullptr; |
| 3035 | } |
| 3036 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 3037 | ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion"; |
| 3038 | return nullptr; |
| 3039 | } |
| 3040 | return static_cast<MotionEvent*>(event); |
| 3041 | } |
| 3042 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3043 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 3044 | |
| 3045 | private: |
| 3046 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3047 | }; |
| 3048 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3049 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 3050 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3051 | /** |
| 3052 | * Two entities that receive touch: A window, and a global monitor. |
| 3053 | * The touch goes to the window, and then the window disappears. |
| 3054 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 3055 | * for the monitor, as well. |
| 3056 | * 1. foregroundWindow |
| 3057 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 3058 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3059 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3060 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3061 | sp<FakeWindowHandle> window = |
| 3062 | new FakeWindowHandle(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 3063 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3064 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3065 | |
| 3066 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3067 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3068 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3069 | {100, 200})) |
| 3070 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3071 | |
| 3072 | // Both the foreground window and the global monitor should receive the touch down |
| 3073 | window->consumeMotionDown(); |
| 3074 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3075 | |
| 3076 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3077 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3078 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 3079 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3080 | |
| 3081 | window->consumeMotionMove(); |
| 3082 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 3083 | |
| 3084 | // Now the foreground window goes away |
| 3085 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 3086 | window->consumeMotionCancel(); |
| 3087 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 3088 | |
| 3089 | // If more events come in, there will be no more foreground window to send them to. This will |
| 3090 | // cause a cancel for the monitor, as well. |
| 3091 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 3092 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3093 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 3094 | << "Injection should fail because the window was removed"; |
| 3095 | window->assertNoEvents(); |
| 3096 | // Global monitor now gets the cancel |
| 3097 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 3098 | } |
| 3099 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3100 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3101 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3102 | sp<FakeWindowHandle> window = |
| 3103 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3104 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3105 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3106 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3107 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3108 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3109 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3110 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3111 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3112 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3113 | } |
| 3114 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3115 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
| 3116 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3117 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3118 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3119 | sp<FakeWindowHandle> window = |
| 3120 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3121 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3122 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3123 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3124 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3125 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3126 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3127 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3128 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3129 | // Pilfer pointers from the monitor. |
| 3130 | // This should not do anything and the window should continue to receive events. |
| 3131 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3132 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3133 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3134 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3135 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3136 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3137 | |
| 3138 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 3139 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3142 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3143 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3144 | sp<FakeWindowHandle> window = |
| 3145 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3146 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3147 | window->setWindowOffset(20, 40); |
| 3148 | window->setWindowTransform(0, 1, -1, 0); |
| 3149 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3150 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3151 | |
| 3152 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3153 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 3154 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3155 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3156 | MotionEvent* event = monitor.consumeMotion(); |
| 3157 | // Even though window has transform, gesture monitor must not. |
| 3158 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 3159 | } |
| 3160 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3161 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3162 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3163 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3164 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3165 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3166 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3167 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 3168 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3169 | } |
| 3170 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3171 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3172 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3173 | sp<FakeWindowHandle> window = |
| 3174 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3175 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3176 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3177 | |
| 3178 | NotifyMotionArgs motionArgs = |
| 3179 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3180 | ADISPLAY_ID_DEFAULT); |
| 3181 | |
| 3182 | mDispatcher->notifyMotion(&motionArgs); |
| 3183 | // Window should receive motion down event. |
| 3184 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3185 | |
| 3186 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3187 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3188 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 3189 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 3190 | motionArgs.pointerCoords[0].getX() - 10); |
| 3191 | |
| 3192 | mDispatcher->notifyMotion(&motionArgs); |
| 3193 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
| 3194 | 0 /*expectedFlags*/); |
| 3195 | } |
| 3196 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3197 | /** |
| 3198 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 3199 | * the device default right away. In the test scenario, we check both the default value, |
| 3200 | * and the action of enabling / disabling. |
| 3201 | */ |
| 3202 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3203 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3204 | sp<FakeWindowHandle> window = |
| 3205 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 3206 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3207 | |
| 3208 | // Set focused application. |
| 3209 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3210 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3211 | |
| 3212 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3213 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3214 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3215 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 3216 | |
| 3217 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3218 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3219 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3220 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); |
| 3221 | |
| 3222 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 3223 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
| 3224 | /* hasPermission */ true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3225 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3226 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3227 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3228 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3229 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); |
| 3230 | |
| 3231 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3232 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3233 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3234 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); |
| 3235 | |
| 3236 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 3237 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
| 3238 | /* hasPermission */ true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3239 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3240 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3241 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3242 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3243 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 3244 | |
| 3245 | window->assertNoEvents(); |
| 3246 | } |
| 3247 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3248 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3249 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3250 | sp<FakeWindowHandle> window = |
| 3251 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 3252 | |
| 3253 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3254 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3255 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3256 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3257 | setFocusedWindow(window); |
| 3258 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3259 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 3260 | |
| 3261 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 3262 | mDispatcher->notifyKey(&keyArgs); |
| 3263 | |
| 3264 | InputEvent* event = window->consume(); |
| 3265 | ASSERT_NE(event, nullptr); |
| 3266 | |
| 3267 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 3268 | ASSERT_NE(verified, nullptr); |
| 3269 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 3270 | |
| 3271 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 3272 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 3273 | ASSERT_EQ(keyArgs.source, verified->source); |
| 3274 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 3275 | |
| 3276 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 3277 | |
| 3278 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3279 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 3280 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3281 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 3282 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 3283 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 3284 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 3285 | } |
| 3286 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3287 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3288 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3289 | sp<FakeWindowHandle> window = |
| 3290 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 3291 | |
| 3292 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3293 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3294 | ui::Transform transform; |
| 3295 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 3296 | |
| 3297 | gui::DisplayInfo displayInfo; |
| 3298 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 3299 | displayInfo.transform = transform; |
| 3300 | |
| 3301 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3302 | |
| 3303 | NotifyMotionArgs motionArgs = |
| 3304 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3305 | ADISPLAY_ID_DEFAULT); |
| 3306 | mDispatcher->notifyMotion(&motionArgs); |
| 3307 | |
| 3308 | InputEvent* event = window->consume(); |
| 3309 | ASSERT_NE(event, nullptr); |
| 3310 | |
| 3311 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 3312 | ASSERT_NE(verified, nullptr); |
| 3313 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 3314 | |
| 3315 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 3316 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 3317 | EXPECT_EQ(motionArgs.source, verified->source); |
| 3318 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 3319 | |
| 3320 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 3321 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3322 | const vec2 rawXY = |
| 3323 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 3324 | motionArgs.pointerCoords[0].getXYValue()); |
| 3325 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 3326 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3327 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3328 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 3329 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3330 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 3331 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 3332 | } |
| 3333 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3334 | /** |
| 3335 | * Ensure that separate calls to sign the same data are generating the same key. |
| 3336 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 3337 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 3338 | * tests. |
| 3339 | */ |
| 3340 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 3341 | KeyEvent event = getTestKeyEvent(); |
| 3342 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 3343 | |
| 3344 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 3345 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 3346 | ASSERT_EQ(hmac1, hmac2); |
| 3347 | } |
| 3348 | |
| 3349 | /** |
| 3350 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 3351 | */ |
| 3352 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 3353 | KeyEvent event = getTestKeyEvent(); |
| 3354 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 3355 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 3356 | |
| 3357 | verifiedEvent.deviceId += 1; |
| 3358 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3359 | |
| 3360 | verifiedEvent.source += 1; |
| 3361 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3362 | |
| 3363 | verifiedEvent.eventTimeNanos += 1; |
| 3364 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3365 | |
| 3366 | verifiedEvent.displayId += 1; |
| 3367 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3368 | |
| 3369 | verifiedEvent.action += 1; |
| 3370 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3371 | |
| 3372 | verifiedEvent.downTimeNanos += 1; |
| 3373 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3374 | |
| 3375 | verifiedEvent.flags += 1; |
| 3376 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3377 | |
| 3378 | verifiedEvent.keyCode += 1; |
| 3379 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3380 | |
| 3381 | verifiedEvent.scanCode += 1; |
| 3382 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3383 | |
| 3384 | verifiedEvent.metaState += 1; |
| 3385 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3386 | |
| 3387 | verifiedEvent.repeatCount += 1; |
| 3388 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3389 | } |
| 3390 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3391 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 3392 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3393 | sp<FakeWindowHandle> windowTop = |
| 3394 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 3395 | sp<FakeWindowHandle> windowSecond = |
| 3396 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 3397 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3398 | |
| 3399 | // Top window is also focusable but is not granted focus. |
| 3400 | windowTop->setFocusable(true); |
| 3401 | windowSecond->setFocusable(true); |
| 3402 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 3403 | setFocusedWindow(windowSecond); |
| 3404 | |
| 3405 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3406 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3407 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3408 | |
| 3409 | // Focused window should receive event. |
| 3410 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 3411 | windowTop->assertNoEvents(); |
| 3412 | } |
| 3413 | |
| 3414 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 3415 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3416 | sp<FakeWindowHandle> window = |
| 3417 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 3418 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3419 | |
| 3420 | window->setFocusable(true); |
| 3421 | // Release channel for window is no longer valid. |
| 3422 | window->releaseChannel(); |
| 3423 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3424 | setFocusedWindow(window); |
| 3425 | |
| 3426 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3427 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3428 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3429 | |
| 3430 | // window channel is invalid, so it should not receive any input event. |
| 3431 | window->assertNoEvents(); |
| 3432 | } |
| 3433 | |
| 3434 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 3435 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3436 | sp<FakeWindowHandle> window = |
| 3437 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3438 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3439 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3440 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3441 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3442 | setFocusedWindow(window); |
| 3443 | |
| 3444 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3445 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3446 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3447 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3448 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3449 | window->assertNoEvents(); |
| 3450 | } |
| 3451 | |
| 3452 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 3453 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3454 | sp<FakeWindowHandle> windowTop = |
| 3455 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 3456 | sp<FakeWindowHandle> windowSecond = |
| 3457 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 3458 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3459 | |
| 3460 | windowTop->setFocusable(true); |
| 3461 | windowSecond->setFocusable(true); |
| 3462 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 3463 | setFocusedWindow(windowTop); |
| 3464 | windowTop->consumeFocusEvent(true); |
| 3465 | |
| 3466 | setFocusedWindow(windowSecond, windowTop); |
| 3467 | windowSecond->consumeFocusEvent(true); |
| 3468 | windowTop->consumeFocusEvent(false); |
| 3469 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3470 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3471 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3472 | |
| 3473 | // Focused window should receive event. |
| 3474 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 3475 | } |
| 3476 | |
| 3477 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { |
| 3478 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3479 | sp<FakeWindowHandle> windowTop = |
| 3480 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 3481 | sp<FakeWindowHandle> windowSecond = |
| 3482 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 3483 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3484 | |
| 3485 | windowTop->setFocusable(true); |
| 3486 | windowSecond->setFocusable(true); |
| 3487 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 3488 | setFocusedWindow(windowSecond, windowTop); |
| 3489 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3490 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3491 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3492 | |
| 3493 | // Event should be dropped. |
| 3494 | windowTop->assertNoEvents(); |
| 3495 | windowSecond->assertNoEvents(); |
| 3496 | } |
| 3497 | |
| 3498 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 3499 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3500 | sp<FakeWindowHandle> window = |
| 3501 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 3502 | sp<FakeWindowHandle> previousFocusedWindow = |
| 3503 | new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow", |
| 3504 | ADISPLAY_ID_DEFAULT); |
| 3505 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3506 | |
| 3507 | window->setFocusable(true); |
| 3508 | previousFocusedWindow->setFocusable(true); |
| 3509 | window->setVisible(false); |
| 3510 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 3511 | setFocusedWindow(previousFocusedWindow); |
| 3512 | previousFocusedWindow->consumeFocusEvent(true); |
| 3513 | |
| 3514 | // Requesting focus on invisible window takes focus from currently focused window. |
| 3515 | setFocusedWindow(window); |
| 3516 | previousFocusedWindow->consumeFocusEvent(false); |
| 3517 | |
| 3518 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3519 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3520 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3521 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3522 | |
| 3523 | // Window does not get focus event or key down. |
| 3524 | window->assertNoEvents(); |
| 3525 | |
| 3526 | // Window becomes visible. |
| 3527 | window->setVisible(true); |
| 3528 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3529 | |
| 3530 | // Window receives focus event. |
| 3531 | window->consumeFocusEvent(true); |
| 3532 | // Focused window receives key down. |
| 3533 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3534 | } |
| 3535 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 3536 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 3537 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3538 | sp<FakeWindowHandle> window = |
| 3539 | new FakeWindowHandle(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
| 3540 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3541 | |
| 3542 | // window is granted focus. |
| 3543 | window->setFocusable(true); |
| 3544 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3545 | setFocusedWindow(window); |
| 3546 | window->consumeFocusEvent(true); |
| 3547 | |
| 3548 | // When a display is removed window loses focus. |
| 3549 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 3550 | window->consumeFocusEvent(false); |
| 3551 | } |
| 3552 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3553 | /** |
| 3554 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 3555 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 3556 | * of the 'slipperyEnterWindow'. |
| 3557 | * |
| 3558 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 3559 | * a way so that the touched location is no longer covered by the top window. |
| 3560 | * |
| 3561 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 3562 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 3563 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 3564 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 3565 | * with ACTION_DOWN). |
| 3566 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 3567 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 3568 | * |
| 3569 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 3570 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 3571 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 3572 | * |
| 3573 | * In this test, we ensure that the event received by the bottom window has |
| 3574 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 3575 | */ |
| 3576 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 3577 | constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1; |
| 3578 | constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3579 | |
| 3580 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3581 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3582 | |
| 3583 | sp<FakeWindowHandle> slipperyExitWindow = |
| 3584 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3585 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3586 | // Make sure this one overlaps the bottom window |
| 3587 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 3588 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 3589 | // one. Windows with the same owner are not considered to be occluding each other. |
| 3590 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 3591 | |
| 3592 | sp<FakeWindowHandle> slipperyEnterWindow = |
| 3593 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 3594 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3595 | |
| 3596 | mDispatcher->setInputWindows( |
| 3597 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 3598 | |
| 3599 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
| 3600 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3601 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 3602 | mDispatcher->notifyMotion(&args); |
| 3603 | slipperyExitWindow->consumeMotionDown(); |
| 3604 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 3605 | mDispatcher->setInputWindows( |
| 3606 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 3607 | |
| 3608 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3609 | ADISPLAY_ID_DEFAULT, {{51, 51}}); |
| 3610 | mDispatcher->notifyMotion(&args); |
| 3611 | |
| 3612 | slipperyExitWindow->consumeMotionCancel(); |
| 3613 | |
| 3614 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 3615 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 3616 | } |
| 3617 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3618 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 3619 | protected: |
| 3620 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 3621 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 3622 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3623 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3624 | sp<FakeWindowHandle> mWindow; |
| 3625 | |
| 3626 | virtual void SetUp() override { |
| 3627 | mFakePolicy = new FakeInputDispatcherPolicy(); |
| 3628 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3629 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3630 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 3631 | ASSERT_EQ(OK, mDispatcher->start()); |
| 3632 | |
| 3633 | setUpWindow(); |
| 3634 | } |
| 3635 | |
| 3636 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3637 | mApp = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3638 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3639 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3640 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3641 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3642 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3643 | mWindow->consumeFocusEvent(true); |
| 3644 | } |
| 3645 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3646 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3647 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3648 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3649 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
| 3650 | mDispatcher->notifyKey(&keyArgs); |
| 3651 | |
| 3652 | // Window should receive key down event. |
| 3653 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3654 | } |
| 3655 | |
| 3656 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 3657 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 3658 | InputEvent* repeatEvent = mWindow->consume(); |
| 3659 | ASSERT_NE(nullptr, repeatEvent); |
| 3660 | |
| 3661 | uint32_t eventType = repeatEvent->getType(); |
| 3662 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); |
| 3663 | |
| 3664 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 3665 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 3666 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 3667 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 3668 | } |
| 3669 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3670 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3671 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3672 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3673 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
| 3674 | mDispatcher->notifyKey(&keyArgs); |
| 3675 | |
| 3676 | // Window should receive key down event. |
| 3677 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 3678 | 0 /*expectedFlags*/); |
| 3679 | } |
| 3680 | }; |
| 3681 | |
| 3682 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3683 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 3684 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 3685 | expectKeyRepeatOnce(repeatCount); |
| 3686 | } |
| 3687 | } |
| 3688 | |
| 3689 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
| 3690 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 3691 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 3692 | expectKeyRepeatOnce(repeatCount); |
| 3693 | } |
| 3694 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 3695 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3696 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 3697 | expectKeyRepeatOnce(repeatCount); |
| 3698 | } |
| 3699 | } |
| 3700 | |
| 3701 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3702 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3703 | expectKeyRepeatOnce(1 /*repeatCount*/); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3704 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 3705 | mWindow->assertNoEvents(); |
| 3706 | } |
| 3707 | |
| 3708 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
| 3709 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 3710 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 3711 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 3712 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 3713 | // Stale key up from device 1. |
| 3714 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 3715 | // Device 2 is still down, keep repeating |
| 3716 | expectKeyRepeatOnce(2 /*repeatCount*/); |
| 3717 | expectKeyRepeatOnce(3 /*repeatCount*/); |
| 3718 | // Device 2 key up |
| 3719 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 3720 | mWindow->assertNoEvents(); |
| 3721 | } |
| 3722 | |
| 3723 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
| 3724 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 3725 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 3726 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 3727 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 3728 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. |
| 3729 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 3730 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3731 | mWindow->assertNoEvents(); |
| 3732 | } |
| 3733 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 3734 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 3735 | sendAndConsumeKeyDown(DEVICE_ID); |
| 3736 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 3737 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
| 3738 | mDispatcher->notifyDeviceReset(&args); |
| 3739 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 3740 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 3741 | mWindow->assertNoEvents(); |
| 3742 | } |
| 3743 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3744 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3745 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3746 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 3747 | InputEvent* repeatEvent = mWindow->consume(); |
| 3748 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 3749 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 3750 | IdGenerator::getSource(repeatEvent->getId())); |
| 3751 | } |
| 3752 | } |
| 3753 | |
| 3754 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 3755 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3756 | |
| 3757 | std::unordered_set<int32_t> idSet; |
| 3758 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 3759 | InputEvent* repeatEvent = mWindow->consume(); |
| 3760 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 3761 | int32_t id = repeatEvent->getId(); |
| 3762 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 3763 | idSet.insert(id); |
| 3764 | } |
| 3765 | } |
| 3766 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3767 | /* Test InputDispatcher for MultiDisplay */ |
| 3768 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 3769 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3770 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3771 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3772 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3773 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3774 | windowInPrimary = |
| 3775 | new FakeWindowHandle(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3776 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3777 | // Set focus window for primary display, but focused display would be second one. |
| 3778 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3779 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3780 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3781 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3782 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3783 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3784 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3785 | windowInSecondary = |
| 3786 | new FakeWindowHandle(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3787 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3788 | // Set focus display to second one. |
| 3789 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 3790 | // Set focus window for second display. |
| 3791 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3792 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3793 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3794 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3795 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3796 | } |
| 3797 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 3798 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3799 | InputDispatcherTest::TearDown(); |
| 3800 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3801 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3802 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3803 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3804 | windowInSecondary.clear(); |
| 3805 | } |
| 3806 | |
| 3807 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3808 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3809 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3810 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3811 | sp<FakeWindowHandle> windowInSecondary; |
| 3812 | }; |
| 3813 | |
| 3814 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 3815 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3816 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3817 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 3818 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3819 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3820 | windowInSecondary->assertNoEvents(); |
| 3821 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3822 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3823 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3824 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 3825 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3826 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3827 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3828 | } |
| 3829 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3830 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3831 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3832 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3833 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3834 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3835 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3836 | windowInSecondary->assertNoEvents(); |
| 3837 | |
| 3838 | // Test inject a key down without display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3839 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3840 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3841 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3842 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3843 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 3844 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3845 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3846 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3847 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3848 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
| 3849 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3850 | |
| 3851 | // Test inject a key down, should timeout because of no target window. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 3852 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3853 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3854 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3855 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 3856 | windowInSecondary->assertNoEvents(); |
| 3857 | } |
| 3858 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3859 | // Test per-display input monitors for motion event. |
| 3860 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3861 | FakeMonitorReceiver monitorInPrimary = |
| 3862 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 3863 | FakeMonitorReceiver monitorInSecondary = |
| 3864 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3865 | |
| 3866 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3867 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3868 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 3869 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3870 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3871 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3872 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3873 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3874 | |
| 3875 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3876 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3877 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 3878 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3879 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3880 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3881 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3882 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3883 | |
| 3884 | // Test inject a non-pointer motion event. |
| 3885 | // If specific a display, it will dispatch to the focused window of particular display, |
| 3886 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3887 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3888 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 3889 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3890 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3891 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3892 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3893 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | // Test per-display input monitors for key event. |
| 3897 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3898 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3899 | FakeMonitorReceiver monitorInPrimary = |
| 3900 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 3901 | FakeMonitorReceiver monitorInSecondary = |
| 3902 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3903 | |
| 3904 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3905 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3906 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3907 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3908 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3909 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3910 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3911 | } |
| 3912 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3913 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 3914 | sp<FakeWindowHandle> secondWindowInPrimary = |
| 3915 | new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
| 3916 | secondWindowInPrimary->setFocusable(true); |
| 3917 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 3918 | setFocusedWindow(secondWindowInPrimary); |
| 3919 | windowInPrimary->consumeFocusEvent(false); |
| 3920 | secondWindowInPrimary->consumeFocusEvent(true); |
| 3921 | |
| 3922 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3923 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 3924 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3925 | windowInPrimary->assertNoEvents(); |
| 3926 | windowInSecondary->assertNoEvents(); |
| 3927 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3928 | } |
| 3929 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 3930 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 3931 | FakeMonitorReceiver monitorInPrimary = |
| 3932 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 3933 | FakeMonitorReceiver monitorInSecondary = |
| 3934 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 3935 | |
| 3936 | // Test touch down on primary display. |
| 3937 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3938 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 3939 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3940 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3941 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3942 | |
| 3943 | // Test touch down on second display. |
| 3944 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3945 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 3946 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3947 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 3948 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 3949 | |
| 3950 | // Trigger cancel touch. |
| 3951 | mDispatcher->cancelCurrentTouch(); |
| 3952 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 3953 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 3954 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 3955 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 3956 | |
| 3957 | // Test inject a move motion event, no window/monitor should receive the event. |
| 3958 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 3959 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3960 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 3961 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 3962 | windowInPrimary->assertNoEvents(); |
| 3963 | monitorInPrimary.assertNoEvents(); |
| 3964 | |
| 3965 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 3966 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3967 | SECOND_DISPLAY_ID, {110, 200})) |
| 3968 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 3969 | windowInSecondary->assertNoEvents(); |
| 3970 | monitorInSecondary.assertNoEvents(); |
| 3971 | } |
| 3972 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3973 | class InputFilterTest : public InputDispatcherTest { |
| 3974 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3975 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 3976 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3977 | NotifyMotionArgs motionArgs; |
| 3978 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3979 | motionArgs = |
| 3980 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3981 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3982 | motionArgs = |
| 3983 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3984 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3985 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3986 | if (expectToBeFiltered) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3987 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); |
| 3988 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3989 | } else { |
| 3990 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 3991 | } |
| 3992 | } |
| 3993 | |
| 3994 | void testNotifyKey(bool expectToBeFiltered) { |
| 3995 | NotifyKeyArgs keyArgs; |
| 3996 | |
| 3997 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 3998 | mDispatcher->notifyKey(&keyArgs); |
| 3999 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 4000 | mDispatcher->notifyKey(&keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4001 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4002 | |
| 4003 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 4004 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4005 | } else { |
| 4006 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 4007 | } |
| 4008 | } |
| 4009 | }; |
| 4010 | |
| 4011 | // Test InputFilter for MotionEvent |
| 4012 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 4013 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 4014 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 4015 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 4016 | |
| 4017 | // Enable InputFilter |
| 4018 | mDispatcher->setInputFilterEnabled(true); |
| 4019 | // Test touch on both primary and second display, and check if both events are filtered. |
| 4020 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 4021 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 4022 | |
| 4023 | // Disable InputFilter |
| 4024 | mDispatcher->setInputFilterEnabled(false); |
| 4025 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 4026 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 4027 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 4028 | } |
| 4029 | |
| 4030 | // Test InputFilter for KeyEvent |
| 4031 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 4032 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 4033 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 4034 | |
| 4035 | // Enable InputFilter |
| 4036 | mDispatcher->setInputFilterEnabled(true); |
| 4037 | // Send a key event, and check if it is filtered. |
| 4038 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 4039 | |
| 4040 | // Disable InputFilter |
| 4041 | mDispatcher->setInputFilterEnabled(false); |
| 4042 | // Send a key event, and check if it isn't filtered. |
| 4043 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 4044 | } |
| 4045 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4046 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 4047 | // logical display coordinate space. |
| 4048 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 4049 | ui::Transform firstDisplayTransform; |
| 4050 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 4051 | ui::Transform secondDisplayTransform; |
| 4052 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 4053 | |
| 4054 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 4055 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 4056 | displayInfos[0].transform = firstDisplayTransform; |
| 4057 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 4058 | displayInfos[1].transform = secondDisplayTransform; |
| 4059 | |
| 4060 | mDispatcher->onWindowInfosChanged({}, displayInfos); |
| 4061 | |
| 4062 | // Enable InputFilter |
| 4063 | mDispatcher->setInputFilterEnabled(true); |
| 4064 | |
| 4065 | // Ensure the correct transforms are used for the displays. |
| 4066 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); |
| 4067 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); |
| 4068 | } |
| 4069 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4070 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 4071 | protected: |
| 4072 | virtual void SetUp() override { |
| 4073 | InputDispatcherTest::SetUp(); |
| 4074 | |
| 4075 | /** |
| 4076 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 4077 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 4078 | * on. |
| 4079 | */ |
| 4080 | mDispatcher->setInputFilterEnabled(true); |
| 4081 | |
| 4082 | std::shared_ptr<InputApplicationHandle> application = |
| 4083 | std::make_shared<FakeApplicationHandle>(); |
| 4084 | mWindow = |
| 4085 | new FakeWindowHandle(application, mDispatcher, "Test Window", ADISPLAY_ID_DEFAULT); |
| 4086 | |
| 4087 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4088 | mWindow->setFocusable(true); |
| 4089 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4090 | setFocusedWindow(mWindow); |
| 4091 | mWindow->consumeFocusEvent(true); |
| 4092 | } |
| 4093 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4094 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 4095 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4096 | KeyEvent event; |
| 4097 | |
| 4098 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4099 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 4100 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
| 4101 | KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime); |
| 4102 | const int32_t additionalPolicyFlags = |
| 4103 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 4104 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4105 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4106 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 4107 | policyFlags | additionalPolicyFlags)); |
| 4108 | |
| 4109 | InputEvent* received = mWindow->consume(); |
| 4110 | ASSERT_NE(nullptr, received); |
| 4111 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4112 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY); |
| 4113 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); |
| 4114 | ASSERT_EQ(flags, keyEvent.getFlags()); |
| 4115 | } |
| 4116 | |
| 4117 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 4118 | int32_t flags) { |
| 4119 | MotionEvent event; |
| 4120 | PointerProperties pointerProperties[1]; |
| 4121 | PointerCoords pointerCoords[1]; |
| 4122 | pointerProperties[0].clear(); |
| 4123 | pointerProperties[0].id = 0; |
| 4124 | pointerCoords[0].clear(); |
| 4125 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 4126 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 4127 | |
| 4128 | ui::Transform identityTransform; |
| 4129 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4130 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 4131 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 4132 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 4133 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4134 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 4135 | eventTime, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4136 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 4137 | |
| 4138 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 4139 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4140 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4141 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 4142 | policyFlags | additionalPolicyFlags)); |
| 4143 | |
| 4144 | InputEvent* received = mWindow->consume(); |
| 4145 | ASSERT_NE(nullptr, received); |
| 4146 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
| 4147 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 4148 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); |
| 4149 | ASSERT_EQ(flags, motionEvent.getFlags()); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4150 | } |
| 4151 | |
| 4152 | private: |
| 4153 | sp<FakeWindowHandle> mWindow; |
| 4154 | }; |
| 4155 | |
| 4156 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4157 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 4158 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 4159 | // injected device id will be overwritten. |
| 4160 | testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 4161 | 0 /*flags*/); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4164 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4165 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4166 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 4167 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 4168 | } |
| 4169 | |
| 4170 | TEST_F(InputFilterInjectionPolicyTest, |
| 4171 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 4172 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
| 4173 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 4174 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
| 4177 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
| 4178 | testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4179 | VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4180 | } |
| 4181 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4182 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4183 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4184 | InputDispatcherTest::SetUp(); |
| 4185 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4186 | std::shared_ptr<FakeApplicationHandle> application = |
| 4187 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4188 | mUnfocusedWindow = |
| 4189 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4190 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4191 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4192 | mFocusedWindow = |
| 4193 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 4194 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4195 | |
| 4196 | // Set focused application. |
| 4197 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4198 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4199 | |
| 4200 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4201 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4202 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4203 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4204 | } |
| 4205 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4206 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4207 | InputDispatcherTest::TearDown(); |
| 4208 | |
| 4209 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4210 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | protected: |
| 4214 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4215 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4216 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4217 | }; |
| 4218 | |
| 4219 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 4220 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 4221 | // the onPointerDownOutsideFocus callback. |
| 4222 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4223 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4224 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4225 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4226 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4227 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4228 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4229 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4230 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 4231 | } |
| 4232 | |
| 4233 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 4234 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 4235 | // onPointerDownOutsideFocus callback. |
| 4236 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4237 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4238 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4239 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4240 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4241 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4242 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4243 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4244 | } |
| 4245 | |
| 4246 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 4247 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 4248 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4249 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4250 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4251 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4252 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4253 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4254 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4255 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4256 | } |
| 4257 | |
| 4258 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 4259 | // DOWN on the window that already has focus. Ensure no window received the |
| 4260 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4261 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4262 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4263 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4264 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4265 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4266 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4267 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4268 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4269 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4270 | } |
| 4271 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 4272 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 4273 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 4274 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 4275 | const MotionEvent event = |
| 4276 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4277 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 4278 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20)) |
| 4279 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 4280 | .build(); |
| 4281 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) |
| 4282 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4283 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 4284 | |
| 4285 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4286 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 4287 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 4288 | mUnfocusedWindow->assertNoEvents(); |
| 4289 | } |
| 4290 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4291 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 4292 | // windows that point to the same client token. |
| 4293 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 4294 | virtual void SetUp() override { |
| 4295 | InputDispatcherTest::SetUp(); |
| 4296 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4297 | std::shared_ptr<FakeApplicationHandle> application = |
| 4298 | std::make_shared<FakeApplicationHandle>(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4299 | mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1", |
| 4300 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4301 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 4302 | |
| 4303 | mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2", |
| 4304 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4305 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 4306 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4307 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4308 | } |
| 4309 | |
| 4310 | protected: |
| 4311 | sp<FakeWindowHandle> mWindow1; |
| 4312 | sp<FakeWindowHandle> mWindow2; |
| 4313 | |
| 4314 | // 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] | 4315 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4316 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 4317 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4318 | } |
| 4319 | |
| 4320 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 4321 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4322 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4323 | InputEvent* event = window->consume(); |
| 4324 | |
| 4325 | ASSERT_NE(nullptr, event) << name.c_str() |
| 4326 | << ": consumer should have returned non-NULL event."; |
| 4327 | |
| 4328 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 4329 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 4330 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 4331 | |
| 4332 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4333 | assertMotionAction(expectedAction, motionEvent.getAction()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4334 | |
| 4335 | for (size_t i = 0; i < points.size(); i++) { |
| 4336 | float expectedX = points[i].x; |
| 4337 | float expectedY = points[i].y; |
| 4338 | |
| 4339 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 4340 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 4341 | << ", got " << motionEvent.getX(i); |
| 4342 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 4343 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 4344 | << ", got " << motionEvent.getY(i); |
| 4345 | } |
| 4346 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4347 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 4348 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4349 | std::vector<PointF> expectedPoints) { |
| 4350 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 4351 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 4352 | mDispatcher->notifyMotion(&motionArgs); |
| 4353 | |
| 4354 | // Always consume from window1 since it's the window that has the InputReceiver |
| 4355 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 4356 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4357 | }; |
| 4358 | |
| 4359 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 4360 | // Touch Window 1 |
| 4361 | PointF touchedPoint = {10, 10}; |
| 4362 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4363 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4364 | |
| 4365 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4366 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4367 | |
| 4368 | // Touch Window 2 |
| 4369 | touchedPoint = {150, 150}; |
| 4370 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4371 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4372 | } |
| 4373 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4374 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 4375 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4376 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4377 | |
| 4378 | // Touch Window 1 |
| 4379 | PointF touchedPoint = {10, 10}; |
| 4380 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4381 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4382 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4383 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4384 | |
| 4385 | // Touch Window 2 |
| 4386 | touchedPoint = {150, 150}; |
| 4387 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4388 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 4389 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4390 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4391 | // Update the transform so rotation is set |
| 4392 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4393 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 4394 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4395 | } |
| 4396 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4397 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4398 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4399 | |
| 4400 | // Touch Window 1 |
| 4401 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4402 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4403 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4404 | |
| 4405 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4406 | touchedPoints.push_back(PointF{150, 150}); |
| 4407 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4408 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4409 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4410 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4411 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4412 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4413 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4414 | // Update the transform so rotation is set for Window 2 |
| 4415 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4416 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4417 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4420 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4421 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4422 | |
| 4423 | // Touch Window 1 |
| 4424 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4425 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4426 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4427 | |
| 4428 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4429 | touchedPoints.push_back(PointF{150, 150}); |
| 4430 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4431 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4432 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4433 | |
| 4434 | // Move both windows |
| 4435 | touchedPoints = {{20, 20}, {175, 175}}; |
| 4436 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 4437 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 4438 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4439 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4440 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4441 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4442 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4443 | expectedPoints.pop_back(); |
| 4444 | |
| 4445 | // Touch Window 2 |
| 4446 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4447 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4448 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4449 | |
| 4450 | // Move both windows |
| 4451 | touchedPoints = {{20, 20}, {175, 175}}; |
| 4452 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 4453 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 4454 | |
| 4455 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
| 4458 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 4459 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 4460 | |
| 4461 | // Touch Window 1 |
| 4462 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4463 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4464 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4465 | |
| 4466 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4467 | touchedPoints.push_back(PointF{150, 150}); |
| 4468 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4469 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4470 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4471 | |
| 4472 | // Move both windows |
| 4473 | touchedPoints = {{20, 20}, {175, 175}}; |
| 4474 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 4475 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 4476 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4477 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4478 | } |
| 4479 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4480 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 4481 | virtual void SetUp() override { |
| 4482 | InputDispatcherTest::SetUp(); |
| 4483 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4484 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4485 | mApplication->setDispatchingTimeout(20ms); |
| 4486 | mWindow = |
| 4487 | new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 4488 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 4489 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4490 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4491 | |
| 4492 | // Set focused application. |
| 4493 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 4494 | |
| 4495 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4496 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4497 | mWindow->consumeFocusEvent(true); |
| 4498 | } |
| 4499 | |
| 4500 | virtual void TearDown() override { |
| 4501 | InputDispatcherTest::TearDown(); |
| 4502 | mWindow.clear(); |
| 4503 | } |
| 4504 | |
| 4505 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4506 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4507 | sp<FakeWindowHandle> mWindow; |
| 4508 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 4509 | |
| 4510 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4511 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4512 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4513 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4514 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4515 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4516 | WINDOW_LOCATION)); |
| 4517 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4518 | |
| 4519 | sp<FakeWindowHandle> addSpyWindow() { |
| 4520 | sp<FakeWindowHandle> spy = |
| 4521 | new FakeWindowHandle(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4522 | spy->setTrustedOverlay(true); |
| 4523 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4524 | spy->setSpy(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4525 | spy->setDispatchingTimeout(30ms); |
| 4526 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); |
| 4527 | return spy; |
| 4528 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4529 | }; |
| 4530 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4531 | // Send a tap and respond, which should not cause an ANR. |
| 4532 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 4533 | tapOnWindow(); |
| 4534 | mWindow->consumeMotionDown(); |
| 4535 | mWindow->consumeMotionUp(); |
| 4536 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4537 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4538 | } |
| 4539 | |
| 4540 | // Send a regular key and respond, which should not cause an ANR. |
| 4541 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4542 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4543 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 4544 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4545 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4546 | } |
| 4547 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4548 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 4549 | mWindow->setFocusable(false); |
| 4550 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4551 | mWindow->consumeFocusEvent(false); |
| 4552 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4553 | InputEventInjectionResult result = |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4554 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4555 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, |
| 4556 | false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4557 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4558 | // Key will not go to window because we have no focused window. |
| 4559 | // The 'no focused window' ANR timer should start instead. |
| 4560 | |
| 4561 | // Now, the focused application goes away. |
| 4562 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 4563 | // The key should get dropped and there should be no ANR. |
| 4564 | |
| 4565 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4566 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4567 | } |
| 4568 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4569 | // 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] | 4570 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 4571 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4572 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4573 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4574 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4575 | WINDOW_LOCATION)); |
| 4576 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4577 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 4578 | ASSERT_TRUE(sequenceNum); |
| 4579 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4580 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4581 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4582 | mWindow->finishEvent(*sequenceNum); |
| 4583 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 4584 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4585 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4586 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4587 | } |
| 4588 | |
| 4589 | // Send a key to the app and have the app not respond right away. |
| 4590 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 4591 | // Inject a key, and don't respond - expect that ANR is called. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4592 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4593 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 4594 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4595 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4596 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4597 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4598 | } |
| 4599 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4600 | // We have a focused application, but no focused window |
| 4601 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4602 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4603 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4604 | mWindow->consumeFocusEvent(false); |
| 4605 | |
| 4606 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4607 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4608 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4609 | WINDOW_LOCATION)); |
| 4610 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 4611 | mDispatcher->waitForIdle(); |
| 4612 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4613 | |
| 4614 | // Once a focused event arrives, we get an ANR for this application |
| 4615 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 4616 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4617 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4618 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4619 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4620 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4621 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4622 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4623 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4624 | } |
| 4625 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 4626 | /** |
| 4627 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 4628 | * there will not be an ANR. |
| 4629 | */ |
| 4630 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 4631 | mWindow->setFocusable(false); |
| 4632 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4633 | mWindow->consumeFocusEvent(false); |
| 4634 | |
| 4635 | KeyEvent event; |
| 4636 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 4637 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 4638 | |
| 4639 | // Define a valid key down event that is stale (too old). |
| 4640 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 4641 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, |
| 4642 | AMETA_NONE, 1 /*repeatCount*/, eventTime, eventTime); |
| 4643 | |
| 4644 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 4645 | |
| 4646 | InputEventInjectionResult result = |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4647 | mDispatcher->injectInputEvent(&event, {} /* targetUid */, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 4648 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 4649 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 4650 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 4651 | << "Injection should fail because the event is stale"; |
| 4652 | |
| 4653 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4654 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4655 | mWindow->assertNoEvents(); |
| 4656 | } |
| 4657 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4658 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4659 | // Make sure that we don't notify policy twice about the same ANR. |
| 4660 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4661 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4662 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4663 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4664 | |
| 4665 | // Once a focused event arrives, we get an ANR for this application |
| 4666 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 4667 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4668 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4669 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4670 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4671 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4672 | const std::chrono::duration appTimeout = |
| 4673 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4674 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4675 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4676 | std::this_thread::sleep_for(appTimeout); |
| 4677 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 4678 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4679 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4680 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 4681 | // '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] | 4682 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4683 | } |
| 4684 | |
| 4685 | // We have a focused application, but no focused window |
| 4686 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4687 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4688 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4689 | mWindow->consumeFocusEvent(false); |
| 4690 | |
| 4691 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4692 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4693 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4694 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 4695 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4696 | |
| 4697 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4698 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4699 | |
| 4700 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4701 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4702 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4703 | mWindow->assertNoEvents(); |
| 4704 | } |
| 4705 | |
| 4706 | /** |
| 4707 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 4708 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 4709 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 4710 | * the ANR mechanism should still work. |
| 4711 | * |
| 4712 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 4713 | * DOWN event, while not responding on the second one. |
| 4714 | */ |
| 4715 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 4716 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4717 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4718 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 4719 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 4720 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4721 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4722 | |
| 4723 | // Now send ACTION_UP, with identical timestamp |
| 4724 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4725 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 4726 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 4727 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4728 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4729 | |
| 4730 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 4731 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4732 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4733 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4734 | } |
| 4735 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4736 | // A spy window can receive an ANR |
| 4737 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 4738 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 4739 | |
| 4740 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4741 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4742 | WINDOW_LOCATION)); |
| 4743 | mWindow->consumeMotionDown(); |
| 4744 | |
| 4745 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 4746 | ASSERT_TRUE(sequenceNum); |
| 4747 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4748 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4749 | |
| 4750 | spy->finishEvent(*sequenceNum); |
| 4751 | spy->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, |
| 4752 | 0 /*flags*/); |
| 4753 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4754 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | // If an app is not responding to a key event, spy windows should continue to receive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4758 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4759 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 4760 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4761 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4762 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4763 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4764 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4765 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4766 | |
| 4767 | // Stuck on the ACTION_UP |
| 4768 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4769 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4770 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4771 | // New tap will go to the spy window, but not to the window |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4772 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4773 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4774 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4775 | |
| 4776 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 4777 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4778 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4779 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4780 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4781 | } |
| 4782 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4783 | // If an app is not responding to a motion event, spy windows should continue to receive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4784 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4785 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 4786 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4787 | |
| 4788 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4789 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4790 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4791 | |
| 4792 | mWindow->consumeMotionDown(); |
| 4793 | // Stuck on the ACTION_UP |
| 4794 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4795 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4796 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4797 | // New tap will go to the spy window, but not to the window |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4798 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4799 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4800 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4801 | |
| 4802 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 4803 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4804 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4805 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4806 | spy->assertNoEvents(); |
| 4807 | } |
| 4808 | |
| 4809 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
| 4810 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); |
| 4811 | |
| 4812 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4813 | |
| 4814 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4815 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4816 | WINDOW_LOCATION)); |
| 4817 | |
| 4818 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4819 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 4820 | ASSERT_TRUE(consumeSeq); |
| 4821 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4822 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4823 | |
| 4824 | monitor.finishEvent(*consumeSeq); |
| 4825 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4826 | |
| 4827 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4828 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 4832 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 4833 | // get an ANR again. |
| 4834 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 4835 | // 2. consume all pending events (= queue becomes healthy again) |
| 4836 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 4837 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 4838 | tapOnWindow(); |
| 4839 | |
| 4840 | mWindow->consumeMotionDown(); |
| 4841 | // Block on ACTION_UP |
| 4842 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4843 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4844 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 4845 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4846 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4847 | mWindow->assertNoEvents(); |
| 4848 | |
| 4849 | tapOnWindow(); |
| 4850 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4851 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4852 | mWindow->consumeMotionUp(); |
| 4853 | |
| 4854 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4855 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4856 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4857 | mWindow->assertNoEvents(); |
| 4858 | } |
| 4859 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4860 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 4861 | // it. |
| 4862 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4863 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4864 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4865 | WINDOW_LOCATION)); |
| 4866 | |
| 4867 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4868 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4869 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4870 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 4871 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4872 | // 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] | 4873 | mWindow->consumeMotionDown(); |
| 4874 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 4875 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 4876 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4877 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4878 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 4879 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4880 | } |
| 4881 | |
| 4882 | /** |
| 4883 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 4884 | * not to to the focused window until the motion is processed. |
| 4885 | * |
| 4886 | * Warning!!! |
| 4887 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 4888 | * and the injection timeout that we specify when injecting the key. |
| 4889 | * We must have the injection timeout (10ms) be smaller than |
| 4890 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 4891 | * |
| 4892 | * If that value changes, this test should also change. |
| 4893 | */ |
| 4894 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 4895 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 4896 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4897 | |
| 4898 | tapOnWindow(); |
| 4899 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 4900 | ASSERT_TRUE(downSequenceNum); |
| 4901 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 4902 | ASSERT_TRUE(upSequenceNum); |
| 4903 | // Don't finish the events yet, and send a key |
| 4904 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 4905 | // window even if motions are still being processed. But because the injection timeout is short, |
| 4906 | // we will receive INJECTION_TIMED_OUT as the result. |
| 4907 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4908 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4909 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4910 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 4911 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4912 | // Key will not be sent to the window, yet, because the window is still processing events |
| 4913 | // and the key remains pending, waiting for the touch events to be processed |
| 4914 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 4915 | ASSERT_FALSE(keySequenceNum); |
| 4916 | |
| 4917 | std::this_thread::sleep_for(500ms); |
| 4918 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 4919 | // to the focused window, even though we have not yet finished the motion event |
| 4920 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4921 | mWindow->finishEvent(*downSequenceNum); |
| 4922 | mWindow->finishEvent(*upSequenceNum); |
| 4923 | } |
| 4924 | |
| 4925 | /** |
| 4926 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 4927 | * not go to the focused window until the motion is processed. |
| 4928 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 4929 | * focused window right away. |
| 4930 | */ |
| 4931 | TEST_F(InputDispatcherSingleWindowAnr, |
| 4932 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 4933 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 4934 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4935 | |
| 4936 | tapOnWindow(); |
| 4937 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 4938 | ASSERT_TRUE(downSequenceNum); |
| 4939 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 4940 | ASSERT_TRUE(upSequenceNum); |
| 4941 | // Don't finish the events yet, and send a key |
| 4942 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4943 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4944 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4945 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4946 | // At this point, key is still pending, and should not be sent to the application yet. |
| 4947 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 4948 | ASSERT_FALSE(keySequenceNum); |
| 4949 | |
| 4950 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 4951 | tapOnWindow(); |
| 4952 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 4953 | // the other events yet. We can finish events in any order. |
| 4954 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 4955 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 4956 | mWindow->consumeMotionDown(); |
| 4957 | mWindow->consumeMotionUp(); |
| 4958 | mWindow->assertNoEvents(); |
| 4959 | } |
| 4960 | |
| 4961 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 4962 | virtual void SetUp() override { |
| 4963 | InputDispatcherTest::SetUp(); |
| 4964 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4965 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4966 | mApplication->setDispatchingTimeout(10ms); |
| 4967 | mUnfocusedWindow = |
| 4968 | new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT); |
| 4969 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4970 | // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4971 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4972 | |
| 4973 | mFocusedWindow = |
| 4974 | new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 4975 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4976 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4977 | |
| 4978 | // Set focused application. |
| 4979 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4980 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4981 | |
| 4982 | // Expect one focus window exist in display. |
| 4983 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4984 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4985 | mFocusedWindow->consumeFocusEvent(true); |
| 4986 | } |
| 4987 | |
| 4988 | virtual void TearDown() override { |
| 4989 | InputDispatcherTest::TearDown(); |
| 4990 | |
| 4991 | mUnfocusedWindow.clear(); |
| 4992 | mFocusedWindow.clear(); |
| 4993 | } |
| 4994 | |
| 4995 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4996 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4997 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 4998 | sp<FakeWindowHandle> mFocusedWindow; |
| 4999 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 5000 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 5001 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 5002 | |
| 5003 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 5004 | |
| 5005 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 5006 | |
| 5007 | private: |
| 5008 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5009 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5010 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5011 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5012 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5013 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5014 | location)); |
| 5015 | } |
| 5016 | }; |
| 5017 | |
| 5018 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 5019 | // should be ANR'd first. |
| 5020 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5021 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5022 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5023 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5024 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5025 | mFocusedWindow->consumeMotionDown(); |
| 5026 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 5027 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 5028 | // We consumed all events, so no ANR |
| 5029 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5030 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5031 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5032 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5033 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5034 | FOCUSED_WINDOW_LOCATION)); |
| 5035 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 5036 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5037 | |
| 5038 | const std::chrono::duration timeout = |
| 5039 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5040 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5041 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 5042 | // sequence to make it consistent |
| 5043 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5044 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5045 | mFocusedWindow->consumeMotionDown(); |
| 5046 | // This cancel is generated because the connection was unresponsive |
| 5047 | mFocusedWindow->consumeMotionCancel(); |
| 5048 | mFocusedWindow->assertNoEvents(); |
| 5049 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5050 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5051 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 5052 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5053 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5054 | } |
| 5055 | |
| 5056 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 5057 | // it doesn't matter which order they should have ANR. |
| 5058 | // But we should receive ANR for both. |
| 5059 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 5060 | // Set the timeout for unfocused window to match the focused window |
| 5061 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 5062 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 5063 | |
| 5064 | tapOnFocusedWindow(); |
| 5065 | // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5066 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; |
| 5067 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); |
| 5068 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5069 | |
| 5070 | // 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] | 5071 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 5072 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 5073 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 5074 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5075 | |
| 5076 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5077 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5078 | |
| 5079 | mFocusedWindow->consumeMotionDown(); |
| 5080 | mFocusedWindow->consumeMotionUp(); |
| 5081 | mUnfocusedWindow->consumeMotionOutside(); |
| 5082 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5083 | sp<IBinder> responsiveToken1, responsiveToken2; |
| 5084 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); |
| 5085 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5086 | |
| 5087 | // Both applications should be marked as responsive, in any order |
| 5088 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 5089 | mFocusedWindow->getToken() == responsiveToken2); |
| 5090 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 5091 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 5092 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5093 | } |
| 5094 | |
| 5095 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 5096 | // We should also log an error to account for the dropped event (not tested here). |
| 5097 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 5098 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 5099 | tapOnFocusedWindow(); |
| 5100 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 5101 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 5102 | // Receive the events, but don't respond |
| 5103 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 5104 | ASSERT_TRUE(downEventSequenceNum); |
| 5105 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 5106 | ASSERT_TRUE(upEventSequenceNum); |
| 5107 | const std::chrono::duration timeout = |
| 5108 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5109 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5110 | |
| 5111 | // Tap once again |
| 5112 | // 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] | 5113 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5114 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5115 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5116 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5117 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5118 | FOCUSED_WINDOW_LOCATION)); |
| 5119 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 5120 | // valid touch target |
| 5121 | mUnfocusedWindow->assertNoEvents(); |
| 5122 | |
| 5123 | // Consume the first tap |
| 5124 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 5125 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 5126 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5127 | // The second tap did not go to the focused window |
| 5128 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5129 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5130 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 5131 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5132 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5133 | } |
| 5134 | |
| 5135 | // If you tap outside of all windows, there will not be ANR |
| 5136 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5137 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5138 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5139 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 5140 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5141 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5142 | } |
| 5143 | |
| 5144 | // Since the focused window is paused, tapping on it should not produce any events |
| 5145 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 5146 | mFocusedWindow->setPaused(true); |
| 5147 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 5148 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5149 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5150 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5151 | FOCUSED_WINDOW_LOCATION)); |
| 5152 | |
| 5153 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 5154 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5155 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 5156 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5157 | |
| 5158 | mFocusedWindow->assertNoEvents(); |
| 5159 | mUnfocusedWindow->assertNoEvents(); |
| 5160 | } |
| 5161 | |
| 5162 | /** |
| 5163 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 5164 | * not to to the focused window until the motion is processed. |
| 5165 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 5166 | * |
| 5167 | * Warning!!! |
| 5168 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 5169 | * and the injection timeout that we specify when injecting the key. |
| 5170 | * We must have the injection timeout (10ms) be smaller than |
| 5171 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 5172 | * |
| 5173 | * If that value changes, this test should also change. |
| 5174 | */ |
| 5175 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 5176 | // Set a long ANR timeout to prevent it from triggering |
| 5177 | mFocusedWindow->setDispatchingTimeout(2s); |
| 5178 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 5179 | |
| 5180 | tapOnUnfocusedWindow(); |
| 5181 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 5182 | ASSERT_TRUE(downSequenceNum); |
| 5183 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 5184 | ASSERT_TRUE(upSequenceNum); |
| 5185 | // Don't finish the events yet, and send a key |
| 5186 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 5187 | // window even if motions are still being processed. |
| 5188 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5189 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5190 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5191 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 5192 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5193 | // Key will not be sent to the window, yet, because the window is still processing events |
| 5194 | // and the key remains pending, waiting for the touch events to be processed |
| 5195 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 5196 | ASSERT_FALSE(keySequenceNum); |
| 5197 | |
| 5198 | // 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] | 5199 | mFocusedWindow->setFocusable(false); |
| 5200 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5201 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5202 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5203 | |
| 5204 | // Focus events should precede the key events |
| 5205 | mUnfocusedWindow->consumeFocusEvent(true); |
| 5206 | mFocusedWindow->consumeFocusEvent(false); |
| 5207 | |
| 5208 | // Finish the tap events, which should unblock dispatcher |
| 5209 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 5210 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 5211 | |
| 5212 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 5213 | // can finally go to the newly focused "mUnfocusedWindow". |
| 5214 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5215 | mFocusedWindow->assertNoEvents(); |
| 5216 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5217 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5218 | } |
| 5219 | |
| 5220 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 5221 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 5222 | // The other window should not be affected by that. |
| 5223 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 5224 | // Touch Window 1 |
| 5225 | NotifyMotionArgs motionArgs = |
| 5226 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5227 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); |
| 5228 | mDispatcher->notifyMotion(&motionArgs); |
| 5229 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 5230 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 5231 | |
| 5232 | // Touch Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5233 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5234 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5235 | mDispatcher->notifyMotion(&motionArgs); |
| 5236 | |
| 5237 | const std::chrono::duration timeout = |
| 5238 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5239 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5240 | |
| 5241 | mUnfocusedWindow->consumeMotionDown(); |
| 5242 | mFocusedWindow->consumeMotionDown(); |
| 5243 | // Focused window may or may not receive ACTION_MOVE |
| 5244 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 5245 | InputEvent* event; |
| 5246 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 5247 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 5248 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 5249 | ASSERT_NE(nullptr, event); |
| 5250 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 5251 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 5252 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 5253 | mFocusedWindow->consumeMotionCancel(); |
| 5254 | } else { |
| 5255 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 5256 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5257 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5258 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 5259 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5260 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5261 | mUnfocusedWindow->assertNoEvents(); |
| 5262 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5263 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5264 | } |
| 5265 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5266 | /** |
| 5267 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 5268 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 5269 | * |
| 5270 | * If the user touches another application during this time, the key should be dropped. |
| 5271 | * Next, if a new focused window comes in, without toggling the focused application, |
| 5272 | * then no ANR should occur. |
| 5273 | * |
| 5274 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 5275 | * but in some cases the policy may not update the focused application. |
| 5276 | */ |
| 5277 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 5278 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 5279 | std::make_shared<FakeApplicationHandle>(); |
| 5280 | focusedApplication->setDispatchingTimeout(60ms); |
| 5281 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 5282 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 5283 | mFocusedWindow->setFocusable(false); |
| 5284 | |
| 5285 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 5286 | mFocusedWindow->consumeFocusEvent(false); |
| 5287 | |
| 5288 | // Send a key. The ANR timer should start because there is no focused window. |
| 5289 | // 'focusedApplication' will get blamed if this timer completes. |
| 5290 | // 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] | 5291 | InputEventInjectionResult result = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5292 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5293 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, |
| 5294 | false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5295 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5296 | |
| 5297 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 5298 | // then the injected touches won't cause the focused event to get dropped. |
| 5299 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 5300 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 5301 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 5302 | // For this test, it means that the key would get delivered to the window once it becomes |
| 5303 | // focused. |
| 5304 | std::this_thread::sleep_for(10ms); |
| 5305 | |
| 5306 | // Touch unfocused window. This should force the pending key to get dropped. |
| 5307 | NotifyMotionArgs motionArgs = |
| 5308 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5309 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); |
| 5310 | mDispatcher->notifyMotion(&motionArgs); |
| 5311 | |
| 5312 | // We do not consume the motion right away, because that would require dispatcher to first |
| 5313 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 5314 | // Set the focused window first. |
| 5315 | mFocusedWindow->setFocusable(true); |
| 5316 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 5317 | setFocusedWindow(mFocusedWindow); |
| 5318 | mFocusedWindow->consumeFocusEvent(true); |
| 5319 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 5320 | // to another application. This could be a bug / behaviour in the policy. |
| 5321 | |
| 5322 | mUnfocusedWindow->consumeMotionDown(); |
| 5323 | |
| 5324 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5325 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 5326 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 5327 | } |
| 5328 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5329 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 5330 | // that has feature NO_INPUT_CHANNEL. |
| 5331 | // Layout: |
| 5332 | // Top (closest to user) |
| 5333 | // mNoInputWindow (above all windows) |
| 5334 | // mBottomWindow |
| 5335 | // Bottom (furthest from user) |
| 5336 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 5337 | virtual void SetUp() override { |
| 5338 | InputDispatcherTest::SetUp(); |
| 5339 | |
| 5340 | mApplication = std::make_shared<FakeApplicationHandle>(); |
| 5341 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, |
| 5342 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
| 5343 | std::make_optional<sp<IBinder>>(nullptr) /*token*/); |
| 5344 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5345 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5346 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5347 | // It's perfectly valid for this window to not have an associated input channel |
| 5348 | |
| 5349 | mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window", |
| 5350 | ADISPLAY_ID_DEFAULT); |
| 5351 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5352 | |
| 5353 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 5354 | } |
| 5355 | |
| 5356 | protected: |
| 5357 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 5358 | sp<FakeWindowHandle> mNoInputWindow; |
| 5359 | sp<FakeWindowHandle> mBottomWindow; |
| 5360 | }; |
| 5361 | |
| 5362 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 5363 | PointF touchedPoint = {10, 10}; |
| 5364 | |
| 5365 | NotifyMotionArgs motionArgs = |
| 5366 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5367 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 5368 | mDispatcher->notifyMotion(&motionArgs); |
| 5369 | |
| 5370 | mNoInputWindow->assertNoEvents(); |
| 5371 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 5372 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 5373 | // and therefore should prevent mBottomWindow from receiving touches |
| 5374 | mBottomWindow->assertNoEvents(); |
| 5375 | } |
| 5376 | |
| 5377 | /** |
| 5378 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 5379 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 5380 | */ |
| 5381 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 5382 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
| 5383 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, |
| 5384 | "Window with input channel and NO_INPUT_CHANNEL", |
| 5385 | ADISPLAY_ID_DEFAULT); |
| 5386 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5387 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5388 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5389 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 5390 | |
| 5391 | PointF touchedPoint = {10, 10}; |
| 5392 | |
| 5393 | NotifyMotionArgs motionArgs = |
| 5394 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5395 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 5396 | mDispatcher->notifyMotion(&motionArgs); |
| 5397 | |
| 5398 | mNoInputWindow->assertNoEvents(); |
| 5399 | mBottomWindow->assertNoEvents(); |
| 5400 | } |
| 5401 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5402 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 5403 | protected: |
| 5404 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 5405 | sp<FakeWindowHandle> mWindow; |
| 5406 | sp<FakeWindowHandle> mMirror; |
| 5407 | |
| 5408 | virtual void SetUp() override { |
| 5409 | InputDispatcherTest::SetUp(); |
| 5410 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 5411 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 5412 | mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT, |
| 5413 | mWindow->getToken()); |
| 5414 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 5415 | mWindow->setFocusable(true); |
| 5416 | mMirror->setFocusable(true); |
| 5417 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5418 | } |
| 5419 | }; |
| 5420 | |
| 5421 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 5422 | // Request focus on a mirrored window |
| 5423 | setFocusedWindow(mMirror); |
| 5424 | |
| 5425 | // window gets focused |
| 5426 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5427 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5428 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5429 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5430 | } |
| 5431 | |
| 5432 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 5433 | // focusable. |
| 5434 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 5435 | setFocusedWindow(mMirror); |
| 5436 | |
| 5437 | // window gets focused |
| 5438 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5439 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5440 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5441 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5442 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5443 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5444 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5445 | |
| 5446 | mMirror->setFocusable(false); |
| 5447 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5448 | |
| 5449 | // window loses focus since one of the windows associated with the token in not focusable |
| 5450 | mWindow->consumeFocusEvent(false); |
| 5451 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5452 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5453 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5454 | mWindow->assertNoEvents(); |
| 5455 | } |
| 5456 | |
| 5457 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 5458 | // invisible. |
| 5459 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 5460 | setFocusedWindow(mMirror); |
| 5461 | |
| 5462 | // window gets focused |
| 5463 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5464 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5465 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5466 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5467 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5468 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5469 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5470 | |
| 5471 | mMirror->setVisible(false); |
| 5472 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5473 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5474 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5475 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5476 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5477 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5478 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5479 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5480 | |
| 5481 | mWindow->setVisible(false); |
| 5482 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5483 | |
| 5484 | // window loses focus only after all windows associated with the token become invisible. |
| 5485 | mWindow->consumeFocusEvent(false); |
| 5486 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5487 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5488 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5489 | mWindow->assertNoEvents(); |
| 5490 | } |
| 5491 | |
| 5492 | // A focused & mirrored window remains focused until both windows are removed. |
| 5493 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 5494 | setFocusedWindow(mMirror); |
| 5495 | |
| 5496 | // window gets focused |
| 5497 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5498 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5499 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5500 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5501 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5502 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5503 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5504 | |
| 5505 | // single window is removed but the window token remains focused |
| 5506 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 5507 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5508 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5509 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5510 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5511 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5512 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5513 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5514 | |
| 5515 | // Both windows are removed |
| 5516 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 5517 | mWindow->consumeFocusEvent(false); |
| 5518 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5519 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5520 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5521 | mWindow->assertNoEvents(); |
| 5522 | } |
| 5523 | |
| 5524 | // Focus request can be pending until one window becomes visible. |
| 5525 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 5526 | // Request focus on an invisible mirror. |
| 5527 | mWindow->setVisible(false); |
| 5528 | mMirror->setVisible(false); |
| 5529 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5530 | setFocusedWindow(mMirror); |
| 5531 | |
| 5532 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5533 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5534 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5535 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5536 | |
| 5537 | mMirror->setVisible(true); |
| 5538 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5539 | |
| 5540 | // window gets focused |
| 5541 | mWindow->consumeFocusEvent(true); |
| 5542 | // window gets the pending key event |
| 5543 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5544 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5545 | |
| 5546 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 5547 | protected: |
| 5548 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 5549 | sp<FakeWindowHandle> mWindow; |
| 5550 | sp<FakeWindowHandle> mSecondWindow; |
| 5551 | |
| 5552 | void SetUp() override { |
| 5553 | InputDispatcherTest::SetUp(); |
| 5554 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 5555 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 5556 | mWindow->setFocusable(true); |
| 5557 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
| 5558 | mSecondWindow->setFocusable(true); |
| 5559 | |
| 5560 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 5561 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 5562 | |
| 5563 | setFocusedWindow(mWindow); |
| 5564 | mWindow->consumeFocusEvent(true); |
| 5565 | } |
| 5566 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5567 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
| 5568 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5569 | mDispatcher->notifyPointerCaptureChanged(&args); |
| 5570 | } |
| 5571 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5572 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 5573 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5574 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5575 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 5576 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5577 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5578 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5579 | } |
| 5580 | }; |
| 5581 | |
| 5582 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 5583 | // Ensure that capture cannot be obtained for unfocused windows. |
| 5584 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 5585 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 5586 | mSecondWindow->assertNoEvents(); |
| 5587 | |
| 5588 | // Ensure that capture can be enabled from the focus window. |
| 5589 | requestAndVerifyPointerCapture(mWindow, true); |
| 5590 | |
| 5591 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 5592 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 5593 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 5594 | |
| 5595 | // Ensure that capture can be disabled from the window with capture. |
| 5596 | requestAndVerifyPointerCapture(mWindow, false); |
| 5597 | } |
| 5598 | |
| 5599 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5600 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5601 | |
| 5602 | setFocusedWindow(mSecondWindow); |
| 5603 | |
| 5604 | // Ensure that the capture disabled event was sent first. |
| 5605 | mWindow->consumeCaptureEvent(false); |
| 5606 | mWindow->consumeFocusEvent(false); |
| 5607 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5608 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5609 | |
| 5610 | // 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] | 5611 | notifyPointerCaptureChanged({}); |
| 5612 | notifyPointerCaptureChanged(request); |
| 5613 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5614 | mWindow->assertNoEvents(); |
| 5615 | mSecondWindow->assertNoEvents(); |
| 5616 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 5617 | } |
| 5618 | |
| 5619 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5620 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5621 | |
| 5622 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5623 | notifyPointerCaptureChanged({}); |
| 5624 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5625 | |
| 5626 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5627 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5628 | mWindow->consumeCaptureEvent(false); |
| 5629 | mWindow->assertNoEvents(); |
| 5630 | } |
| 5631 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 5632 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 5633 | requestAndVerifyPointerCapture(mWindow, true); |
| 5634 | |
| 5635 | // The first window loses focus. |
| 5636 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5637 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 5638 | mWindow->consumeCaptureEvent(false); |
| 5639 | |
| 5640 | // Request Pointer Capture from the second window before the notification from InputReader |
| 5641 | // arrives. |
| 5642 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5643 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 5644 | |
| 5645 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5646 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 5647 | |
| 5648 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5649 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 5650 | |
| 5651 | mSecondWindow->consumeFocusEvent(true); |
| 5652 | mSecondWindow->consumeCaptureEvent(true); |
| 5653 | } |
| 5654 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5655 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 5656 | // App repeatedly enables and disables capture. |
| 5657 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 5658 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 5659 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 5660 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 5661 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 5662 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 5663 | |
| 5664 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 5665 | // first request is now stale, this should do nothing. |
| 5666 | notifyPointerCaptureChanged(firstRequest); |
| 5667 | mWindow->assertNoEvents(); |
| 5668 | |
| 5669 | // InputReader notifies that the second request was enabled. |
| 5670 | notifyPointerCaptureChanged(secondRequest); |
| 5671 | mWindow->consumeCaptureEvent(true); |
| 5672 | } |
| 5673 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 5674 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 5675 | requestAndVerifyPointerCapture(mWindow, true); |
| 5676 | |
| 5677 | // App toggles pointer capture off and on. |
| 5678 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 5679 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 5680 | |
| 5681 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 5682 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 5683 | |
| 5684 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 5685 | // preceding "disable" request. |
| 5686 | notifyPointerCaptureChanged(enableRequest); |
| 5687 | |
| 5688 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 5689 | // any notifications. |
| 5690 | mWindow->assertNoEvents(); |
| 5691 | } |
| 5692 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5693 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 5694 | protected: |
| 5695 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5696 | |
| 5697 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 5698 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 5699 | |
| 5700 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 5701 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 5702 | |
| 5703 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 5704 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 5705 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 5706 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 5707 | MAXIMUM_OBSCURING_OPACITY); |
| 5708 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5709 | static const int32_t TOUCHED_APP_UID = 10001; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5710 | static const int32_t APP_B_UID = 10002; |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5711 | static const int32_t APP_C_UID = 10003; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5712 | |
| 5713 | sp<FakeWindowHandle> mTouchWindow; |
| 5714 | |
| 5715 | virtual void SetUp() override { |
| 5716 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5717 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5718 | mDispatcher->setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode::BLOCK); |
| 5719 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 5720 | } |
| 5721 | |
| 5722 | virtual void TearDown() override { |
| 5723 | InputDispatcherTest::TearDown(); |
| 5724 | mTouchWindow.clear(); |
| 5725 | } |
| 5726 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5727 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, |
| 5728 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5729 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5730 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5731 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5732 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5733 | return window; |
| 5734 | } |
| 5735 | |
| 5736 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { |
| 5737 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 5738 | sp<FakeWindowHandle> window = |
| 5739 | new FakeWindowHandle(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
| 5740 | // Generate an arbitrary PID based on the UID |
| 5741 | window->setOwnerInfo(1777 + (uid % 10000), uid); |
| 5742 | return window; |
| 5743 | } |
| 5744 | |
| 5745 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
| 5746 | NotifyMotionArgs args = |
| 5747 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5748 | ADISPLAY_ID_DEFAULT, points); |
| 5749 | mDispatcher->notifyMotion(&args); |
| 5750 | } |
| 5751 | }; |
| 5752 | |
| 5753 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5754 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5755 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5756 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5757 | |
| 5758 | touch(); |
| 5759 | |
| 5760 | mTouchWindow->assertNoEvents(); |
| 5761 | } |
| 5762 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5763 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5764 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 5765 | const sp<FakeWindowHandle>& w = |
| 5766 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 5767 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5768 | |
| 5769 | touch(); |
| 5770 | |
| 5771 | mTouchWindow->assertNoEvents(); |
| 5772 | } |
| 5773 | |
| 5774 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5775 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 5776 | const sp<FakeWindowHandle>& w = |
| 5777 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 5778 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5779 | |
| 5780 | touch(); |
| 5781 | |
| 5782 | w->assertNoEvents(); |
| 5783 | } |
| 5784 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5785 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5786 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 5787 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5788 | |
| 5789 | touch(); |
| 5790 | |
| 5791 | mTouchWindow->consumeAnyMotionDown(); |
| 5792 | } |
| 5793 | |
| 5794 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5795 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5796 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5797 | w->setFrame(Rect(0, 0, 50, 50)); |
| 5798 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5799 | |
| 5800 | touch({PointF{100, 100}}); |
| 5801 | |
| 5802 | mTouchWindow->consumeAnyMotionDown(); |
| 5803 | } |
| 5804 | |
| 5805 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5806 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5807 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5808 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5809 | |
| 5810 | touch(); |
| 5811 | |
| 5812 | mTouchWindow->consumeAnyMotionDown(); |
| 5813 | } |
| 5814 | |
| 5815 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 5816 | const sp<FakeWindowHandle>& w = |
| 5817 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 5818 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5819 | |
| 5820 | touch(); |
| 5821 | |
| 5822 | mTouchWindow->consumeAnyMotionDown(); |
| 5823 | } |
| 5824 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5825 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 5826 | const sp<FakeWindowHandle>& w = |
| 5827 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 5828 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5829 | |
| 5830 | touch(); |
| 5831 | |
| 5832 | w->assertNoEvents(); |
| 5833 | } |
| 5834 | |
| 5835 | /** |
| 5836 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 5837 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 5838 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 5839 | */ |
| 5840 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5841 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 5842 | const sp<FakeWindowHandle>& w = |
| 5843 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5844 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5845 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5846 | |
| 5847 | touch(); |
| 5848 | |
| 5849 | w->consumeMotionOutside(); |
| 5850 | } |
| 5851 | |
| 5852 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 5853 | const sp<FakeWindowHandle>& w = |
| 5854 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5855 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5856 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5857 | |
| 5858 | touch(); |
| 5859 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5860 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 5861 | } |
| 5862 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5863 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5864 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5865 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5866 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5867 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5868 | |
| 5869 | touch(); |
| 5870 | |
| 5871 | mTouchWindow->consumeAnyMotionDown(); |
| 5872 | } |
| 5873 | |
| 5874 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 5875 | const sp<FakeWindowHandle>& w = |
| 5876 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5877 | MAXIMUM_OBSCURING_OPACITY); |
| 5878 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5879 | |
| 5880 | touch(); |
| 5881 | |
| 5882 | mTouchWindow->consumeAnyMotionDown(); |
| 5883 | } |
| 5884 | |
| 5885 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5886 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5887 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5888 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5889 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5890 | |
| 5891 | touch(); |
| 5892 | |
| 5893 | mTouchWindow->assertNoEvents(); |
| 5894 | } |
| 5895 | |
| 5896 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 5897 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 5898 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5899 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 5900 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5901 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5902 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 5903 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5904 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 5905 | |
| 5906 | touch(); |
| 5907 | |
| 5908 | mTouchWindow->assertNoEvents(); |
| 5909 | } |
| 5910 | |
| 5911 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 5912 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 5913 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5914 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 5915 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5916 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5917 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 5918 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5919 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 5920 | |
| 5921 | touch(); |
| 5922 | |
| 5923 | mTouchWindow->consumeAnyMotionDown(); |
| 5924 | } |
| 5925 | |
| 5926 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5927 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 5928 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5929 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5930 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5931 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5932 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 5933 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5934 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 5935 | |
| 5936 | touch(); |
| 5937 | |
| 5938 | mTouchWindow->consumeAnyMotionDown(); |
| 5939 | } |
| 5940 | |
| 5941 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 5942 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5943 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5944 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5945 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5946 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 5947 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 5948 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 5949 | |
| 5950 | touch(); |
| 5951 | |
| 5952 | mTouchWindow->assertNoEvents(); |
| 5953 | } |
| 5954 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5955 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5956 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 5957 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5958 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 5959 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5960 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5961 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5962 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5963 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 5964 | |
| 5965 | touch(); |
| 5966 | |
| 5967 | mTouchWindow->assertNoEvents(); |
| 5968 | } |
| 5969 | |
| 5970 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 5971 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 5972 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5973 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 5974 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5975 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5976 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 5977 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5978 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 5979 | |
| 5980 | touch(); |
| 5981 | |
| 5982 | mTouchWindow->consumeAnyMotionDown(); |
| 5983 | } |
| 5984 | |
| 5985 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 5986 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 5987 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 5988 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 5989 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 5990 | |
| 5991 | touch(); |
| 5992 | |
| 5993 | mTouchWindow->consumeAnyMotionDown(); |
| 5994 | } |
| 5995 | |
| 5996 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 5997 | const sp<FakeWindowHandle>& w = |
| 5998 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 5999 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6000 | |
| 6001 | touch(); |
| 6002 | |
| 6003 | mTouchWindow->consumeAnyMotionDown(); |
| 6004 | } |
| 6005 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 6006 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6007 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 6008 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 6009 | const sp<FakeWindowHandle>& w = |
| 6010 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 6011 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6012 | |
| 6013 | touch(); |
| 6014 | |
| 6015 | mTouchWindow->assertNoEvents(); |
| 6016 | } |
| 6017 | |
| 6018 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 6019 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 6020 | const sp<FakeWindowHandle>& w = |
| 6021 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 6022 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6023 | |
| 6024 | touch(); |
| 6025 | |
| 6026 | mTouchWindow->consumeAnyMotionDown(); |
| 6027 | } |
| 6028 | |
| 6029 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6030 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 6031 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 6032 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6033 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6034 | OPACITY_ABOVE_THRESHOLD); |
| 6035 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6036 | |
| 6037 | touch(); |
| 6038 | |
| 6039 | mTouchWindow->consumeAnyMotionDown(); |
| 6040 | } |
| 6041 | |
| 6042 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6043 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 6044 | const sp<FakeWindowHandle>& w1 = |
| 6045 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 6046 | OPACITY_BELOW_THRESHOLD); |
| 6047 | const sp<FakeWindowHandle>& w2 = |
| 6048 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6049 | OPACITY_BELOW_THRESHOLD); |
| 6050 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6051 | |
| 6052 | touch(); |
| 6053 | |
| 6054 | mTouchWindow->assertNoEvents(); |
| 6055 | } |
| 6056 | |
| 6057 | /** |
| 6058 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 6059 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 6060 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 6061 | */ |
| 6062 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6063 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 6064 | const sp<FakeWindowHandle>& wB = |
| 6065 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 6066 | OPACITY_BELOW_THRESHOLD); |
| 6067 | const sp<FakeWindowHandle>& wC = |
| 6068 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6069 | OPACITY_BELOW_THRESHOLD); |
| 6070 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 6071 | |
| 6072 | touch(); |
| 6073 | |
| 6074 | mTouchWindow->assertNoEvents(); |
| 6075 | } |
| 6076 | |
| 6077 | /** |
| 6078 | * This test is testing that a window from a different UID but with same application token doesn't |
| 6079 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 6080 | */ |
| 6081 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6082 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 6083 | const sp<FakeWindowHandle>& w = |
| 6084 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6085 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 6086 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6087 | |
| 6088 | touch(); |
| 6089 | |
| 6090 | mTouchWindow->consumeAnyMotionDown(); |
| 6091 | } |
| 6092 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6093 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 6094 | protected: |
| 6095 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6096 | sp<FakeWindowHandle> mWindow; |
| 6097 | sp<FakeWindowHandle> mSecondWindow; |
| 6098 | sp<FakeWindowHandle> mDragWindow; |
| 6099 | |
| 6100 | void SetUp() override { |
| 6101 | InputDispatcherTest::SetUp(); |
| 6102 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 6103 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 6104 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6105 | |
| 6106 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
| 6107 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6108 | |
| 6109 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 6110 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 6111 | } |
| 6112 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6113 | void injectDown() { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6114 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6115 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6116 | {50, 50})) |
| 6117 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6118 | |
| 6119 | // Window should receive motion event. |
| 6120 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6121 | } |
| 6122 | |
| 6123 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 6124 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 6125 | // Returns true on success. |
| 6126 | bool performDrag(bool sendDown = true) { |
| 6127 | if (sendDown) { |
| 6128 | injectDown(); |
| 6129 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6130 | |
| 6131 | // The drag window covers the entire display |
| 6132 | mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
| 6133 | mDispatcher->setInputWindows( |
| 6134 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 6135 | |
| 6136 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6137 | bool transferred = |
| 6138 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
| 6139 | true /* isDragDrop */); |
| 6140 | if (transferred) { |
| 6141 | mWindow->consumeMotionCancel(); |
| 6142 | mDragWindow->consumeMotionDown(); |
| 6143 | } |
| 6144 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6145 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 6146 | |
| 6147 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 6148 | void performStylusDrag() { |
| 6149 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6150 | injectMotionEvent(mDispatcher, |
| 6151 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 6152 | AINPUT_SOURCE_STYLUS) |
| 6153 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 6154 | .pointer(PointerBuilder(0, |
| 6155 | AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6156 | .x(50) |
| 6157 | .y(50)) |
| 6158 | .build())); |
| 6159 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6160 | |
| 6161 | // The drag window covers the entire display |
| 6162 | mDragWindow = new FakeWindowHandle(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
| 6163 | mDispatcher->setInputWindows( |
| 6164 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 6165 | |
| 6166 | // Transfer touch focus to the drag window |
| 6167 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
| 6168 | true /* isDragDrop */); |
| 6169 | mWindow->consumeMotionCancel(); |
| 6170 | mDragWindow->consumeMotionDown(); |
| 6171 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6172 | }; |
| 6173 | |
| 6174 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
| 6175 | performDrag(); |
| 6176 | |
| 6177 | // Move on window. |
| 6178 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6179 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6180 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6181 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6182 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6183 | mWindow->consumeDragEvent(false, 50, 50); |
| 6184 | mSecondWindow->assertNoEvents(); |
| 6185 | |
| 6186 | // Move to another window. |
| 6187 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6188 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6189 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 6190 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6191 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6192 | mWindow->consumeDragEvent(true, 150, 50); |
| 6193 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 6194 | |
| 6195 | // Move back to original window. |
| 6196 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6197 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6198 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6199 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6200 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6201 | mWindow->consumeDragEvent(false, 50, 50); |
| 6202 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 6203 | |
| 6204 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6205 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6206 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6207 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6208 | mWindow->assertNoEvents(); |
| 6209 | mSecondWindow->assertNoEvents(); |
| 6210 | } |
| 6211 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 6212 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
| 6213 | performDrag(); |
| 6214 | |
| 6215 | // Move on window. |
| 6216 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6217 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6218 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6219 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6220 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6221 | mWindow->consumeDragEvent(false, 50, 50); |
| 6222 | mSecondWindow->assertNoEvents(); |
| 6223 | |
| 6224 | // Move to another window. |
| 6225 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6226 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6227 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 6228 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6229 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6230 | mWindow->consumeDragEvent(true, 150, 50); |
| 6231 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 6232 | |
| 6233 | // drop to another window. |
| 6234 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6235 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6236 | {150, 50})) |
| 6237 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6238 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6239 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 6240 | mWindow->assertNoEvents(); |
| 6241 | mSecondWindow->assertNoEvents(); |
| 6242 | } |
| 6243 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 6244 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
| 6245 | performStylusDrag(); |
| 6246 | |
| 6247 | // Move on window and keep button pressed. |
| 6248 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6249 | injectMotionEvent(mDispatcher, |
| 6250 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 6251 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 6252 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6253 | .x(50) |
| 6254 | .y(50)) |
| 6255 | .build())) |
| 6256 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6257 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6258 | mWindow->consumeDragEvent(false, 50, 50); |
| 6259 | mSecondWindow->assertNoEvents(); |
| 6260 | |
| 6261 | // Move to another window and release button, expect to drop item. |
| 6262 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6263 | injectMotionEvent(mDispatcher, |
| 6264 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 6265 | .buttonState(0) |
| 6266 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6267 | .x(150) |
| 6268 | .y(50)) |
| 6269 | .build())) |
| 6270 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6271 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6272 | mWindow->assertNoEvents(); |
| 6273 | mSecondWindow->assertNoEvents(); |
| 6274 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 6275 | |
| 6276 | // nothing to the window. |
| 6277 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6278 | injectMotionEvent(mDispatcher, |
| 6279 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 6280 | .buttonState(0) |
| 6281 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6282 | .x(150) |
| 6283 | .y(50)) |
| 6284 | .build())) |
| 6285 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6286 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6287 | mWindow->assertNoEvents(); |
| 6288 | mSecondWindow->assertNoEvents(); |
| 6289 | } |
| 6290 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6291 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 6292 | performDrag(); |
| 6293 | |
| 6294 | // Set second window invisible. |
| 6295 | mSecondWindow->setVisible(false); |
| 6296 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 6297 | |
| 6298 | // Move on window. |
| 6299 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6300 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6301 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6302 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6303 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6304 | mWindow->consumeDragEvent(false, 50, 50); |
| 6305 | mSecondWindow->assertNoEvents(); |
| 6306 | |
| 6307 | // Move to another window. |
| 6308 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6309 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6310 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 6311 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6312 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6313 | mWindow->consumeDragEvent(true, 150, 50); |
| 6314 | mSecondWindow->assertNoEvents(); |
| 6315 | |
| 6316 | // drop to another window. |
| 6317 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6318 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6319 | {150, 50})) |
| 6320 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6321 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6322 | mFakePolicy->assertDropTargetEquals(nullptr); |
| 6323 | mWindow->assertNoEvents(); |
| 6324 | mSecondWindow->assertNoEvents(); |
| 6325 | } |
| 6326 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6327 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
| 6328 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6329 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6330 | {50, 50})) |
| 6331 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6332 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6333 | |
| 6334 | const MotionEvent secondFingerDownEvent = |
| 6335 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6336 | .displayId(ADISPLAY_ID_DEFAULT) |
| 6337 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6338 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6339 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(75).y(50)) |
| 6340 | .build(); |
| 6341 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6342 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6343 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6344 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6345 | mWindow->consumeMotionPointerDown(1 /* pointerIndex */); |
| 6346 | |
| 6347 | // Should not perform drag and drop when window has multi fingers. |
| 6348 | ASSERT_FALSE(performDrag(false)); |
| 6349 | } |
| 6350 | |
| 6351 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 6352 | // First down on second window. |
| 6353 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6354 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6355 | {150, 50})) |
| 6356 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6357 | |
| 6358 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6359 | |
| 6360 | // Second down on first window. |
| 6361 | const MotionEvent secondFingerDownEvent = |
| 6362 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6363 | .displayId(ADISPLAY_ID_DEFAULT) |
| 6364 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6365 | .pointer( |
| 6366 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 6367 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6368 | .build(); |
| 6369 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6370 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6371 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6372 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6373 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6374 | |
| 6375 | // Perform drag and drop from first window. |
| 6376 | ASSERT_TRUE(performDrag(false)); |
| 6377 | |
| 6378 | // Move on window. |
| 6379 | const MotionEvent secondFingerMoveEvent = |
| 6380 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6381 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6382 | .pointer( |
| 6383 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 6384 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6385 | .build(); |
| 6386 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6387 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 6388 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 6389 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6390 | mWindow->consumeDragEvent(false, 50, 50); |
| 6391 | mSecondWindow->consumeMotionMove(); |
| 6392 | |
| 6393 | // Release the drag pointer should perform drop. |
| 6394 | const MotionEvent secondFingerUpEvent = |
| 6395 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6396 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6397 | .pointer( |
| 6398 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 6399 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6400 | .build(); |
| 6401 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6402 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 6403 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 6404 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6405 | mFakePolicy->assertDropTargetEquals(mWindow->getToken()); |
| 6406 | mWindow->assertNoEvents(); |
| 6407 | mSecondWindow->consumeMotionMove(); |
| 6408 | } |
| 6409 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame^] | 6410 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
| 6411 | performDrag(); |
| 6412 | |
| 6413 | // Update window of second display. |
| 6414 | sp<FakeWindowHandle> windowInSecondary = |
| 6415 | new FakeWindowHandle(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
| 6416 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 6417 | |
| 6418 | // Let second display has a touch state. |
| 6419 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6420 | injectMotionEvent(mDispatcher, |
| 6421 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 6422 | AINPUT_SOURCE_TOUCHSCREEN) |
| 6423 | .displayId(SECOND_DISPLAY_ID) |
| 6424 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 6425 | .x(100) |
| 6426 | .y(100)) |
| 6427 | .build())); |
| 6428 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 6429 | SECOND_DISPLAY_ID, 0 /* expectedFlag */); |
| 6430 | // Update window again. |
| 6431 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 6432 | |
| 6433 | // Move on window. |
| 6434 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6435 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6436 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6437 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6438 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6439 | mWindow->consumeDragEvent(false, 50, 50); |
| 6440 | mSecondWindow->assertNoEvents(); |
| 6441 | |
| 6442 | // Move to another window. |
| 6443 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6444 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6445 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 6446 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6447 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6448 | mWindow->consumeDragEvent(true, 150, 50); |
| 6449 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 6450 | |
| 6451 | // drop to another window. |
| 6452 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6453 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6454 | {150, 50})) |
| 6455 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6456 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6457 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 6458 | mWindow->assertNoEvents(); |
| 6459 | mSecondWindow->assertNoEvents(); |
| 6460 | } |
| 6461 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6462 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 6463 | |
| 6464 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 6465 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6466 | sp<FakeWindowHandle> window = |
| 6467 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6468 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6469 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6470 | window->setFocusable(true); |
| 6471 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 6472 | setFocusedWindow(window); |
| 6473 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 6474 | |
| 6475 | // With the flag set, window should not get any input |
| 6476 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 6477 | mDispatcher->notifyKey(&keyArgs); |
| 6478 | window->assertNoEvents(); |
| 6479 | |
| 6480 | NotifyMotionArgs motionArgs = |
| 6481 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6482 | ADISPLAY_ID_DEFAULT); |
| 6483 | mDispatcher->notifyMotion(&motionArgs); |
| 6484 | window->assertNoEvents(); |
| 6485 | |
| 6486 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6487 | window->setDropInput(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6488 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 6489 | |
| 6490 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 6491 | mDispatcher->notifyKey(&keyArgs); |
| 6492 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 6493 | |
| 6494 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6495 | ADISPLAY_ID_DEFAULT); |
| 6496 | mDispatcher->notifyMotion(&motionArgs); |
| 6497 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6498 | window->assertNoEvents(); |
| 6499 | } |
| 6500 | |
| 6501 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 6502 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 6503 | std::make_shared<FakeApplicationHandle>(); |
| 6504 | sp<FakeWindowHandle> obscuringWindow = |
| 6505 | new FakeWindowHandle(obscuringApplication, mDispatcher, "obscuringWindow", |
| 6506 | ADISPLAY_ID_DEFAULT); |
| 6507 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 6508 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6509 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6510 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6511 | sp<FakeWindowHandle> window = |
| 6512 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6513 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6514 | window->setOwnerInfo(222, 222); |
| 6515 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6516 | window->setFocusable(true); |
| 6517 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 6518 | setFocusedWindow(window); |
| 6519 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 6520 | |
| 6521 | // With the flag set, window should not get any input |
| 6522 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 6523 | mDispatcher->notifyKey(&keyArgs); |
| 6524 | window->assertNoEvents(); |
| 6525 | |
| 6526 | NotifyMotionArgs motionArgs = |
| 6527 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6528 | ADISPLAY_ID_DEFAULT); |
| 6529 | mDispatcher->notifyMotion(&motionArgs); |
| 6530 | window->assertNoEvents(); |
| 6531 | |
| 6532 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6533 | window->setDropInputIfObscured(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6534 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 6535 | |
| 6536 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 6537 | mDispatcher->notifyKey(&keyArgs); |
| 6538 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 6539 | |
| 6540 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6541 | ADISPLAY_ID_DEFAULT); |
| 6542 | mDispatcher->notifyMotion(&motionArgs); |
| 6543 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 6544 | window->assertNoEvents(); |
| 6545 | } |
| 6546 | |
| 6547 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 6548 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 6549 | std::make_shared<FakeApplicationHandle>(); |
| 6550 | sp<FakeWindowHandle> obscuringWindow = |
| 6551 | new FakeWindowHandle(obscuringApplication, mDispatcher, "obscuringWindow", |
| 6552 | ADISPLAY_ID_DEFAULT); |
| 6553 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 6554 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6555 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6556 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6557 | sp<FakeWindowHandle> window = |
| 6558 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6559 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6560 | window->setOwnerInfo(222, 222); |
| 6561 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6562 | window->setFocusable(true); |
| 6563 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 6564 | setFocusedWindow(window); |
| 6565 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 6566 | |
| 6567 | // With the flag set, window should not get any input |
| 6568 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 6569 | mDispatcher->notifyKey(&keyArgs); |
| 6570 | window->assertNoEvents(); |
| 6571 | |
| 6572 | NotifyMotionArgs motionArgs = |
| 6573 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6574 | ADISPLAY_ID_DEFAULT); |
| 6575 | mDispatcher->notifyMotion(&motionArgs); |
| 6576 | window->assertNoEvents(); |
| 6577 | |
| 6578 | // When the window is no longer obscured because it went on top, it should get input |
| 6579 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 6580 | |
| 6581 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 6582 | mDispatcher->notifyKey(&keyArgs); |
| 6583 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 6584 | |
| 6585 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6586 | ADISPLAY_ID_DEFAULT); |
| 6587 | mDispatcher->notifyMotion(&motionArgs); |
| 6588 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6589 | window->assertNoEvents(); |
| 6590 | } |
| 6591 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6592 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 6593 | protected: |
| 6594 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6595 | sp<FakeWindowHandle> mWindow; |
| 6596 | sp<FakeWindowHandle> mSecondWindow; |
| 6597 | |
| 6598 | void SetUp() override { |
| 6599 | InputDispatcherTest::SetUp(); |
| 6600 | |
| 6601 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 6602 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 6603 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 6604 | setFocusedWindow(mWindow); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6605 | mSecondWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
| 6606 | mSecondWindow->setFocusable(true); |
| 6607 | |
| 6608 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 6609 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6610 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 6611 | |
| 6612 | // Set initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 6613 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
| 6614 | WINDOW_UID, /* hasPermission */ true)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 6615 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 6616 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 6617 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6618 | } |
| 6619 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6620 | void changeAndVerifyTouchMode(bool inTouchMode, int32_t pid, int32_t uid, bool hasPermission) { |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 6621 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6622 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 6623 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
| 6624 | } |
| 6625 | }; |
| 6626 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 6627 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6628 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 6629 | changeAndVerifyTouchMode(!InputDispatcher::kDefaultInTouchMode, windowInfo.ownerPid, |
| 6630 | windowInfo.ownerUid, /* hasPermission */ false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6631 | } |
| 6632 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 6633 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 6634 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 6635 | int32_t ownerPid = windowInfo.ownerPid; |
| 6636 | int32_t ownerUid = windowInfo.ownerUid; |
| 6637 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
| 6638 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
| 6639 | ownerUid, /* hasPermission */ false)); |
| 6640 | mWindow->assertNoEvents(); |
| 6641 | mSecondWindow->assertNoEvents(); |
| 6642 | } |
| 6643 | |
| 6644 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 6645 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 6646 | int32_t ownerPid = windowInfo.ownerPid; |
| 6647 | int32_t ownerUid = windowInfo.ownerUid; |
| 6648 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
| 6649 | changeAndVerifyTouchMode(!InputDispatcher::kDefaultInTouchMode, ownerPid, ownerUid, |
| 6650 | /* hasPermission */ true); |
| 6651 | } |
| 6652 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6653 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6654 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 6655 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 6656 | windowInfo.ownerPid, windowInfo.ownerUid, |
| 6657 | /* hasPermission */ true)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6658 | mWindow->assertNoEvents(); |
| 6659 | mSecondWindow->assertNoEvents(); |
| 6660 | } |
| 6661 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 6662 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 6663 | // Interact with the window first. |
| 6664 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 6665 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 6666 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6667 | |
| 6668 | // Then remove focus. |
| 6669 | mWindow->setFocusable(false); |
| 6670 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6671 | |
| 6672 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 6673 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 6674 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 6675 | windowInfo.ownerPid, windowInfo.ownerUid, |
| 6676 | /* hasPermission= */ false)); |
| 6677 | } |
| 6678 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6679 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 6680 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6681 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6682 | std::shared_ptr<FakeApplicationHandle> application = |
| 6683 | std::make_shared<FakeApplicationHandle>(); |
| 6684 | std::string name = "Fake Spy "; |
| 6685 | name += std::to_string(mSpyCount++); |
| 6686 | sp<FakeWindowHandle> spy = |
| 6687 | new FakeWindowHandle(application, mDispatcher, name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6688 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 6689 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6690 | return spy; |
| 6691 | } |
| 6692 | |
| 6693 | sp<FakeWindowHandle> createForeground() { |
| 6694 | std::shared_ptr<FakeApplicationHandle> application = |
| 6695 | std::make_shared<FakeApplicationHandle>(); |
| 6696 | sp<FakeWindowHandle> window = |
| 6697 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6698 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6699 | return window; |
| 6700 | } |
| 6701 | |
| 6702 | private: |
| 6703 | int mSpyCount{0}; |
| 6704 | }; |
| 6705 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 6706 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6707 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 6708 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 6709 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 6710 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
| 6711 | ScopedSilentDeath _silentDeath; |
| 6712 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6713 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 6714 | spy->setTrustedOverlay(false); |
| 6715 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), |
| 6716 | ".* not a trusted overlay"); |
| 6717 | } |
| 6718 | |
| 6719 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6720 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 6721 | */ |
| 6722 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6723 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6724 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); |
| 6725 | |
| 6726 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6727 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6728 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6729 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6730 | } |
| 6731 | |
| 6732 | /** |
| 6733 | * Verify the order in which different input windows receive events. The touched foreground window |
| 6734 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 6735 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 6736 | * receive events before ones belows it. |
| 6737 | * |
| 6738 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 6739 | * spy1, spy2, window, spy3. |
| 6740 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 6741 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 6742 | * window. |
| 6743 | */ |
| 6744 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 6745 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6746 | auto spy1 = createSpy(); |
| 6747 | auto spy2 = createSpy(); |
| 6748 | auto spy3 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6749 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); |
| 6750 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 6751 | const size_t numChannels = channels.size(); |
| 6752 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 6753 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6754 | if (!epollFd.ok()) { |
| 6755 | FAIL() << "Failed to create epoll fd"; |
| 6756 | } |
| 6757 | |
| 6758 | for (size_t i = 0; i < numChannels; i++) { |
| 6759 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 6760 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 6761 | FAIL() << "Failed to add fd to epoll"; |
| 6762 | } |
| 6763 | } |
| 6764 | |
| 6765 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6766 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6767 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6768 | |
| 6769 | std::vector<size_t> eventOrder; |
| 6770 | std::vector<struct epoll_event> events(numChannels); |
| 6771 | for (;;) { |
| 6772 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 6773 | (100ms).count()); |
| 6774 | if (nFds < 0) { |
| 6775 | FAIL() << "Failed to call epoll_wait"; |
| 6776 | } |
| 6777 | if (nFds == 0) { |
| 6778 | break; // epoll_wait timed out |
| 6779 | } |
| 6780 | for (int i = 0; i < nFds; i++) { |
| 6781 | ASSERT_EQ(EPOLLIN, events[i].events); |
| 6782 | eventOrder.push_back(events[i].data.u64); |
| 6783 | channels[i]->consumeMotionDown(); |
| 6784 | } |
| 6785 | } |
| 6786 | |
| 6787 | // Verify the order in which the events were received. |
| 6788 | EXPECT_EQ(3u, eventOrder.size()); |
| 6789 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 6790 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 6791 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 6792 | } |
| 6793 | |
| 6794 | /** |
| 6795 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 6796 | */ |
| 6797 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 6798 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6799 | auto spy = createSpy(); |
| 6800 | spy->setTouchable(false); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6801 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 6802 | |
| 6803 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6804 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6805 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6806 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6807 | spy->assertNoEvents(); |
| 6808 | } |
| 6809 | |
| 6810 | /** |
| 6811 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 6812 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 6813 | * to the window. |
| 6814 | */ |
| 6815 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 6816 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6817 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6818 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
| 6819 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 6820 | |
| 6821 | // Inject an event outside the spy window's touchable region. |
| 6822 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6823 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6824 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6825 | window->consumeMotionDown(); |
| 6826 | spy->assertNoEvents(); |
| 6827 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6828 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6829 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6830 | window->consumeMotionUp(); |
| 6831 | spy->assertNoEvents(); |
| 6832 | |
| 6833 | // Inject an event inside the spy window's touchable region. |
| 6834 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6835 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6836 | {5, 10})) |
| 6837 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6838 | window->consumeMotionDown(); |
| 6839 | spy->consumeMotionDown(); |
| 6840 | } |
| 6841 | |
| 6842 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6843 | * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6844 | * flag, but it will get zero-ed out coordinates if the foreground has a different owner. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6845 | */ |
| 6846 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 6847 | auto window = createForeground(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6848 | window->setOwnerInfo(12, 34); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6849 | auto spy = createSpy(); |
| 6850 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6851 | spy->setOwnerInfo(56, 78); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6852 | spy->setFrame(Rect{0, 0, 20, 20}); |
| 6853 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 6854 | |
| 6855 | // Inject an event outside the spy window's frame and touchable region. |
| 6856 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6857 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6858 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6859 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6860 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6861 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6862 | } |
| 6863 | |
| 6864 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6865 | * A spy window can pilfer pointers. When this happens, touch gestures that are currently sent to |
| 6866 | * any other windows - including other spy windows - will also be cancelled. |
| 6867 | */ |
| 6868 | TEST_F(InputDispatcherSpyWindowTest, PilferPointers) { |
| 6869 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6870 | auto spy1 = createSpy(); |
| 6871 | auto spy2 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6872 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); |
| 6873 | |
| 6874 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6875 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6876 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6877 | window->consumeMotionDown(); |
| 6878 | spy1->consumeMotionDown(); |
| 6879 | spy2->consumeMotionDown(); |
| 6880 | |
| 6881 | // Pilfer pointers from the second spy window. |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6882 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6883 | spy2->assertNoEvents(); |
| 6884 | spy1->consumeMotionCancel(); |
| 6885 | window->consumeMotionCancel(); |
| 6886 | |
| 6887 | // The rest of the gesture should only be sent to the second spy window. |
| 6888 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6889 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6890 | ADISPLAY_ID_DEFAULT)) |
| 6891 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6892 | spy2->consumeMotionMove(); |
| 6893 | spy1->assertNoEvents(); |
| 6894 | window->assertNoEvents(); |
| 6895 | } |
| 6896 | |
| 6897 | /** |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6898 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 6899 | * in the middle of the gesture. |
| 6900 | */ |
| 6901 | TEST_F(InputDispatcherSpyWindowTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 6902 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6903 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6904 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 6905 | |
| 6906 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6907 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6908 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6909 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6910 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6911 | |
| 6912 | window->releaseChannel(); |
| 6913 | |
| 6914 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 6915 | |
| 6916 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6917 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6918 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6919 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6920 | } |
| 6921 | |
| 6922 | /** |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6923 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 6924 | * the spy, but not to any other windows. |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6925 | */ |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6926 | TEST_F(InputDispatcherSpyWindowTest, ContinuesToReceiveGestureAfterPilfer) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6927 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6928 | auto window = createForeground(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6929 | |
| 6930 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 6931 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6932 | // First finger down on the window and the spy. |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6933 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6934 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6935 | {100, 200})) |
| 6936 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6937 | spy->consumeMotionDown(); |
| 6938 | window->consumeMotionDown(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6939 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6940 | // Spy window pilfers the pointers. |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6941 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6942 | window->consumeMotionCancel(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6943 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6944 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6945 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6946 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6947 | .displayId(ADISPLAY_ID_DEFAULT) |
| 6948 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6949 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 6950 | .x(100) |
| 6951 | .y(200)) |
| 6952 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6953 | .build(); |
| 6954 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6955 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6956 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6957 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6958 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6959 | spy->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 6960 | |
| 6961 | // Third finger goes down outside all windows, so injection should fail. |
| 6962 | const MotionEvent thirdFingerDownEvent = |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 6963 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6964 | .displayId(ADISPLAY_ID_DEFAULT) |
| 6965 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6966 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 6967 | .x(100) |
| 6968 | .y(200)) |
| 6969 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6970 | .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-5).y(-5)) |
| 6971 | .build(); |
| 6972 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6973 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6974 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6975 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6976 | |
| 6977 | spy->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6978 | window->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6979 | } |
| 6980 | |
| 6981 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6982 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 6983 | * pointers that are down within its bounds. |
| 6984 | */ |
| 6985 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 6986 | auto windowLeft = createForeground(); |
| 6987 | windowLeft->setFrame({0, 0, 100, 200}); |
| 6988 | auto windowRight = createForeground(); |
| 6989 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6990 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 6991 | spy->setFrame({0, 0, 200, 200}); |
| 6992 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); |
| 6993 | |
| 6994 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6995 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6996 | {50, 50})) |
| 6997 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6998 | windowLeft->consumeMotionDown(); |
| 6999 | spy->consumeMotionDown(); |
| 7000 | |
| 7001 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7002 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7003 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7004 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7005 | .pointer( |
| 7006 | PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7007 | .build(); |
| 7008 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7009 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7010 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7011 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7012 | windowRight->consumeMotionDown(); |
| 7013 | spy->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 7014 | } |
| 7015 | |
| 7016 | /** |
| 7017 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 7018 | * the spy should receive the second pointer with ACTION_DOWN. |
| 7019 | */ |
| 7020 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 7021 | auto window = createForeground(); |
| 7022 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7023 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7024 | spyRight->setFrame({100, 0, 200, 200}); |
| 7025 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); |
| 7026 | |
| 7027 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7028 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7029 | {50, 50})) |
| 7030 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7031 | window->consumeMotionDown(); |
| 7032 | spyRight->assertNoEvents(); |
| 7033 | |
| 7034 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7035 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7036 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7037 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7038 | .pointer( |
| 7039 | PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7040 | .build(); |
| 7041 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7042 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7043 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7044 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7045 | window->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 7046 | spyRight->consumeMotionDown(); |
| 7047 | } |
| 7048 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7049 | /** |
| 7050 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 7051 | * windows should be allowed to control split touch. |
| 7052 | */ |
| 7053 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7054 | // This spy window prevents touch splitting. However, we still expect to split touches |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7055 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7056 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7057 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7058 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7059 | auto window = createForeground(); |
| 7060 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7061 | |
| 7062 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7063 | |
| 7064 | // First finger down, no window touched. |
| 7065 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7066 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7067 | {100, 200})) |
| 7068 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7069 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7070 | window->assertNoEvents(); |
| 7071 | |
| 7072 | // Second finger down on window, the window should receive touch down. |
| 7073 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7074 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7075 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7076 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7077 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7078 | .x(100) |
| 7079 | .y(200)) |
| 7080 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7081 | .build(); |
| 7082 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7083 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7084 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7085 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7086 | |
| 7087 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7088 | spy->consumeMotionPointerDown(1 /* pointerIndex */); |
| 7089 | } |
| 7090 | |
| 7091 | /** |
| 7092 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 7093 | * do not receive key events. |
| 7094 | */ |
| 7095 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7096 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7097 | spy->setFocusable(false); |
| 7098 | |
| 7099 | auto window = createForeground(); |
| 7100 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7101 | setFocusedWindow(window); |
| 7102 | window->consumeFocusEvent(true); |
| 7103 | |
| 7104 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7105 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 7106 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7107 | |
| 7108 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7109 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 7110 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7111 | |
| 7112 | spy->assertNoEvents(); |
| 7113 | } |
| 7114 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7115 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 7116 | public: |
| 7117 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 7118 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 7119 | std::make_shared<FakeApplicationHandle>(); |
| 7120 | sp<FakeWindowHandle> overlay = |
| 7121 | new FakeWindowHandle(overlayApplication, mDispatcher, "Stylus interceptor window", |
| 7122 | ADISPLAY_ID_DEFAULT); |
| 7123 | overlay->setFocusable(false); |
| 7124 | overlay->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7125 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7126 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7127 | overlay->setTrustedOverlay(true); |
| 7128 | |
| 7129 | std::shared_ptr<FakeApplicationHandle> application = |
| 7130 | std::make_shared<FakeApplicationHandle>(); |
| 7131 | sp<FakeWindowHandle> window = |
| 7132 | new FakeWindowHandle(application, mDispatcher, "Application window", |
| 7133 | ADISPLAY_ID_DEFAULT); |
| 7134 | window->setFocusable(true); |
| 7135 | window->setOwnerInfo(222, 222); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7136 | |
| 7137 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7138 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 7139 | setFocusedWindow(window); |
| 7140 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 7141 | return {std::move(overlay), std::move(window)}; |
| 7142 | } |
| 7143 | |
| 7144 | void sendFingerEvent(int32_t action) { |
| 7145 | NotifyMotionArgs motionArgs = |
| 7146 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 7147 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}}); |
| 7148 | mDispatcher->notifyMotion(&motionArgs); |
| 7149 | } |
| 7150 | |
| 7151 | void sendStylusEvent(int32_t action) { |
| 7152 | NotifyMotionArgs motionArgs = |
| 7153 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 7154 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
| 7155 | motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 7156 | mDispatcher->notifyMotion(&motionArgs); |
| 7157 | } |
| 7158 | }; |
| 7159 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7160 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 7161 | |
| 7162 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
| 7163 | ScopedSilentDeath _silentDeath; |
| 7164 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7165 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 7166 | overlay->setTrustedOverlay(false); |
| 7167 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
| 7168 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), |
| 7169 | ".* not a trusted overlay"); |
| 7170 | } |
| 7171 | |
| 7172 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 7173 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 7174 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 7175 | |
| 7176 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 7177 | overlay->consumeMotionDown(); |
| 7178 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 7179 | overlay->consumeMotionUp(); |
| 7180 | |
| 7181 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 7182 | window->consumeMotionDown(); |
| 7183 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 7184 | window->consumeMotionUp(); |
| 7185 | |
| 7186 | overlay->assertNoEvents(); |
| 7187 | window->assertNoEvents(); |
| 7188 | } |
| 7189 | |
| 7190 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 7191 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7192 | overlay->setSpy(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7193 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 7194 | |
| 7195 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 7196 | overlay->consumeMotionDown(); |
| 7197 | window->consumeMotionDown(); |
| 7198 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 7199 | overlay->consumeMotionUp(); |
| 7200 | window->consumeMotionUp(); |
| 7201 | |
| 7202 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 7203 | window->consumeMotionDown(); |
| 7204 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 7205 | window->consumeMotionUp(); |
| 7206 | |
| 7207 | overlay->assertNoEvents(); |
| 7208 | window->assertNoEvents(); |
| 7209 | } |
| 7210 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 7211 | /** |
| 7212 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 7213 | * The scenario is as follows: |
| 7214 | * - The stylus interceptor overlay is configured as a spy window. |
| 7215 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 7216 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 7217 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 7218 | */ |
| 7219 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 7220 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 7221 | overlay->setSpy(true); |
| 7222 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 7223 | |
| 7224 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 7225 | overlay->consumeMotionDown(); |
| 7226 | window->consumeMotionDown(); |
| 7227 | |
| 7228 | // The interceptor pilfers the pointers. |
| 7229 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 7230 | window->consumeMotionCancel(); |
| 7231 | |
| 7232 | // The interceptor configures itself so that it is no longer a spy. |
| 7233 | overlay->setSpy(false); |
| 7234 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 7235 | |
| 7236 | // It continues to receive the rest of the stylus gesture. |
| 7237 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 7238 | overlay->consumeMotionMove(); |
| 7239 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 7240 | overlay->consumeMotionUp(); |
| 7241 | |
| 7242 | window->assertNoEvents(); |
| 7243 | } |
| 7244 | |
Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 7245 | struct User { |
| 7246 | int32_t mPid; |
| 7247 | int32_t mUid; |
| 7248 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 7249 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 7250 | |
| 7251 | User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid) |
| 7252 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 7253 | |
| 7254 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
| 7255 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
| 7256 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 7257 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 7258 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 7259 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 7260 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 7261 | } |
| 7262 | |
| 7263 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
| 7264 | return inputdispatcher::injectKey(mDispatcher, action, 0 /* repeatCount*/, ADISPLAY_ID_NONE, |
| 7265 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 7266 | INJECT_EVENT_TIMEOUT, false /*allowKeyRepeat*/, {mUid}, |
| 7267 | mPolicyFlags); |
| 7268 | } |
| 7269 | |
| 7270 | sp<FakeWindowHandle> createWindow() const { |
| 7271 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 7272 | std::make_shared<FakeApplicationHandle>(); |
| 7273 | sp<FakeWindowHandle> window = new FakeWindowHandle(overlayApplication, mDispatcher, |
| 7274 | "Owned Window", ADISPLAY_ID_DEFAULT); |
| 7275 | window->setOwnerInfo(mPid, mUid); |
| 7276 | return window; |
| 7277 | } |
| 7278 | }; |
| 7279 | |
| 7280 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 7281 | |
| 7282 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
| 7283 | auto owner = User(mDispatcher, 10, 11); |
| 7284 | auto window = owner.createWindow(); |
| 7285 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 7286 | |
| 7287 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7288 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 7289 | window->consumeMotionDown(); |
| 7290 | |
| 7291 | setFocusedWindow(window); |
| 7292 | window->consumeFocusEvent(true); |
| 7293 | |
| 7294 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7295 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 7296 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7297 | } |
| 7298 | |
| 7299 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
| 7300 | auto owner = User(mDispatcher, 10, 11); |
| 7301 | auto window = owner.createWindow(); |
| 7302 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 7303 | |
| 7304 | auto rando = User(mDispatcher, 20, 21); |
| 7305 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 7306 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 7307 | |
| 7308 | setFocusedWindow(window); |
| 7309 | window->consumeFocusEvent(true); |
| 7310 | |
| 7311 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 7312 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 7313 | window->assertNoEvents(); |
| 7314 | } |
| 7315 | |
| 7316 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
| 7317 | auto owner = User(mDispatcher, 10, 11); |
| 7318 | auto window = owner.createWindow(); |
| 7319 | auto spy = owner.createWindow(); |
| 7320 | spy->setSpy(true); |
| 7321 | spy->setTrustedOverlay(true); |
| 7322 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7323 | |
| 7324 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7325 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 7326 | spy->consumeMotionDown(); |
| 7327 | window->consumeMotionDown(); |
| 7328 | } |
| 7329 | |
| 7330 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
| 7331 | auto owner = User(mDispatcher, 10, 11); |
| 7332 | auto window = owner.createWindow(); |
| 7333 | |
| 7334 | auto rando = User(mDispatcher, 20, 21); |
| 7335 | auto randosSpy = rando.createWindow(); |
| 7336 | randosSpy->setSpy(true); |
| 7337 | randosSpy->setTrustedOverlay(true); |
| 7338 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 7339 | |
| 7340 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 7341 | // not receive the event. |
| 7342 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7343 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 7344 | randosSpy->assertNoEvents(); |
| 7345 | window->consumeMotionDown(); |
| 7346 | } |
| 7347 | |
| 7348 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
| 7349 | auto owner = User(mDispatcher, 10, 11); |
| 7350 | auto window = owner.createWindow(); |
| 7351 | |
| 7352 | auto rando = User(mDispatcher, 20, 21); |
| 7353 | auto randosSpy = rando.createWindow(); |
| 7354 | randosSpy->setSpy(true); |
| 7355 | randosSpy->setTrustedOverlay(true); |
| 7356 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 7357 | |
| 7358 | // A user that has injection permission can inject into any window. |
| 7359 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7360 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7361 | ADISPLAY_ID_DEFAULT)); |
| 7362 | randosSpy->consumeMotionDown(); |
| 7363 | window->consumeMotionDown(); |
| 7364 | |
| 7365 | setFocusedWindow(randosSpy); |
| 7366 | randosSpy->consumeFocusEvent(true); |
| 7367 | |
| 7368 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
| 7369 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7370 | window->assertNoEvents(); |
| 7371 | } |
| 7372 | |
| 7373 | TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) { |
| 7374 | auto owner = User(mDispatcher, 10, 11); |
| 7375 | auto window = owner.createWindow(); |
| 7376 | |
| 7377 | auto rando = User(mDispatcher, 20, 21); |
| 7378 | auto randosWindow = rando.createWindow(); |
| 7379 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 7380 | randosWindow->setWatchOutsideTouch(true); |
| 7381 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); |
| 7382 | |
| 7383 | // We allow generation of ACTION_OUTSIDE events into windows owned by different uids. |
| 7384 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7385 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 7386 | window->consumeMotionDown(); |
| 7387 | randosWindow->consumeMotionOutside(); |
| 7388 | } |
| 7389 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 7390 | } // namespace android::inputdispatcher |