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> |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 25 | #include <gmock/gmock.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 27 | #include <input/Input.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 28 | #include <linux/input.h> |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 29 | #include <sys/epoll.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 30 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 31 | #include <cinttypes> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 32 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 33 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 34 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 35 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 36 | using android::base::StringPrintf; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 37 | using android::gui::FocusRequest; |
| 38 | using android::gui::TouchOcclusionMode; |
| 39 | using android::gui::WindowInfo; |
| 40 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 41 | using android::os::InputEventInjectionResult; |
| 42 | using android::os::InputEventInjectionSync; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 43 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 44 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 45 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 46 | using namespace ftl::flag_operators; |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 47 | using testing::AllOf; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 48 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 49 | // An arbitrary time value. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 50 | static constexpr nsecs_t ARBITRARY_TIME = 1234; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 51 | |
| 52 | // An arbitrary device id. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 53 | static constexpr int32_t DEVICE_ID = 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 54 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 55 | // An arbitrary display id. |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 56 | static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
| 57 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 58 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 59 | static constexpr int32_t POINTER_1_DOWN = |
| 60 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 61 | static constexpr int32_t POINTER_2_DOWN = |
| 62 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 63 | static constexpr int32_t POINTER_3_DOWN = |
| 64 | AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 65 | static constexpr int32_t POINTER_1_UP = |
| 66 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 67 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 68 | // The default pid and uid for windows created on the primary display by the test. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 69 | static constexpr int32_t WINDOW_PID = 999; |
| 70 | static constexpr int32_t WINDOW_UID = 1001; |
| 71 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 72 | // The default pid and uid for the windows created on the secondary display by the test. |
| 73 | static constexpr int32_t SECONDARY_WINDOW_PID = 1010; |
| 74 | static constexpr int32_t SECONDARY_WINDOW_UID = 1012; |
| 75 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 76 | // The default policy flags to use for event injection by tests. |
| 77 | 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] | 78 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 79 | // An arbitrary pid of the gesture monitor window |
| 80 | static constexpr int32_t MONITOR_PID = 2001; |
| 81 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 82 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 83 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 84 | struct PointF { |
| 85 | float x; |
| 86 | float y; |
| 87 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 88 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 89 | /** |
| 90 | * Return a DOWN key event with KEYCODE_A. |
| 91 | */ |
| 92 | static KeyEvent getTestKeyEvent() { |
| 93 | KeyEvent event; |
| 94 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 95 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 96 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 97 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 98 | return event; |
| 99 | } |
| 100 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 101 | static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) { |
| 102 | ASSERT_EQ(expectedAction, receivedAction) |
| 103 | << "expected " << MotionEvent::actionToString(expectedAction) << ", got " |
| 104 | << MotionEvent::actionToString(receivedAction); |
| 105 | } |
| 106 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 107 | MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") { |
| 108 | bool matches = action == arg.getAction(); |
| 109 | if (!matches) { |
| 110 | *result_listener << "expected action " << MotionEvent::actionToString(action) |
| 111 | << ", but got " << MotionEvent::actionToString(arg.getAction()); |
| 112 | } |
| 113 | if (action == AMOTION_EVENT_ACTION_CANCEL) { |
| 114 | if (!matches) { |
| 115 | *result_listener << "; "; |
| 116 | } |
| 117 | *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set"; |
| 118 | matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0; |
| 119 | } |
| 120 | return matches; |
| 121 | } |
| 122 | |
| 123 | MATCHER_P(WithSource, source, "InputEvent with specified source") { |
| 124 | *result_listener << "expected source " << inputEventSourceToString(source) << ", but got " |
| 125 | << inputEventSourceToString(arg.getSource()); |
| 126 | return arg.getSource() == source; |
| 127 | } |
| 128 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 129 | // --- FakeInputDispatcherPolicy --- |
| 130 | |
| 131 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 132 | InputDispatcherConfiguration mConfig; |
| 133 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 134 | using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>; |
| 135 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 136 | protected: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 137 | virtual ~FakeInputDispatcherPolicy() {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 138 | |
| 139 | public: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 140 | FakeInputDispatcherPolicy() {} |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 141 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 142 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 143 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
| 144 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_KEY); |
| 145 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 146 | |
| 147 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 148 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 149 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 150 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 151 | } |
| 152 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 153 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 154 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
| 155 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_MOTION); |
| 156 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 157 | |
| 158 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 159 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 160 | EXPECT_EQ(motionEvent.getAction(), args.action); |
| 161 | EXPECT_EQ(motionEvent.getX(0), point.x); |
| 162 | EXPECT_EQ(motionEvent.getY(0), point.y); |
| 163 | EXPECT_EQ(motionEvent.getRawX(0), point.x); |
| 164 | EXPECT_EQ(motionEvent.getRawY(0), point.y); |
| 165 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 166 | } |
| 167 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 168 | void assertFilterInputEventWasNotCalled() { |
| 169 | std::scoped_lock lock(mLock); |
| 170 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 171 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 172 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 173 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 174 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 175 | ASSERT_TRUE(mConfigurationChangedTime) |
| 176 | << "Timed out waiting for configuration changed call"; |
| 177 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 178 | mConfigurationChangedTime = std::nullopt; |
| 179 | } |
| 180 | |
| 181 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 182 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 183 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 184 | // 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] | 185 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 186 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 187 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 188 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 189 | mLastNotifySwitch = std::nullopt; |
| 190 | } |
| 191 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 192 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 193 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 194 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 195 | mOnPointerDownToken.clear(); |
| 196 | } |
| 197 | |
| 198 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 199 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 200 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 201 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 204 | // This function must be called soon after the expected ANR timer starts, |
| 205 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 206 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 207 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 208 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 209 | std::unique_lock lock(mLock); |
| 210 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 211 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 212 | ASSERT_NO_FATAL_FAILURE( |
| 213 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 214 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 217 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 218 | const sp<WindowInfoHandle>& window) { |
| 219 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 220 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 221 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 222 | } |
| 223 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 224 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 225 | const sp<IBinder>& expectedToken, |
| 226 | int32_t expectedPid) { |
| 227 | std::unique_lock lock(mLock); |
| 228 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 229 | AnrResult result; |
| 230 | ASSERT_NO_FATAL_FAILURE(result = |
| 231 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
| 232 | const auto& [token, pid] = result; |
| 233 | ASSERT_EQ(expectedToken, token); |
| 234 | ASSERT_EQ(expectedPid, pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 235 | } |
| 236 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 237 | /** 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] | 238 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 239 | std::unique_lock lock(mLock); |
| 240 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 241 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 242 | const auto& [token, _] = result; |
| 243 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 246 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
| 247 | int32_t expectedPid) { |
| 248 | std::unique_lock lock(mLock); |
| 249 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 250 | AnrResult result; |
| 251 | ASSERT_NO_FATAL_FAILURE( |
| 252 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
| 253 | const auto& [token, pid] = result; |
| 254 | ASSERT_EQ(expectedToken, token); |
| 255 | ASSERT_EQ(expectedPid, pid); |
| 256 | } |
| 257 | |
| 258 | /** 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] | 259 | sp<IBinder> getResponsiveWindowToken() { |
| 260 | std::unique_lock lock(mLock); |
| 261 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 262 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 263 | const auto& [token, _] = result; |
| 264 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void assertNotifyAnrWasNotCalled() { |
| 268 | std::scoped_lock lock(mLock); |
| 269 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 270 | ASSERT_TRUE(mAnrWindows.empty()); |
| 271 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 272 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 273 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 276 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 277 | mConfig.keyRepeatTimeout = timeout; |
| 278 | mConfig.keyRepeatDelay = delay; |
| 279 | } |
| 280 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 281 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 282 | std::unique_lock lock(mLock); |
| 283 | base::ScopedLockAssertion assumeLocked(mLock); |
| 284 | |
| 285 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 286 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 287 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 288 | enabled; |
| 289 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 290 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 291 | << ") to be called."; |
| 292 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 293 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 294 | auto request = *mPointerCaptureRequest; |
| 295 | mPointerCaptureRequest.reset(); |
| 296 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void assertSetPointerCaptureNotCalled() { |
| 300 | std::unique_lock lock(mLock); |
| 301 | base::ScopedLockAssertion assumeLocked(mLock); |
| 302 | |
| 303 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 304 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 305 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 306 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 307 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 308 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 309 | } |
| 310 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 311 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { |
| 312 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 313 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 314 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 315 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 316 | } |
| 317 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 318 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 319 | std::unique_lock lock(mLock); |
| 320 | base::ScopedLockAssertion assumeLocked(mLock); |
| 321 | std::optional<sp<IBinder>> receivedToken = |
| 322 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 323 | mNotifyInputChannelBroken); |
| 324 | ASSERT_TRUE(receivedToken.has_value()); |
| 325 | ASSERT_EQ(token, *receivedToken); |
| 326 | } |
| 327 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 328 | /** |
| 329 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 330 | */ |
| 331 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 332 | mInterceptKeyTimeout = timeout; |
| 333 | } |
| 334 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 335 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 336 | std::mutex mLock; |
| 337 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 338 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 339 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 340 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 341 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 342 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 343 | |
| 344 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 345 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 346 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 347 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 348 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 349 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 350 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 351 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 352 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 353 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 354 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 355 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 356 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 357 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 358 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 359 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 360 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 361 | // first entry from the container and erase it. |
| 362 | template <class T> |
| 363 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 364 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 365 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 366 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 367 | // before checking if ANR was called. |
| 368 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 369 | // to provide it some time to act. 100ms seems reasonable. |
| 370 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 371 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 372 | std::optional<T> token = |
| 373 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 374 | if (!token.has_value()) { |
| 375 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 376 | return {}; |
| 377 | } |
| 378 | |
| 379 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 380 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 381 | // the dispatcher started counting before this function was called |
| 382 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 383 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 384 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 385 | << "ms, but waited " |
| 386 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 387 | << "ms instead"; |
| 388 | } |
| 389 | return *token; |
| 390 | } |
| 391 | |
| 392 | template <class T> |
| 393 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 394 | std::queue<T>& storage, |
| 395 | std::unique_lock<std::mutex>& lock, |
| 396 | std::condition_variable& condition) |
| 397 | REQUIRES(mLock) { |
| 398 | condition.wait_for(lock, timeout, |
| 399 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 400 | if (storage.empty()) { |
| 401 | ADD_FAILURE() << "Did not receive the expected callback"; |
| 402 | return std::nullopt; |
| 403 | } |
| 404 | T item = storage.front(); |
| 405 | storage.pop(); |
| 406 | return std::make_optional(item); |
| 407 | } |
| 408 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 409 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 410 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 411 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 414 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid, |
| 415 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 416 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 417 | ASSERT_TRUE(pid.has_value()); |
| 418 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 419 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 420 | } |
| 421 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 422 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
| 423 | std::optional<int32_t> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 424 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 425 | ASSERT_TRUE(pid.has_value()); |
| 426 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 427 | mNotifyAnr.notify_all(); |
| 428 | } |
| 429 | |
| 430 | void notifyNoFocusedWindowAnr( |
| 431 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 432 | std::scoped_lock lock(mLock); |
| 433 | mAnrApplications.push(applicationHandle); |
| 434 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 437 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 438 | std::scoped_lock lock(mLock); |
| 439 | mBrokenInputChannels.push(connectionToken); |
| 440 | mNotifyInputChannelBroken.notify_all(); |
| 441 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 442 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 443 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 444 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 445 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 446 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 447 | const std::vector<float>& values) override {} |
| 448 | |
| 449 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 450 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 451 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 452 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 453 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 454 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 455 | *outConfig = mConfig; |
| 456 | } |
| 457 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 458 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 459 | std::scoped_lock lock(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 460 | switch (inputEvent->getType()) { |
| 461 | case AINPUT_EVENT_TYPE_KEY: { |
| 462 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 463 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 464 | break; |
| 465 | } |
| 466 | |
| 467 | case AINPUT_EVENT_TYPE_MOTION: { |
| 468 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 469 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 470 | break; |
| 471 | } |
| 472 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 473 | return true; |
| 474 | } |
| 475 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 476 | void interceptKeyBeforeQueueing(const KeyEvent* inputEvent, uint32_t&) override { |
| 477 | if (inputEvent->getAction() == AKEY_EVENT_ACTION_UP) { |
| 478 | // Clear intercept state when we handled the event. |
| 479 | mInterceptKeyTimeout = 0ms; |
| 480 | } |
| 481 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 482 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 483 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 484 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 485 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 486 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 487 | // Clear intercept state so we could dispatch the event in next wake. |
| 488 | mInterceptKeyTimeout = 0ms; |
| 489 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 492 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 493 | return false; |
| 494 | } |
| 495 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 496 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 497 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 498 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 499 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 500 | * essentially a passthrough for notifySwitch. |
| 501 | */ |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 502 | mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 503 | } |
| 504 | |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 505 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 506 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 507 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 508 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 509 | mOnPointerDownToken = newToken; |
| 510 | } |
| 511 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 512 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 513 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 514 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 515 | mPointerCaptureChangedCondition.notify_all(); |
| 516 | } |
| 517 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 518 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 519 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 520 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 521 | mDropTargetWindowToken = token; |
| 522 | } |
| 523 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 524 | void assertFilterInputEventWasCalledInternal( |
| 525 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 526 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 527 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 528 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 529 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 530 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 531 | }; |
| 532 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 533 | // --- InputDispatcherTest --- |
| 534 | |
| 535 | class InputDispatcherTest : public testing::Test { |
| 536 | protected: |
| 537 | sp<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 538 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 539 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 540 | void SetUp() override { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 541 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 542 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy, STALE_EVENT_TIMEOUT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 543 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 544 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 545 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 548 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 549 | ASSERT_EQ(OK, mDispatcher->stop()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 550 | mFakePolicy.clear(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 551 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 552 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 553 | |
| 554 | /** |
| 555 | * Used for debugging when writing the test |
| 556 | */ |
| 557 | void dumpDispatcherState() { |
| 558 | std::string dump; |
| 559 | mDispatcher->dump(dump); |
| 560 | std::stringstream ss(dump); |
| 561 | std::string to; |
| 562 | |
| 563 | while (std::getline(ss, to, '\n')) { |
| 564 | ALOGE("%s", to.c_str()); |
| 565 | } |
| 566 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 567 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 568 | void setFocusedWindow(const sp<WindowInfoHandle>& window, |
| 569 | const sp<WindowInfoHandle>& focusedWindow = nullptr) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 570 | FocusRequest request; |
| 571 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 572 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 573 | if (focusedWindow) { |
| 574 | request.focusedToken = focusedWindow->getToken(); |
| 575 | } |
| 576 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 577 | request.displayId = window->getInfo()->displayId; |
| 578 | mDispatcher->setFocusedWindow(request); |
| 579 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 580 | }; |
| 581 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 582 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 583 | KeyEvent event; |
| 584 | |
| 585 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 586 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 587 | INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 588 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 589 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 590 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 591 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 592 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 593 | << "Should reject key events with undefined action."; |
| 594 | |
| 595 | // 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] | 596 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 597 | 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] | 598 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 599 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 600 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 601 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 602 | << "Should reject key events with ACTION_MULTIPLE."; |
| 603 | } |
| 604 | |
| 605 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 606 | MotionEvent event; |
| 607 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 608 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 609 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 610 | pointerProperties[i].clear(); |
| 611 | pointerProperties[i].id = i; |
| 612 | pointerCoords[i].clear(); |
| 613 | } |
| 614 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 615 | // Some constants commonly used below |
| 616 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 617 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 618 | constexpr int32_t metaState = AMETA_NONE; |
| 619 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 620 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 621 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 622 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 623 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 624 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, |
| 625 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 626 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 627 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 628 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 629 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 630 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 631 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 632 | << "Should reject motion events with undefined action."; |
| 633 | |
| 634 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 635 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 636 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 637 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 638 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 639 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 640 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 641 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 642 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 643 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 644 | << "Should reject motion events with pointer down index too large."; |
| 645 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 646 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 647 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 648 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 649 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 650 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 651 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 652 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 653 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 654 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 655 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 656 | << "Should reject motion events with pointer down index too small."; |
| 657 | |
| 658 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 659 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 660 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 661 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 662 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 663 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 664 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 665 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 666 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 667 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 668 | << "Should reject motion events with pointer up index too large."; |
| 669 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 670 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 671 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 672 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 673 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 674 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 675 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 676 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 677 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 678 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 679 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 680 | << "Should reject motion events with pointer up index too small."; |
| 681 | |
| 682 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 683 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 684 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 685 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 686 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 687 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 688 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 689 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 690 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 691 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 692 | << "Should reject motion events with 0 pointers."; |
| 693 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 694 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 695 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 696 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 697 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 698 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 699 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 700 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 701 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 702 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 703 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 704 | |
| 705 | // Rejects motion events with invalid pointer ids. |
| 706 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 707 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 708 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 709 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 710 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 711 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 712 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 713 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 714 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 715 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 716 | << "Should reject motion events with pointer ids less than 0."; |
| 717 | |
| 718 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 719 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 720 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 721 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 722 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 723 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 724 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 725 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 726 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 727 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 728 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 729 | |
| 730 | // Rejects motion events with duplicate pointer ids. |
| 731 | pointerProperties[0].id = 1; |
| 732 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 733 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 734 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 735 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 736 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 737 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 738 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 739 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 740 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 741 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 742 | << "Should reject motion events with duplicate pointer ids."; |
| 743 | } |
| 744 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 745 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 746 | |
| 747 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 748 | constexpr nsecs_t eventTime = 20; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 749 | NotifyConfigurationChangedArgs args(10 /*id*/, eventTime); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 750 | mDispatcher->notifyConfigurationChanged(&args); |
| 751 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 752 | |
| 753 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 754 | } |
| 755 | |
| 756 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 757 | NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/, |
| 758 | 2 /*switchMask*/); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 759 | mDispatcher->notifySwitch(&args); |
| 760 | |
| 761 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 762 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 763 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 764 | } |
| 765 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 766 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 767 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 768 | // Default input dispatching timeout if there is no focused application or paused window |
| 769 | // from which to determine an appropriate dispatching timeout. |
| 770 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 771 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 772 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 773 | |
| 774 | class FakeApplicationHandle : public InputApplicationHandle { |
| 775 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 776 | FakeApplicationHandle() { |
| 777 | mInfo.name = "Fake Application"; |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 778 | mInfo.token = sp<BBinder>::make(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 779 | mInfo.dispatchingTimeoutMillis = |
| 780 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 781 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 782 | virtual ~FakeApplicationHandle() {} |
| 783 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 784 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 785 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 786 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 787 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 788 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 789 | }; |
| 790 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 791 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 792 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 793 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 794 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 795 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 796 | } |
| 797 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 798 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 799 | InputEvent* event; |
| 800 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 801 | if (!consumeSeq) { |
| 802 | return nullptr; |
| 803 | } |
| 804 | finishEvent(*consumeSeq); |
| 805 | return event; |
| 806 | } |
| 807 | |
| 808 | /** |
| 809 | * Receive an event without acknowledging it. |
| 810 | * Return the sequence number that could later be used to send finished signal. |
| 811 | */ |
| 812 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 813 | uint32_t consumeSeq; |
| 814 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 815 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 816 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 817 | status_t status = WOULD_BLOCK; |
| 818 | while (status == WOULD_BLOCK) { |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 819 | status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 820 | &event); |
| 821 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 822 | if (elapsed > 100ms) { |
| 823 | break; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | if (status == WOULD_BLOCK) { |
| 828 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 829 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | if (status != OK) { |
| 833 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 834 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 835 | } |
| 836 | if (event == nullptr) { |
| 837 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 838 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 839 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 840 | if (outEvent != nullptr) { |
| 841 | *outEvent = event; |
| 842 | } |
| 843 | return consumeSeq; |
| 844 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 845 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 846 | /** |
| 847 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 848 | */ |
| 849 | void finishEvent(uint32_t consumeSeq) { |
| 850 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 851 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 852 | } |
| 853 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 854 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 855 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); |
| 856 | ASSERT_EQ(OK, status); |
| 857 | } |
| 858 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 859 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 860 | std::optional<int32_t> expectedDisplayId, |
| 861 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 862 | InputEvent* event = consume(); |
| 863 | |
| 864 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 865 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 866 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 867 | << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType) |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 868 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 869 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 870 | if (expectedDisplayId.has_value()) { |
| 871 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 872 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 873 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 874 | switch (expectedEventType) { |
| 875 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 876 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 877 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 878 | if (expectedFlags.has_value()) { |
| 879 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 880 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 881 | break; |
| 882 | } |
| 883 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 884 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 885 | assertMotionAction(expectedAction, motionEvent.getAction()); |
| 886 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 887 | if (expectedFlags.has_value()) { |
| 888 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 889 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 890 | break; |
| 891 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 892 | case AINPUT_EVENT_TYPE_FOCUS: { |
| 893 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 894 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 895 | case AINPUT_EVENT_TYPE_CAPTURE: { |
| 896 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 897 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 898 | case AINPUT_EVENT_TYPE_TOUCH_MODE: { |
| 899 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 900 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 901 | case AINPUT_EVENT_TYPE_DRAG: { |
| 902 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 903 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 904 | default: { |
| 905 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; |
| 906 | } |
| 907 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 908 | } |
| 909 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 910 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 911 | InputEvent* event = consume(); |
| 912 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 913 | << ": consumer should have returned non-NULL event."; |
| 914 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType()) |
| 915 | << "Got " << inputEventTypeToString(event->getType()) |
| 916 | << " event instead of FOCUS event"; |
| 917 | |
| 918 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 919 | << mName.c_str() << ": event displayId should always be NONE."; |
| 920 | |
| 921 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 922 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 923 | } |
| 924 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 925 | void consumeCaptureEvent(bool hasCapture) { |
| 926 | const InputEvent* event = consume(); |
| 927 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 928 | << ": consumer should have returned non-NULL event."; |
| 929 | ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType()) |
| 930 | << "Got " << inputEventTypeToString(event->getType()) |
| 931 | << " event instead of CAPTURE event"; |
| 932 | |
| 933 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 934 | << mName.c_str() << ": event displayId should always be NONE."; |
| 935 | |
| 936 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 937 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 938 | } |
| 939 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 940 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 941 | const InputEvent* event = consume(); |
| 942 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 943 | << ": consumer should have returned non-NULL event."; |
| 944 | ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType()) |
| 945 | << "Got " << inputEventTypeToString(event->getType()) |
| 946 | << " event instead of DRAG event"; |
| 947 | |
| 948 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 949 | << mName.c_str() << ": event displayId should always be NONE."; |
| 950 | |
| 951 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 952 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 953 | EXPECT_EQ(x, dragEvent.getX()); |
| 954 | EXPECT_EQ(y, dragEvent.getY()); |
| 955 | } |
| 956 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 957 | void consumeTouchModeEvent(bool inTouchMode) { |
| 958 | const InputEvent* event = consume(); |
| 959 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 960 | << ": consumer should have returned non-NULL event."; |
| 961 | ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType()) |
| 962 | << "Got " << inputEventTypeToString(event->getType()) |
| 963 | << " event instead of TOUCH_MODE event"; |
| 964 | |
| 965 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 966 | << mName.c_str() << ": event displayId should always be NONE."; |
| 967 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 968 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 969 | } |
| 970 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 971 | void assertNoEvents() { |
| 972 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 973 | if (event == nullptr) { |
| 974 | return; |
| 975 | } |
| 976 | if (event->getType() == AINPUT_EVENT_TYPE_KEY) { |
| 977 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 978 | ADD_FAILURE() << "Received key event " |
| 979 | << KeyEvent::actionToString(keyEvent.getAction()); |
| 980 | } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) { |
| 981 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 982 | ADD_FAILURE() << "Received motion event " |
| 983 | << MotionEvent::actionToString(motionEvent.getAction()); |
| 984 | } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) { |
| 985 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 986 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 987 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 988 | } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) { |
| 989 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 990 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 991 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 992 | } else if (event->getType() == AINPUT_EVENT_TYPE_TOUCH_MODE) { |
| 993 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 994 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 995 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 996 | } |
| 997 | FAIL() << mName.c_str() |
| 998 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 1002 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1003 | int getChannelFd() { return mConsumer->getChannel()->getFd().get(); } |
| 1004 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1005 | protected: |
| 1006 | std::unique_ptr<InputConsumer> mConsumer; |
| 1007 | PreallocatedInputEventFactory mEventFactory; |
| 1008 | |
| 1009 | std::string mName; |
| 1010 | }; |
| 1011 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1012 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1013 | public: |
| 1014 | static const int32_t WIDTH = 600; |
| 1015 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1016 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1017 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1018 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1019 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1020 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1021 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1022 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1023 | dispatcher->createInputChannel(name); |
| 1024 | token = (*channel)->getConnectionToken(); |
| 1025 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | inputApplicationHandle->updateInfo(); |
| 1029 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1030 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1031 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1032 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1033 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1034 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1035 | mInfo.alpha = 1.0; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1036 | mInfo.frameLeft = 0; |
| 1037 | mInfo.frameTop = 0; |
| 1038 | mInfo.frameRight = WIDTH; |
| 1039 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1040 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1041 | mInfo.globalScaleFactor = 1.0; |
| 1042 | mInfo.touchableRegion.clear(); |
| 1043 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1044 | mInfo.ownerPid = WINDOW_PID; |
| 1045 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1046 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1047 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1048 | } |
| 1049 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1050 | sp<FakeWindowHandle> clone( |
| 1051 | const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1052 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1053 | sp<FakeWindowHandle> handle = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1054 | sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher, |
| 1055 | mInfo.name + "(Mirror)", displayId, mInfo.token); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1056 | return handle; |
| 1057 | } |
| 1058 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1059 | void setTouchable(bool touchable) { |
| 1060 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1061 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1062 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1063 | void setFocusable(bool focusable) { |
| 1064 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1065 | } |
| 1066 | |
| 1067 | void setVisible(bool visible) { |
| 1068 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1069 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1070 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1071 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1072 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1075 | void setPaused(bool paused) { |
| 1076 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1077 | } |
| 1078 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1079 | void setPreventSplitting(bool preventSplitting) { |
| 1080 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | void setSlippery(bool slippery) { |
| 1084 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1085 | } |
| 1086 | |
| 1087 | void setWatchOutsideTouch(bool watchOutside) { |
| 1088 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1089 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1090 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1091 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1092 | |
| 1093 | void setInterceptsStylus(bool interceptsStylus) { |
| 1094 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1095 | } |
| 1096 | |
| 1097 | void setDropInput(bool dropInput) { |
| 1098 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1099 | } |
| 1100 | |
| 1101 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1102 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1103 | } |
| 1104 | |
| 1105 | void setNoInputChannel(bool noInputChannel) { |
| 1106 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1107 | } |
| 1108 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1109 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1110 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1111 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1112 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1113 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1114 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1115 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1116 | mInfo.frameLeft = frame.left; |
| 1117 | mInfo.frameTop = frame.top; |
| 1118 | mInfo.frameRight = frame.right; |
| 1119 | mInfo.frameBottom = frame.bottom; |
| 1120 | mInfo.touchableRegion.clear(); |
| 1121 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1122 | |
| 1123 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1124 | ui::Transform translate; |
| 1125 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1126 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1127 | } |
| 1128 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1129 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1130 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1131 | void setIsWallpaper(bool isWallpaper) { |
| 1132 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1133 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1134 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1135 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1136 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1137 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1138 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1139 | void setTrustedOverlay(bool trustedOverlay) { |
| 1140 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1141 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1142 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1143 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1144 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1145 | } |
| 1146 | |
| 1147 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1148 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1149 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1150 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1151 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1152 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1153 | expectedFlags); |
| 1154 | } |
| 1155 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1156 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1157 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
| 1158 | } |
| 1159 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1160 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1161 | int32_t expectedFlags = 0) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1162 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId, |
| 1163 | expectedFlags); |
| 1164 | } |
| 1165 | |
| 1166 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1167 | int32_t expectedFlags = 0) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1168 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId, |
| 1169 | expectedFlags); |
| 1170 | } |
| 1171 | |
| 1172 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1173 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1174 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1175 | } |
| 1176 | |
| 1177 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1178 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1179 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1180 | expectedFlags); |
| 1181 | } |
| 1182 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1183 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1184 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1185 | int32_t expectedFlags = 0) { |
| 1186 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1187 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1188 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1189 | } |
| 1190 | |
| 1191 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1192 | int32_t expectedFlags = 0) { |
| 1193 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1194 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1195 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1196 | } |
| 1197 | |
| 1198 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1199 | int32_t expectedFlags = 0) { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1200 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
| 1201 | expectedFlags); |
| 1202 | } |
| 1203 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1204 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1205 | int32_t expectedFlags = 0) { |
| 1206 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
| 1207 | expectedFlags); |
| 1208 | } |
| 1209 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1210 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1211 | int32_t expectedFlags = 0) { |
| 1212 | InputEvent* event = consume(); |
| 1213 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()); |
| 1214 | const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1215 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked()); |
| 1216 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX()); |
| 1217 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY()); |
| 1218 | } |
| 1219 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1220 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1221 | ASSERT_NE(mInputReceiver, nullptr) |
| 1222 | << "Cannot consume events from a window with no receiver"; |
| 1223 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1224 | } |
| 1225 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1226 | void consumeCaptureEvent(bool hasCapture) { |
| 1227 | ASSERT_NE(mInputReceiver, nullptr) |
| 1228 | << "Cannot consume events from a window with no receiver"; |
| 1229 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1230 | } |
| 1231 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1232 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1233 | MotionEvent* motionEvent = consumeMotion(); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame^] | 1234 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1235 | ASSERT_THAT(*motionEvent, matcher); |
| 1236 | } |
| 1237 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1238 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 1239 | std::optional<int32_t> expectedDisplayId, |
| 1240 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1241 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1242 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1243 | expectedFlags); |
| 1244 | } |
| 1245 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1246 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1247 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1248 | } |
| 1249 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1250 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1251 | ASSERT_NE(mInputReceiver, nullptr) |
| 1252 | << "Cannot consume events from a window with no receiver"; |
| 1253 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1254 | } |
| 1255 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1256 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1257 | if (mInputReceiver == nullptr) { |
| 1258 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1259 | return std::nullopt; |
| 1260 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1261 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | void finishEvent(uint32_t sequenceNum) { |
| 1265 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1266 | mInputReceiver->finishEvent(sequenceNum); |
| 1267 | } |
| 1268 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1269 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1270 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1271 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1272 | } |
| 1273 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1274 | InputEvent* consume() { |
| 1275 | if (mInputReceiver == nullptr) { |
| 1276 | return nullptr; |
| 1277 | } |
| 1278 | return mInputReceiver->consume(); |
| 1279 | } |
| 1280 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1281 | MotionEvent* consumeMotion() { |
| 1282 | InputEvent* event = consume(); |
| 1283 | if (event == nullptr) { |
| 1284 | ADD_FAILURE() << "Consume failed : no event"; |
| 1285 | return nullptr; |
| 1286 | } |
| 1287 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 1288 | ADD_FAILURE() << "Instead of motion event, got " |
| 1289 | << inputEventTypeToString(event->getType()); |
| 1290 | return nullptr; |
| 1291 | } |
| 1292 | return static_cast<MotionEvent*>(event); |
| 1293 | } |
| 1294 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1295 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1296 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1297 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1298 | return; // Can't receive events if the window does not have input channel |
| 1299 | } |
| 1300 | ASSERT_NE(nullptr, mInputReceiver) |
| 1301 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1302 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1303 | } |
| 1304 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1305 | sp<IBinder> getToken() { return mInfo.token; } |
| 1306 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1307 | const std::string& getName() { return mName; } |
| 1308 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1309 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { |
| 1310 | mInfo.ownerPid = ownerPid; |
| 1311 | mInfo.ownerUid = ownerUid; |
| 1312 | } |
| 1313 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1314 | int32_t getPid() const { return mInfo.ownerPid; } |
| 1315 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1316 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1317 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1318 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1319 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1320 | private: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1321 | const std::string mName; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1322 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1323 | 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] | 1324 | }; |
| 1325 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1326 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1327 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1328 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1329 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1330 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1331 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1332 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1333 | bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {}, |
| 1334 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1335 | KeyEvent event; |
| 1336 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1337 | |
| 1338 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1339 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1340 | INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE, |
| 1341 | repeatCount, currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1342 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1343 | if (!allowKeyRepeat) { |
| 1344 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1345 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1346 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1347 | return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1348 | } |
| 1349 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1350 | static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1351 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1352 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId); |
| 1353 | } |
| 1354 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1355 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1356 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1357 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1358 | static InputEventInjectionResult injectKeyDownNoRepeat( |
| 1359 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1360 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId, |
| 1361 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
| 1362 | /* allowKeyRepeat */ false); |
| 1363 | } |
| 1364 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1365 | static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1366 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1367 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId); |
| 1368 | } |
| 1369 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1370 | class PointerBuilder { |
| 1371 | public: |
| 1372 | PointerBuilder(int32_t id, int32_t toolType) { |
| 1373 | mProperties.clear(); |
| 1374 | mProperties.id = id; |
| 1375 | mProperties.toolType = toolType; |
| 1376 | mCoords.clear(); |
| 1377 | } |
| 1378 | |
| 1379 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } |
| 1380 | |
| 1381 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } |
| 1382 | |
| 1383 | PointerBuilder& axis(int32_t axis, float value) { |
| 1384 | mCoords.setAxisValue(axis, value); |
| 1385 | return *this; |
| 1386 | } |
| 1387 | |
| 1388 | PointerProperties buildProperties() const { return mProperties; } |
| 1389 | |
| 1390 | PointerCoords buildCoords() const { return mCoords; } |
| 1391 | |
| 1392 | private: |
| 1393 | PointerProperties mProperties; |
| 1394 | PointerCoords mCoords; |
| 1395 | }; |
| 1396 | |
| 1397 | class MotionEventBuilder { |
| 1398 | public: |
| 1399 | MotionEventBuilder(int32_t action, int32_t source) { |
| 1400 | mAction = action; |
| 1401 | mSource = source; |
| 1402 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1403 | } |
| 1404 | |
| 1405 | MotionEventBuilder& eventTime(nsecs_t eventTime) { |
| 1406 | mEventTime = eventTime; |
| 1407 | return *this; |
| 1408 | } |
| 1409 | |
| 1410 | MotionEventBuilder& displayId(int32_t displayId) { |
| 1411 | mDisplayId = displayId; |
| 1412 | return *this; |
| 1413 | } |
| 1414 | |
| 1415 | MotionEventBuilder& actionButton(int32_t actionButton) { |
| 1416 | mActionButton = actionButton; |
| 1417 | return *this; |
| 1418 | } |
| 1419 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1420 | MotionEventBuilder& buttonState(int32_t buttonState) { |
| 1421 | mButtonState = buttonState; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1422 | return *this; |
| 1423 | } |
| 1424 | |
| 1425 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 1426 | mRawXCursorPosition = rawXCursorPosition; |
| 1427 | return *this; |
| 1428 | } |
| 1429 | |
| 1430 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 1431 | mRawYCursorPosition = rawYCursorPosition; |
| 1432 | return *this; |
| 1433 | } |
| 1434 | |
| 1435 | MotionEventBuilder& pointer(PointerBuilder pointer) { |
| 1436 | mPointers.push_back(pointer); |
| 1437 | return *this; |
| 1438 | } |
| 1439 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1440 | MotionEventBuilder& addFlag(uint32_t flags) { |
| 1441 | mFlags |= flags; |
| 1442 | return *this; |
| 1443 | } |
| 1444 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1445 | MotionEvent build() { |
| 1446 | std::vector<PointerProperties> pointerProperties; |
| 1447 | std::vector<PointerCoords> pointerCoords; |
| 1448 | for (const PointerBuilder& pointer : mPointers) { |
| 1449 | pointerProperties.push_back(pointer.buildProperties()); |
| 1450 | pointerCoords.push_back(pointer.buildCoords()); |
| 1451 | } |
| 1452 | |
| 1453 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 1454 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 1455 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 1456 | mPointers.size() == 1) { |
| 1457 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 1458 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 1459 | } |
| 1460 | |
| 1461 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1462 | ui::Transform identityTransform; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1463 | event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC, |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1464 | mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1465 | mButtonState, MotionClassification::NONE, identityTransform, |
| 1466 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 1467 | mRawYCursorPosition, identityTransform, mEventTime, mEventTime, |
| 1468 | mPointers.size(), pointerProperties.data(), pointerCoords.data()); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1469 | |
| 1470 | return event; |
| 1471 | } |
| 1472 | |
| 1473 | private: |
| 1474 | int32_t mAction; |
| 1475 | int32_t mSource; |
| 1476 | nsecs_t mEventTime; |
| 1477 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1478 | int32_t mActionButton{0}; |
| 1479 | int32_t mButtonState{0}; |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1480 | int32_t mFlags{0}; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1481 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1482 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1483 | |
| 1484 | std::vector<PointerBuilder> mPointers; |
| 1485 | }; |
| 1486 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1487 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1488 | const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1489 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1490 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
| 1491 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
| 1492 | return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1493 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1496 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1497 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1498 | int32_t displayId, const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1499 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1500 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1501 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1502 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1503 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
| 1504 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1505 | MotionEvent event = MotionEventBuilder(action, source) |
| 1506 | .displayId(displayId) |
| 1507 | .eventTime(eventTime) |
| 1508 | .rawXCursorPosition(cursorPosition.x) |
| 1509 | .rawYCursorPosition(cursorPosition.y) |
| 1510 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1511 | .x(position.x) |
| 1512 | .y(position.y)) |
| 1513 | .build(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1514 | |
| 1515 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1516 | return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid, |
| 1517 | policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1518 | } |
| 1519 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1520 | static InputEventInjectionResult injectMotionDown( |
| 1521 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId, |
| 1522 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1523 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1526 | static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1527 | int32_t source, int32_t displayId, |
| 1528 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1529 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1532 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1533 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1534 | // Define a valid key event. |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1535 | NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 1536 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, |
| 1537 | KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1538 | |
| 1539 | return args; |
| 1540 | } |
| 1541 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1542 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, |
| 1543 | const std::vector<PointF>& points) { |
| 1544 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1545 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1546 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1547 | } |
| 1548 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1549 | PointerProperties pointerProperties[pointerCount]; |
| 1550 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1551 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1552 | for (size_t i = 0; i < pointerCount; i++) { |
| 1553 | pointerProperties[i].clear(); |
| 1554 | pointerProperties[i].id = i; |
| 1555 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1556 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1557 | pointerCoords[i].clear(); |
| 1558 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1559 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1560 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1561 | |
| 1562 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1563 | // Define a valid motion event. |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1564 | NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1565 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 1566 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1567 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 1568 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1569 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 1570 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1571 | |
| 1572 | return args; |
| 1573 | } |
| 1574 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1575 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1576 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1577 | } |
| 1578 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1579 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1580 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1581 | } |
| 1582 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1583 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1584 | const PointerCaptureRequest& request) { |
| 1585 | return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1586 | } |
| 1587 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1588 | /** |
| 1589 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1590 | * broken channel. |
| 1591 | */ |
| 1592 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1593 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1594 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1595 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1596 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1597 | |
| 1598 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1599 | |
| 1600 | // Window closes its channel, but the window remains. |
| 1601 | window->destroyReceiver(); |
| 1602 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1603 | } |
| 1604 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1605 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1606 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1607 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1608 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1609 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1610 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1611 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1612 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1613 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1614 | |
| 1615 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1616 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1617 | } |
| 1618 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1619 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1620 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1621 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1622 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1623 | |
| 1624 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1625 | // Inject a MotionEvent to an unknown display. |
| 1626 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1627 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
| 1628 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1629 | |
| 1630 | // Window should receive motion event. |
| 1631 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1632 | } |
| 1633 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1634 | /** |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1635 | * Calling setInputWindows once should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1636 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1637 | * called twice. |
| 1638 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1639 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1640 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1641 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1642 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1643 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1644 | |
| 1645 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1646 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1647 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1648 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1649 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1650 | |
| 1651 | // Window should receive motion event. |
| 1652 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1653 | } |
| 1654 | |
| 1655 | /** |
| 1656 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1657 | */ |
| 1658 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1659 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1660 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1661 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1662 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1663 | |
| 1664 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1665 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1666 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1667 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1668 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1669 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1670 | |
| 1671 | // Window should receive motion event. |
| 1672 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1673 | } |
| 1674 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1675 | // The foreground window should receive the first touch down event. |
| 1676 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1677 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1678 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1679 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1680 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1681 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1682 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1683 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1684 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1685 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1686 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1687 | |
| 1688 | // 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] | 1689 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1690 | windowSecond->assertNoEvents(); |
| 1691 | } |
| 1692 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1693 | /** |
| 1694 | * Two windows: A top window, and a wallpaper behind the window. |
| 1695 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1696 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1697 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1698 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1699 | */ |
| 1700 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1701 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1702 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1703 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1704 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1705 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1706 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1707 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1708 | constexpr int expectedWallpaperFlags = |
| 1709 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1710 | |
| 1711 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1712 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1713 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1714 | {100, 200})) |
| 1715 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1716 | |
| 1717 | // Both foreground window and its wallpaper should receive the touch down |
| 1718 | foregroundWindow->consumeMotionDown(); |
| 1719 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1720 | |
| 1721 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1722 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1723 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1724 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1725 | |
| 1726 | foregroundWindow->consumeMotionMove(); |
| 1727 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1728 | |
| 1729 | // Now the foreground window goes away, but the wallpaper stays |
| 1730 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1731 | foregroundWindow->consumeMotionCancel(); |
| 1732 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1733 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1734 | } |
| 1735 | |
| 1736 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1737 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1738 | * with the following differences: |
| 1739 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1740 | * clean up the connection. |
| 1741 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1742 | * Ensure that there's no crash in the dispatcher. |
| 1743 | */ |
| 1744 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1745 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1746 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1747 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1748 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1749 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1750 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1751 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1752 | constexpr int expectedWallpaperFlags = |
| 1753 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1754 | |
| 1755 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1756 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1757 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1758 | {100, 200})) |
| 1759 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1760 | |
| 1761 | // Both foreground window and its wallpaper should receive the touch down |
| 1762 | foregroundWindow->consumeMotionDown(); |
| 1763 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1764 | |
| 1765 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1766 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1767 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1768 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1769 | |
| 1770 | foregroundWindow->consumeMotionMove(); |
| 1771 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1772 | |
| 1773 | // Wallpaper closes its channel, but the window remains. |
| 1774 | wallpaperWindow->destroyReceiver(); |
| 1775 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1776 | |
| 1777 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1778 | // is no longer valid. |
| 1779 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1780 | foregroundWindow->consumeMotionCancel(); |
| 1781 | } |
| 1782 | |
| 1783 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1784 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1785 | * The top window gets a multitouch gesture. |
| 1786 | * Ensure that wallpaper gets the same gesture. |
| 1787 | */ |
| 1788 | TEST_F(InputDispatcherTest, WallpaperWindow_ReceivesMultiTouch) { |
| 1789 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1790 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1791 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1792 | window->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1793 | |
| 1794 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1795 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1796 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1797 | constexpr int expectedWallpaperFlags = |
| 1798 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1799 | |
| 1800 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, wallpaperWindow}}}); |
| 1801 | |
| 1802 | // Touch down on top window |
| 1803 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1804 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1805 | {100, 100})) |
| 1806 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1807 | |
| 1808 | // Both top window and its wallpaper should receive the touch down |
| 1809 | window->consumeMotionDown(); |
| 1810 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1811 | |
| 1812 | // Second finger down on the top window |
| 1813 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1814 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1815 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1816 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1817 | .x(100) |
| 1818 | .y(100)) |
| 1819 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1820 | .x(150) |
| 1821 | .y(150)) |
| 1822 | .build(); |
| 1823 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1824 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1825 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1826 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1827 | |
| 1828 | window->consumeMotionPointerDown(1 /* pointerIndex */); |
| 1829 | wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT, |
| 1830 | expectedWallpaperFlags); |
| 1831 | window->assertNoEvents(); |
| 1832 | wallpaperWindow->assertNoEvents(); |
| 1833 | } |
| 1834 | |
| 1835 | /** |
| 1836 | * Two windows: a window on the left and window on the right. |
| 1837 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1838 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1839 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1840 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1841 | * ACTION_POINTER_DOWN(1). |
| 1842 | */ |
| 1843 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1844 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1845 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1846 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1847 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1848 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1849 | |
| 1850 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1851 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1852 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1853 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1854 | |
| 1855 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1856 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1857 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1858 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1859 | constexpr int expectedWallpaperFlags = |
| 1860 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1861 | |
| 1862 | mDispatcher->setInputWindows( |
| 1863 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
| 1864 | |
| 1865 | // Touch down on left window |
| 1866 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1867 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1868 | {100, 100})) |
| 1869 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1870 | |
| 1871 | // Both foreground window and its wallpaper should receive the touch down |
| 1872 | leftWindow->consumeMotionDown(); |
| 1873 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1874 | |
| 1875 | // Second finger down on the right window |
| 1876 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1877 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1878 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1879 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1880 | .x(100) |
| 1881 | .y(100)) |
| 1882 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1883 | .x(300) |
| 1884 | .y(100)) |
| 1885 | .build(); |
| 1886 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1887 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1888 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1889 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1890 | |
| 1891 | leftWindow->consumeMotionMove(); |
| 1892 | // Since the touch is split, right window gets ACTION_DOWN |
| 1893 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1894 | wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT, |
| 1895 | expectedWallpaperFlags); |
| 1896 | |
| 1897 | // Now, leftWindow, which received the first finger, disappears. |
| 1898 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); |
| 1899 | leftWindow->consumeMotionCancel(); |
| 1900 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1901 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1902 | |
| 1903 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 1904 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 1905 | const MotionEvent secondFingerMoveEvent = |
| 1906 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1907 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1908 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1909 | .x(100) |
| 1910 | .y(100)) |
| 1911 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1912 | .x(310) |
| 1913 | .y(110)) |
| 1914 | .build(); |
| 1915 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1916 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 1917 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 1918 | rightWindow->consumeMotionMove(); |
| 1919 | |
| 1920 | leftWindow->assertNoEvents(); |
| 1921 | rightWindow->assertNoEvents(); |
| 1922 | wallpaperWindow->assertNoEvents(); |
| 1923 | } |
| 1924 | |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1925 | TEST_F(InputDispatcherTest, WallpaperWindowReceivesMultiTouch) { |
| 1926 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1927 | sp<FakeWindowHandle> window = |
| 1928 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 1929 | window->setDupTouchToWallpaper(true); |
| 1930 | |
| 1931 | sp<FakeWindowHandle> wallpaperWindow = |
| 1932 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 1933 | wallpaperWindow->setIsWallpaper(true); |
| 1934 | constexpr int expectedWallpaperFlags = |
| 1935 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 1936 | wallpaperWindow->setPreventSplitting(true); |
| 1937 | |
| 1938 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, wallpaperWindow}}}); |
| 1939 | |
| 1940 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1941 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1942 | {50, 50})) |
| 1943 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1944 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1945 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1946 | |
| 1947 | const MotionEvent secondFingerDownEvent = |
| 1948 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1949 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1950 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1951 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 1952 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 1953 | .build(); |
| 1954 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1955 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1956 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1957 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1958 | |
| 1959 | window->consumeMotionPointerDown(1); |
| 1960 | wallpaperWindow->consumeMotionPointerDown(1, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1961 | |
| 1962 | const MotionEvent secondFingerUpEvent = |
| 1963 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1964 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1965 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1966 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 1967 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 1968 | .build(); |
| 1969 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1970 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 1971 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1972 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1973 | window->consumeMotionPointerUp(1); |
| 1974 | wallpaperWindow->consumeMotionPointerUp(1, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1975 | |
| 1976 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1977 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 1978 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1979 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1980 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1981 | } |
| 1982 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1983 | /** |
| 1984 | * On the display, have a single window, and also an area where there's no window. |
| 1985 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 1986 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 1987 | */ |
| 1988 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 1989 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1990 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1991 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1992 | |
| 1993 | mDispatcher->setInputWindows({{DISPLAY_ID, {window}}}); |
| 1994 | NotifyMotionArgs args; |
| 1995 | |
| 1996 | // Touch down on the empty space |
| 1997 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}))); |
| 1998 | |
| 1999 | mDispatcher->waitForIdle(); |
| 2000 | window->assertNoEvents(); |
| 2001 | |
| 2002 | // Now touch down on the window with another pointer |
| 2003 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}))); |
| 2004 | mDispatcher->waitForIdle(); |
| 2005 | window->consumeMotionDown(); |
| 2006 | } |
| 2007 | |
| 2008 | /** |
| 2009 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 2010 | * non-touchable window. |
| 2011 | */ |
| 2012 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 2013 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2014 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2015 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2016 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2017 | window1->setTouchable(false); |
| 2018 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2019 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2020 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2021 | |
| 2022 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2023 | |
| 2024 | NotifyMotionArgs args; |
| 2025 | // Touch down on the non-touchable window |
| 2026 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); |
| 2027 | |
| 2028 | mDispatcher->waitForIdle(); |
| 2029 | window1->assertNoEvents(); |
| 2030 | window2->assertNoEvents(); |
| 2031 | |
| 2032 | // Now touch down on the window with another pointer |
| 2033 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); |
| 2034 | mDispatcher->waitForIdle(); |
| 2035 | window2->consumeMotionDown(); |
| 2036 | } |
| 2037 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2038 | /** |
| 2039 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 2040 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 2041 | */ |
| 2042 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 2043 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2044 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2045 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2046 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2047 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2048 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2049 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2050 | |
| 2051 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2052 | |
| 2053 | NotifyMotionArgs args; |
| 2054 | // Touch down on the first window |
| 2055 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); |
| 2056 | |
| 2057 | mDispatcher->waitForIdle(); |
| 2058 | InputEvent* inputEvent1 = window1->consume(); |
| 2059 | window2->assertNoEvents(); |
| 2060 | MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1); |
| 2061 | nsecs_t downTimeForWindow1 = motionEvent1.getDownTime(); |
| 2062 | ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime()); |
| 2063 | |
| 2064 | // Now touch down on the window with another pointer |
| 2065 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); |
| 2066 | mDispatcher->waitForIdle(); |
| 2067 | InputEvent* inputEvent2 = window2->consume(); |
| 2068 | MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2); |
| 2069 | nsecs_t downTimeForWindow2 = motionEvent2.getDownTime(); |
| 2070 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
| 2071 | ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime()); |
| 2072 | |
| 2073 | // Now move the pointer on the second window |
| 2074 | mDispatcher->notifyMotion( |
| 2075 | &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}))); |
| 2076 | mDispatcher->waitForIdle(); |
| 2077 | InputEvent* inputEvent3 = window2->consume(); |
| 2078 | MotionEvent& motionEvent3 = static_cast<MotionEvent&>(*inputEvent3); |
| 2079 | ASSERT_EQ(motionEvent3.getDownTime(), downTimeForWindow2); |
| 2080 | |
| 2081 | // Now add new touch down on the second window |
| 2082 | mDispatcher->notifyMotion( |
| 2083 | &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}))); |
| 2084 | mDispatcher->waitForIdle(); |
| 2085 | InputEvent* inputEvent4 = window2->consume(); |
| 2086 | MotionEvent& motionEvent4 = static_cast<MotionEvent&>(*inputEvent4); |
| 2087 | ASSERT_EQ(motionEvent4.getDownTime(), downTimeForWindow2); |
| 2088 | |
| 2089 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 2090 | window1->consumeMotionMove(); |
| 2091 | window1->assertNoEvents(); |
| 2092 | |
| 2093 | // Now move the pointer on the first window |
| 2094 | mDispatcher->notifyMotion( |
| 2095 | &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}}))); |
| 2096 | mDispatcher->waitForIdle(); |
| 2097 | InputEvent* inputEvent5 = window1->consume(); |
| 2098 | MotionEvent& motionEvent5 = static_cast<MotionEvent&>(*inputEvent5); |
| 2099 | ASSERT_EQ(motionEvent5.getDownTime(), downTimeForWindow1); |
| 2100 | |
| 2101 | mDispatcher->notifyMotion(&( |
| 2102 | args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}))); |
| 2103 | mDispatcher->waitForIdle(); |
| 2104 | InputEvent* inputEvent6 = window1->consume(); |
| 2105 | MotionEvent& motionEvent6 = static_cast<MotionEvent&>(*inputEvent6); |
| 2106 | ASSERT_EQ(motionEvent6.getDownTime(), downTimeForWindow1); |
| 2107 | } |
| 2108 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2109 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2110 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2111 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2112 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2113 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2114 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2115 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2116 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2117 | |
| 2118 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2119 | |
| 2120 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 2121 | |
| 2122 | // 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] | 2123 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2124 | injectMotionEvent(mDispatcher, |
| 2125 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2126 | AINPUT_SOURCE_MOUSE) |
| 2127 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2128 | .x(900) |
| 2129 | .y(400)) |
| 2130 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame^] | 2131 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2132 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2133 | |
| 2134 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2135 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2136 | injectMotionEvent(mDispatcher, |
| 2137 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2138 | AINPUT_SOURCE_MOUSE) |
| 2139 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2140 | .x(300) |
| 2141 | .y(400)) |
| 2142 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame^] | 2143 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2144 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2145 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2146 | |
| 2147 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2148 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2149 | injectMotionEvent(mDispatcher, |
| 2150 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2151 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2152 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2153 | .x(300) |
| 2154 | .y(400)) |
| 2155 | .build())); |
| 2156 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2157 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2158 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2159 | injectMotionEvent(mDispatcher, |
| 2160 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 2161 | AINPUT_SOURCE_MOUSE) |
| 2162 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2163 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2164 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2165 | .x(300) |
| 2166 | .y(400)) |
| 2167 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame^] | 2168 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2169 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2170 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2171 | injectMotionEvent(mDispatcher, |
| 2172 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2173 | AINPUT_SOURCE_MOUSE) |
| 2174 | .buttonState(0) |
| 2175 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2176 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2177 | .x(300) |
| 2178 | .y(400)) |
| 2179 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame^] | 2180 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2181 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2182 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2183 | injectMotionEvent(mDispatcher, |
| 2184 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 2185 | .buttonState(0) |
| 2186 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2187 | .x(300) |
| 2188 | .y(400)) |
| 2189 | .build())); |
| 2190 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2191 | |
| 2192 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2193 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2194 | injectMotionEvent(mDispatcher, |
| 2195 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2196 | AINPUT_SOURCE_MOUSE) |
| 2197 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2198 | .x(900) |
| 2199 | .y(400)) |
| 2200 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame^] | 2201 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2202 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2203 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)); |
| 2204 | |
| 2205 | // No more events |
| 2206 | windowLeft->assertNoEvents(); |
| 2207 | windowRight->assertNoEvents(); |
| 2208 | } |
| 2209 | |
| 2210 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 2211 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2212 | |
| 2213 | sp<FakeWindowHandle> spyWindow = |
| 2214 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2215 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 2216 | spyWindow->setTrustedOverlay(true); |
| 2217 | spyWindow->setSpy(true); |
| 2218 | sp<FakeWindowHandle> window = |
| 2219 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2220 | window->setFrame(Rect(0, 0, 600, 800)); |
| 2221 | |
| 2222 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2223 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2224 | |
| 2225 | // Send mouse cursor to the window |
| 2226 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2227 | injectMotionEvent(mDispatcher, |
| 2228 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2229 | AINPUT_SOURCE_MOUSE) |
| 2230 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2231 | .x(100) |
| 2232 | .y(100)) |
| 2233 | .build())); |
| 2234 | |
| 2235 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2236 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2237 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2238 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2239 | |
| 2240 | window->assertNoEvents(); |
| 2241 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 2245 | // directly in this test. |
| 2246 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2247 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2248 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2249 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2250 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2251 | |
| 2252 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2253 | |
| 2254 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2255 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2256 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2257 | injectMotionEvent(mDispatcher, |
| 2258 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2259 | AINPUT_SOURCE_MOUSE) |
| 2260 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2261 | .x(300) |
| 2262 | .y(400)) |
| 2263 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame^] | 2264 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2265 | |
| 2266 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2267 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2268 | injectMotionEvent(mDispatcher, |
| 2269 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2270 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2271 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2272 | .x(300) |
| 2273 | .y(400)) |
| 2274 | .build())); |
| 2275 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2276 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2277 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2278 | injectMotionEvent(mDispatcher, |
| 2279 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 2280 | AINPUT_SOURCE_MOUSE) |
| 2281 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2282 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2283 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2284 | .x(300) |
| 2285 | .y(400)) |
| 2286 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame^] | 2287 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2288 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2289 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2290 | injectMotionEvent(mDispatcher, |
| 2291 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2292 | AINPUT_SOURCE_MOUSE) |
| 2293 | .buttonState(0) |
| 2294 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2295 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2296 | .x(300) |
| 2297 | .y(400)) |
| 2298 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame^] | 2299 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2300 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2301 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2302 | injectMotionEvent(mDispatcher, |
| 2303 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 2304 | .buttonState(0) |
| 2305 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2306 | .x(300) |
| 2307 | .y(400)) |
| 2308 | .build())); |
| 2309 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2310 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2311 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2312 | injectMotionEvent(mDispatcher, |
| 2313 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 2314 | AINPUT_SOURCE_MOUSE) |
| 2315 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2316 | .x(300) |
| 2317 | .y(400)) |
| 2318 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame^] | 2319 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2320 | } |
| 2321 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2322 | /** |
| 2323 | * Inject a mouse hover event followed by a tap from touchscreen. |
| 2324 | * In the current implementation, the tap does not cause a HOVER_EXIT event. |
| 2325 | */ |
| 2326 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 2327 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2328 | sp<FakeWindowHandle> window = |
| 2329 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2330 | window->setFrame(Rect(0, 0, 100, 100)); |
| 2331 | |
| 2332 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2333 | |
| 2334 | // Inject a hover_move from mouse. |
| 2335 | NotifyMotionArgs motionArgs = |
| 2336 | generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE, |
| 2337 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 2338 | motionArgs.xCursorPosition = 50; |
| 2339 | motionArgs.yCursorPosition = 50; |
| 2340 | mDispatcher->notifyMotion(&motionArgs); |
| 2341 | ASSERT_NO_FATAL_FAILURE( |
| 2342 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2343 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 2344 | ASSERT_NO_FATAL_FAILURE( |
| 2345 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 2346 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 2347 | |
| 2348 | // Tap on the window |
| 2349 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2350 | ADISPLAY_ID_DEFAULT, {{10, 10}}); |
| 2351 | mDispatcher->notifyMotion(&motionArgs); |
| 2352 | ASSERT_NO_FATAL_FAILURE( |
| 2353 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2354 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 2355 | |
| 2356 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2357 | ADISPLAY_ID_DEFAULT, {{10, 10}}); |
| 2358 | mDispatcher->notifyMotion(&motionArgs); |
| 2359 | ASSERT_NO_FATAL_FAILURE( |
| 2360 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 2361 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 2362 | } |
| 2363 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2364 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 2365 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2366 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 2367 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 2368 | ADISPLAY_ID_DEFAULT); |
| 2369 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 2370 | sp<FakeWindowHandle> windowSecondDisplay = |
| 2371 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 2372 | SECOND_DISPLAY_ID); |
| 2373 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 2374 | |
| 2375 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 2376 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 2377 | |
| 2378 | // Set cursor position in window in default display and check that hover enter and move |
| 2379 | // events are generated. |
| 2380 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2381 | injectMotionEvent(mDispatcher, |
| 2382 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2383 | AINPUT_SOURCE_MOUSE) |
| 2384 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2385 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2386 | .x(300) |
| 2387 | .y(600)) |
| 2388 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame^] | 2389 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2390 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2391 | |
| 2392 | // Remove all windows in secondary display and check that no event happens on window in |
| 2393 | // primary display. |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame^] | 2394 | mDispatcher->setInputWindows( |
| 2395 | {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2396 | windowDefaultDisplay->assertNoEvents(); |
| 2397 | |
| 2398 | // Move cursor position in window in default display and check that only hover move |
| 2399 | // event is generated and not hover enter event. |
| 2400 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 2401 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 2402 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2403 | injectMotionEvent(mDispatcher, |
| 2404 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2405 | AINPUT_SOURCE_MOUSE) |
| 2406 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2407 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2408 | .x(400) |
| 2409 | .y(700)) |
| 2410 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame^] | 2411 | windowDefaultDisplay->consumeMotionEvent( |
| 2412 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 2413 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2414 | windowDefaultDisplay->assertNoEvents(); |
| 2415 | } |
| 2416 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2417 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2418 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2419 | |
| 2420 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2421 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2422 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2423 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2424 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2425 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2426 | |
| 2427 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2428 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2429 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2430 | |
| 2431 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 2432 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2433 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2434 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2435 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2436 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2437 | windowRight->assertNoEvents(); |
| 2438 | } |
| 2439 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2440 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2441 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2442 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2443 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2444 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2445 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2446 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2447 | setFocusedWindow(window); |
| 2448 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2449 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2450 | |
| 2451 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2452 | mDispatcher->notifyKey(&keyArgs); |
| 2453 | |
| 2454 | // Window should receive key down event. |
| 2455 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2456 | |
| 2457 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 2458 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2459 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2460 | mDispatcher->notifyDeviceReset(&args); |
| 2461 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 2462 | AKEY_EVENT_FLAG_CANCELED); |
| 2463 | } |
| 2464 | |
| 2465 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2466 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2467 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2468 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2469 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2470 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2471 | |
| 2472 | NotifyMotionArgs motionArgs = |
| 2473 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2474 | ADISPLAY_ID_DEFAULT); |
| 2475 | mDispatcher->notifyMotion(&motionArgs); |
| 2476 | |
| 2477 | // Window should receive motion down event. |
| 2478 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2479 | |
| 2480 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 2481 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2482 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2483 | mDispatcher->notifyDeviceReset(&args); |
| 2484 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, |
| 2485 | 0 /*expectedFlags*/); |
| 2486 | } |
| 2487 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2488 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 2489 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2490 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2491 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2492 | window->setFocusable(true); |
| 2493 | |
| 2494 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2495 | setFocusedWindow(window); |
| 2496 | |
| 2497 | window->consumeFocusEvent(true); |
| 2498 | |
| 2499 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2500 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 2501 | const nsecs_t injectTime = keyArgs.eventTime; |
| 2502 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
| 2503 | mDispatcher->notifyKey(&keyArgs); |
| 2504 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 2505 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2506 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 2507 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 2508 | } |
| 2509 | |
| 2510 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 2511 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2512 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2513 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2514 | window->setFocusable(true); |
| 2515 | |
| 2516 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2517 | setFocusedWindow(window); |
| 2518 | |
| 2519 | window->consumeFocusEvent(true); |
| 2520 | |
| 2521 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2522 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 2523 | mFakePolicy->setInterceptKeyTimeout(150ms); |
| 2524 | mDispatcher->notifyKey(&keyDown); |
| 2525 | mDispatcher->notifyKey(&keyUp); |
| 2526 | |
| 2527 | // Window should receive key event immediately when same key up. |
| 2528 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2529 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 2530 | } |
| 2531 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2532 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2533 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 2534 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 2535 | * ACTION_OUTSIDE event is sent per gesture. |
| 2536 | */ |
| 2537 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 2538 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 2539 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2540 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2541 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2542 | window->setWatchOutsideTouch(true); |
| 2543 | window->setFrame(Rect{0, 0, 100, 100}); |
| 2544 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2545 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 2546 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2547 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 2548 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2549 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 2550 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2551 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
| 2552 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}}); |
| 2553 | |
| 2554 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
| 2555 | NotifyMotionArgs motionArgs = |
| 2556 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2557 | ADISPLAY_ID_DEFAULT, {PointF{-10, -10}}); |
| 2558 | mDispatcher->notifyMotion(&motionArgs); |
| 2559 | window->assertNoEvents(); |
| 2560 | secondWindow->assertNoEvents(); |
| 2561 | |
| 2562 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 2563 | // Now, `window` should get ACTION_OUTSIDE. |
| 2564 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2565 | {PointF{-10, -10}, PointF{105, 105}}); |
| 2566 | mDispatcher->notifyMotion(&motionArgs); |
| 2567 | window->consumeMotionOutside(); |
| 2568 | secondWindow->consumeMotionDown(); |
| 2569 | thirdWindow->assertNoEvents(); |
| 2570 | |
| 2571 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 2572 | // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture. |
| 2573 | motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2574 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}); |
| 2575 | mDispatcher->notifyMotion(&motionArgs); |
| 2576 | window->assertNoEvents(); |
| 2577 | secondWindow->consumeMotionMove(); |
| 2578 | thirdWindow->consumeMotionDown(); |
| 2579 | } |
| 2580 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 2581 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 2582 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2583 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2584 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 2585 | window->setFocusable(true); |
| 2586 | |
| 2587 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); |
| 2588 | setFocusedWindow(window); |
| 2589 | |
| 2590 | window->consumeFocusEvent(true); |
| 2591 | |
| 2592 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2593 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 2594 | mDispatcher->notifyKey(&keyDown); |
| 2595 | mDispatcher->notifyKey(&keyUp); |
| 2596 | |
| 2597 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2598 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 2599 | |
| 2600 | // All windows are removed from the display. Ensure that we can no longer dispatch to it. |
| 2601 | mDispatcher->onWindowInfosChanged({}, {}); |
| 2602 | |
| 2603 | window->consumeFocusEvent(false); |
| 2604 | |
| 2605 | mDispatcher->notifyKey(&keyDown); |
| 2606 | mDispatcher->notifyKey(&keyUp); |
| 2607 | window->assertNoEvents(); |
| 2608 | } |
| 2609 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2610 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 2611 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2612 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2613 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2614 | // Ensure window is non-split and have some transform. |
| 2615 | window->setPreventSplitting(true); |
| 2616 | window->setWindowOffset(20, 40); |
| 2617 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); |
| 2618 | |
| 2619 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2620 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2621 | {50, 50})) |
| 2622 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2623 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2624 | |
| 2625 | const MotionEvent secondFingerDownEvent = |
| 2626 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2627 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2628 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 2629 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 2630 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2631 | .x(-30) |
| 2632 | .y(-50)) |
| 2633 | .build(); |
| 2634 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2635 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 2636 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2637 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2638 | |
| 2639 | const MotionEvent* event = window->consumeMotion(); |
| 2640 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 2641 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 2642 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 2643 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 2644 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
| 2645 | } |
| 2646 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2647 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2648 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 2649 | * |
| 2650 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 2651 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 2652 | * space. |
| 2653 | */ |
| 2654 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 2655 | public: |
| 2656 | void SetUp() override { |
| 2657 | InputDispatcherTest::SetUp(); |
| 2658 | mDisplayInfos.clear(); |
| 2659 | mWindowInfos.clear(); |
| 2660 | } |
| 2661 | |
| 2662 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 2663 | gui::DisplayInfo info; |
| 2664 | info.displayId = displayId; |
| 2665 | info.transform = transform; |
| 2666 | mDisplayInfos.push_back(std::move(info)); |
| 2667 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 2668 | } |
| 2669 | |
| 2670 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 2671 | mWindowInfos.push_back(*windowHandle->getInfo()); |
| 2672 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 2673 | } |
| 2674 | |
| 2675 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 2676 | // on the display. |
| 2677 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 2678 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 2679 | // respectively. |
| 2680 | ui::Transform displayTransform; |
| 2681 | displayTransform.set(2, 0, 0, 4); |
| 2682 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 2683 | |
| 2684 | std::shared_ptr<FakeApplicationHandle> application = |
| 2685 | std::make_shared<FakeApplicationHandle>(); |
| 2686 | |
| 2687 | // Add two windows to the display. Their frames are represented in the display space. |
| 2688 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2689 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 2690 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2691 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 2692 | addWindow(firstWindow); |
| 2693 | |
| 2694 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2695 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 2696 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2697 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 2698 | addWindow(secondWindow); |
| 2699 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 2700 | } |
| 2701 | |
| 2702 | private: |
| 2703 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 2704 | std::vector<gui::WindowInfo> mWindowInfos; |
| 2705 | }; |
| 2706 | |
| 2707 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) { |
| 2708 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2709 | // Send down to the first window. The point is represented in the display space. The point is |
| 2710 | // selected so that if the hit test was done with the transform applied to it, then it would |
| 2711 | // end up in the incorrect window. |
| 2712 | NotifyMotionArgs downMotionArgs = |
| 2713 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2714 | ADISPLAY_ID_DEFAULT, {PointF{75, 55}}); |
| 2715 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2716 | |
| 2717 | firstWindow->consumeMotionDown(); |
| 2718 | secondWindow->assertNoEvents(); |
| 2719 | } |
| 2720 | |
| 2721 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 2722 | // the event should be treated as being in the logical display space. |
| 2723 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 2724 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2725 | // Send down to the first window. The point is represented in the logical display space. The |
| 2726 | // point is selected so that if the hit test was done in logical display space, then it would |
| 2727 | // end up in the incorrect window. |
| 2728 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2729 | PointF{75 * 2, 55 * 4}); |
| 2730 | |
| 2731 | firstWindow->consumeMotionDown(); |
| 2732 | secondWindow->assertNoEvents(); |
| 2733 | } |
| 2734 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 2735 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 2736 | // event should be treated as being in the logical display space. |
| 2737 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 2738 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2739 | |
| 2740 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 2741 | ui::Transform injectedEventTransform; |
| 2742 | injectedEventTransform.set(matrix); |
| 2743 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 2744 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 2745 | |
| 2746 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2747 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2748 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 2749 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2750 | .x(untransformedPoint.x) |
| 2751 | .y(untransformedPoint.y)) |
| 2752 | .build(); |
| 2753 | event.transform(matrix); |
| 2754 | |
| 2755 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, |
| 2756 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 2757 | |
| 2758 | firstWindow->consumeMotionDown(); |
| 2759 | secondWindow->assertNoEvents(); |
| 2760 | } |
| 2761 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2762 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 2763 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2764 | |
| 2765 | // Send down to the second window. |
| 2766 | NotifyMotionArgs downMotionArgs = |
| 2767 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2768 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}}); |
| 2769 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2770 | |
| 2771 | firstWindow->assertNoEvents(); |
| 2772 | const MotionEvent* event = secondWindow->consumeMotion(); |
| 2773 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 2774 | |
| 2775 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 2776 | EXPECT_EQ(300, event->getRawX(0)); |
| 2777 | EXPECT_EQ(880, event->getRawY(0)); |
| 2778 | |
| 2779 | // Ensure that the x and y values are in the window's coordinate space. |
| 2780 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 2781 | // the logical display space. This will be the origin of the window space. |
| 2782 | EXPECT_EQ(100, event->getX(0)); |
| 2783 | EXPECT_EQ(80, event->getY(0)); |
| 2784 | } |
| 2785 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2786 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 2787 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2788 | |
| 2789 | class TransferTouchFixture : public InputDispatcherTest, |
| 2790 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 2791 | |
| 2792 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2793 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2794 | |
| 2795 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2796 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2797 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 2798 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2799 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2800 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 2801 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2802 | |
| 2803 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2804 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2805 | |
| 2806 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2807 | NotifyMotionArgs downMotionArgs = |
| 2808 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2809 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2810 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2811 | // Only the first window should get the down event |
| 2812 | firstWindow->consumeMotionDown(); |
| 2813 | secondWindow->assertNoEvents(); |
| 2814 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2815 | // Transfer touch to the second window |
| 2816 | TransferFunction f = GetParam(); |
| 2817 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 2818 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2819 | // The first window gets cancel and the second gets down |
| 2820 | firstWindow->consumeMotionCancel(); |
| 2821 | secondWindow->consumeMotionDown(); |
| 2822 | |
| 2823 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2824 | NotifyMotionArgs upMotionArgs = |
| 2825 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2826 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2827 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2828 | // The first window gets no events and the second gets up |
| 2829 | firstWindow->assertNoEvents(); |
| 2830 | secondWindow->consumeMotionUp(); |
| 2831 | } |
| 2832 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2833 | /** |
| 2834 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 2835 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 2836 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 2837 | * natural to the user. |
| 2838 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 2839 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 2840 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 2841 | * the first window to the second. |
| 2842 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 2843 | * the other API, as well. |
| 2844 | */ |
| 2845 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 2846 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2847 | |
| 2848 | // Create a couple of windows + a spy window |
| 2849 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2850 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2851 | spyWindow->setTrustedOverlay(true); |
| 2852 | spyWindow->setSpy(true); |
| 2853 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2854 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2855 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2856 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2857 | |
| 2858 | // Add the windows to the dispatcher |
| 2859 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}}); |
| 2860 | |
| 2861 | // Send down to the first window |
| 2862 | NotifyMotionArgs downMotionArgs = |
| 2863 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2864 | ADISPLAY_ID_DEFAULT); |
| 2865 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2866 | // Only the first window and spy should get the down event |
| 2867 | spyWindow->consumeMotionDown(); |
| 2868 | firstWindow->consumeMotionDown(); |
| 2869 | |
| 2870 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 2871 | // if f === 'transferTouch'. |
| 2872 | TransferFunction f = GetParam(); |
| 2873 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 2874 | ASSERT_TRUE(success); |
| 2875 | // The first window gets cancel and the second gets down |
| 2876 | firstWindow->consumeMotionCancel(); |
| 2877 | secondWindow->consumeMotionDown(); |
| 2878 | |
| 2879 | // Send up event to the second window |
| 2880 | NotifyMotionArgs upMotionArgs = |
| 2881 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2882 | ADISPLAY_ID_DEFAULT); |
| 2883 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2884 | // The first window gets no events and the second+spy get up |
| 2885 | firstWindow->assertNoEvents(); |
| 2886 | spyWindow->consumeMotionUp(); |
| 2887 | secondWindow->consumeMotionUp(); |
| 2888 | } |
| 2889 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2890 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2891 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2892 | |
| 2893 | PointF touchPoint = {10, 10}; |
| 2894 | |
| 2895 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2896 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2897 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 2898 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 2899 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2900 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2901 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 2902 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 2903 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2904 | |
| 2905 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2906 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2907 | |
| 2908 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2909 | NotifyMotionArgs downMotionArgs = |
| 2910 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2911 | ADISPLAY_ID_DEFAULT, {touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2912 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2913 | // Only the first window should get the down event |
| 2914 | firstWindow->consumeMotionDown(); |
| 2915 | secondWindow->assertNoEvents(); |
| 2916 | |
| 2917 | // Send pointer down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2918 | NotifyMotionArgs pointerDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2919 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2920 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2921 | mDispatcher->notifyMotion(&pointerDownMotionArgs); |
| 2922 | // Only the first window should get the pointer down event |
| 2923 | firstWindow->consumeMotionPointerDown(1); |
| 2924 | secondWindow->assertNoEvents(); |
| 2925 | |
| 2926 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2927 | TransferFunction f = GetParam(); |
| 2928 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 2929 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2930 | // The first window gets cancel and the second gets down and pointer down |
| 2931 | firstWindow->consumeMotionCancel(); |
| 2932 | secondWindow->consumeMotionDown(); |
| 2933 | secondWindow->consumeMotionPointerDown(1); |
| 2934 | |
| 2935 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2936 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2937 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2938 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2939 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 2940 | // The first window gets nothing and the second gets pointer up |
| 2941 | firstWindow->assertNoEvents(); |
| 2942 | secondWindow->consumeMotionPointerUp(1); |
| 2943 | |
| 2944 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2945 | NotifyMotionArgs upMotionArgs = |
| 2946 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2947 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2948 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2949 | // The first window gets nothing and the second gets up |
| 2950 | firstWindow->assertNoEvents(); |
| 2951 | secondWindow->consumeMotionUp(); |
| 2952 | } |
| 2953 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2954 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 2955 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 2956 | // for the case where there are multiple pointers split across several windows. |
| 2957 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 2958 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2959 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 2960 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2961 | return dispatcher->transferTouch(destChannelToken, |
| 2962 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2963 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2964 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 2965 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2966 | return dispatcher->transferTouchFocus(from, to, |
| 2967 | false /*isDragAndDrop*/); |
| 2968 | })); |
| 2969 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2970 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2971 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2972 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2973 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2974 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 2975 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2976 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2977 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2978 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2979 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 2980 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2981 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2982 | |
| 2983 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2984 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2985 | |
| 2986 | PointF pointInFirst = {300, 200}; |
| 2987 | PointF pointInSecond = {300, 600}; |
| 2988 | |
| 2989 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2990 | NotifyMotionArgs firstDownMotionArgs = |
| 2991 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2992 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2993 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 2994 | // Only the first window should get the down event |
| 2995 | firstWindow->consumeMotionDown(); |
| 2996 | secondWindow->assertNoEvents(); |
| 2997 | |
| 2998 | // Send down to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2999 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3000 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3001 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3002 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3003 | // The first window gets a move and the second a down |
| 3004 | firstWindow->consumeMotionMove(); |
| 3005 | secondWindow->consumeMotionDown(); |
| 3006 | |
| 3007 | // Transfer touch focus to the second window |
| 3008 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 3009 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 3010 | firstWindow->consumeMotionCancel(); |
| 3011 | secondWindow->consumeMotionPointerDown(1); |
| 3012 | |
| 3013 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3014 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3015 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3016 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3017 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3018 | // The first window gets nothing and the second gets pointer up |
| 3019 | firstWindow->assertNoEvents(); |
| 3020 | secondWindow->consumeMotionPointerUp(1); |
| 3021 | |
| 3022 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3023 | NotifyMotionArgs upMotionArgs = |
| 3024 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3025 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3026 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3027 | // The first window gets nothing and the second gets up |
| 3028 | firstWindow->assertNoEvents(); |
| 3029 | secondWindow->consumeMotionUp(); |
| 3030 | } |
| 3031 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3032 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 3033 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 3034 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 3035 | // window should get nothing. |
| 3036 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 3037 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3038 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3039 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3040 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3041 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3042 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3043 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3044 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3045 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3046 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3047 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3048 | |
| 3049 | // Add the windows to the dispatcher |
| 3050 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 3051 | |
| 3052 | PointF pointInFirst = {300, 200}; |
| 3053 | PointF pointInSecond = {300, 600}; |
| 3054 | |
| 3055 | // Send down to the first window |
| 3056 | NotifyMotionArgs firstDownMotionArgs = |
| 3057 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3058 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 3059 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 3060 | // Only the first window should get the down event |
| 3061 | firstWindow->consumeMotionDown(); |
| 3062 | secondWindow->assertNoEvents(); |
| 3063 | |
| 3064 | // Send down to the second window |
| 3065 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3066 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3067 | {pointInFirst, pointInSecond}); |
| 3068 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3069 | // The first window gets a move and the second a down |
| 3070 | firstWindow->consumeMotionMove(); |
| 3071 | secondWindow->consumeMotionDown(); |
| 3072 | |
| 3073 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3074 | const bool transferred = |
| 3075 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3076 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 3077 | ASSERT_FALSE(transferred); |
| 3078 | firstWindow->assertNoEvents(); |
| 3079 | secondWindow->assertNoEvents(); |
| 3080 | |
| 3081 | // The rest of the dispatch should proceed as normal |
| 3082 | // Send pointer up to the second window |
| 3083 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3084 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3085 | {pointInFirst, pointInSecond}); |
| 3086 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3087 | // The first window gets MOVE and the second gets pointer up |
| 3088 | firstWindow->consumeMotionMove(); |
| 3089 | secondWindow->consumeMotionUp(); |
| 3090 | |
| 3091 | // Send up event to the first window |
| 3092 | NotifyMotionArgs upMotionArgs = |
| 3093 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3094 | ADISPLAY_ID_DEFAULT); |
| 3095 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3096 | // The first window gets nothing and the second gets up |
| 3097 | firstWindow->consumeMotionUp(); |
| 3098 | secondWindow->assertNoEvents(); |
| 3099 | } |
| 3100 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3101 | // This case will create two windows and one mirrored window on the default display and mirror |
| 3102 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 3103 | // the windows info of second display before default display. |
| 3104 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 3105 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3106 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3107 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3108 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3109 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3110 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3111 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3112 | |
| 3113 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 3114 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 3115 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3116 | |
| 3117 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 3118 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3119 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3120 | |
| 3121 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 3122 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3123 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3124 | |
| 3125 | // Update window info, let it find window handle of second display first. |
| 3126 | mDispatcher->setInputWindows( |
| 3127 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 3128 | {ADISPLAY_ID_DEFAULT, |
| 3129 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 3130 | |
| 3131 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3132 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3133 | {50, 50})) |
| 3134 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3135 | |
| 3136 | // Window should receive motion event. |
| 3137 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3138 | |
| 3139 | // Transfer touch focus |
| 3140 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 3141 | secondWindowInPrimary->getToken())); |
| 3142 | // The first window gets cancel. |
| 3143 | firstWindowInPrimary->consumeMotionCancel(); |
| 3144 | secondWindowInPrimary->consumeMotionDown(); |
| 3145 | |
| 3146 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3147 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3148 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 3149 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3150 | firstWindowInPrimary->assertNoEvents(); |
| 3151 | secondWindowInPrimary->consumeMotionMove(); |
| 3152 | |
| 3153 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3154 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3155 | {150, 50})) |
| 3156 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3157 | firstWindowInPrimary->assertNoEvents(); |
| 3158 | secondWindowInPrimary->consumeMotionUp(); |
| 3159 | } |
| 3160 | |
| 3161 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 3162 | // 'transferTouch' api. |
| 3163 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 3164 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3165 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3166 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3167 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3168 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3169 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3170 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3171 | |
| 3172 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 3173 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 3174 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3175 | |
| 3176 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 3177 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3178 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3179 | |
| 3180 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 3181 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3182 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3183 | |
| 3184 | // Update window info, let it find window handle of second display first. |
| 3185 | mDispatcher->setInputWindows( |
| 3186 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 3187 | {ADISPLAY_ID_DEFAULT, |
| 3188 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 3189 | |
| 3190 | // Touch on second display. |
| 3191 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3192 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) |
| 3193 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3194 | |
| 3195 | // Window should receive motion event. |
| 3196 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 3197 | |
| 3198 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3199 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3200 | |
| 3201 | // The first window gets cancel. |
| 3202 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 3203 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 3204 | |
| 3205 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3206 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3207 | SECOND_DISPLAY_ID, {150, 50})) |
| 3208 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3209 | firstWindowInPrimary->assertNoEvents(); |
| 3210 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 3211 | |
| 3212 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3213 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
| 3214 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3215 | firstWindowInPrimary->assertNoEvents(); |
| 3216 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 3217 | } |
| 3218 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3219 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3220 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3221 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3222 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3223 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3224 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3225 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3226 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3227 | |
| 3228 | window->consumeFocusEvent(true); |
| 3229 | |
| 3230 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3231 | mDispatcher->notifyKey(&keyArgs); |
| 3232 | |
| 3233 | // Window should receive key down event. |
| 3234 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3235 | } |
| 3236 | |
| 3237 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3238 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3239 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3240 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3241 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3242 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3243 | |
| 3244 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3245 | mDispatcher->notifyKey(&keyArgs); |
| 3246 | mDispatcher->waitForIdle(); |
| 3247 | |
| 3248 | window->assertNoEvents(); |
| 3249 | } |
| 3250 | |
| 3251 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 3252 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3253 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3254 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3255 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3256 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3257 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3258 | |
| 3259 | // Send key |
| 3260 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3261 | mDispatcher->notifyKey(&keyArgs); |
| 3262 | // Send motion |
| 3263 | NotifyMotionArgs motionArgs = |
| 3264 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3265 | ADISPLAY_ID_DEFAULT); |
| 3266 | mDispatcher->notifyMotion(&motionArgs); |
| 3267 | |
| 3268 | // Window should receive only the motion event |
| 3269 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3270 | window->assertNoEvents(); // Key event or focus event will not be received |
| 3271 | } |
| 3272 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3273 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 3274 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3275 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3276 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3277 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3278 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3279 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3280 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3281 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3282 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3283 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3284 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3285 | |
| 3286 | // Add the windows to the dispatcher |
| 3287 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 3288 | |
| 3289 | PointF pointInFirst = {300, 200}; |
| 3290 | PointF pointInSecond = {300, 600}; |
| 3291 | |
| 3292 | // Send down to the first window |
| 3293 | NotifyMotionArgs firstDownMotionArgs = |
| 3294 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3295 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 3296 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 3297 | // Only the first window should get the down event |
| 3298 | firstWindow->consumeMotionDown(); |
| 3299 | secondWindow->assertNoEvents(); |
| 3300 | |
| 3301 | // Send down to the second window |
| 3302 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3303 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3304 | {pointInFirst, pointInSecond}); |
| 3305 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3306 | // The first window gets a move and the second a down |
| 3307 | firstWindow->consumeMotionMove(); |
| 3308 | secondWindow->consumeMotionDown(); |
| 3309 | |
| 3310 | // Send pointer cancel to the second window |
| 3311 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3312 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3313 | {pointInFirst, pointInSecond}); |
| 3314 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3315 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3316 | // The first window gets move and the second gets cancel. |
| 3317 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 3318 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 3319 | |
| 3320 | // Send up event. |
| 3321 | NotifyMotionArgs upMotionArgs = |
| 3322 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3323 | ADISPLAY_ID_DEFAULT); |
| 3324 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3325 | // The first window gets up and the second gets nothing. |
| 3326 | firstWindow->consumeMotionUp(); |
| 3327 | secondWindow->assertNoEvents(); |
| 3328 | } |
| 3329 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3330 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 3331 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3332 | |
| 3333 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3334 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3335 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3336 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 3337 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 3338 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 3339 | |
| 3340 | window->sendTimeline(1 /*inputEventId*/, graphicsTimeline); |
| 3341 | window->assertNoEvents(); |
| 3342 | mDispatcher->waitForIdle(); |
| 3343 | } |
| 3344 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3345 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3346 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3347 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3348 | int32_t displayId) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3349 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3350 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3351 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3352 | } |
| 3353 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3354 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 3355 | |
| 3356 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3357 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, |
| 3358 | expectedDisplayId, expectedFlags); |
| 3359 | } |
| 3360 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3361 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 3362 | |
| 3363 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 3364 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3365 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3366 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 3367 | expectedDisplayId, expectedFlags); |
| 3368 | } |
| 3369 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3370 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3371 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 3372 | expectedDisplayId, expectedFlags); |
| 3373 | } |
| 3374 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3375 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3376 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, |
| 3377 | expectedDisplayId, expectedFlags); |
| 3378 | } |
| 3379 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3380 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3381 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 3382 | expectedDisplayId, expectedFlags); |
| 3383 | } |
| 3384 | |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 3385 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 3386 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 3387 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 3388 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT, |
| 3389 | 0 /*expectedFlags*/); |
| 3390 | } |
| 3391 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3392 | MotionEvent* consumeMotion() { |
| 3393 | InputEvent* event = mInputReceiver->consume(); |
| 3394 | if (!event) { |
| 3395 | ADD_FAILURE() << "No event was produced"; |
| 3396 | return nullptr; |
| 3397 | } |
| 3398 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 3399 | ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion"; |
| 3400 | return nullptr; |
| 3401 | } |
| 3402 | return static_cast<MotionEvent*>(event); |
| 3403 | } |
| 3404 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3405 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 3406 | |
| 3407 | private: |
| 3408 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3409 | }; |
| 3410 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3411 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 3412 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3413 | /** |
| 3414 | * Two entities that receive touch: A window, and a global monitor. |
| 3415 | * The touch goes to the window, and then the window disappears. |
| 3416 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 3417 | * for the monitor, as well. |
| 3418 | * 1. foregroundWindow |
| 3419 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 3420 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3421 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3422 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3423 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3424 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3425 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3426 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3427 | |
| 3428 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3429 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3430 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3431 | {100, 200})) |
| 3432 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3433 | |
| 3434 | // Both the foreground window and the global monitor should receive the touch down |
| 3435 | window->consumeMotionDown(); |
| 3436 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3437 | |
| 3438 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3439 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3440 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 3441 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3442 | |
| 3443 | window->consumeMotionMove(); |
| 3444 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 3445 | |
| 3446 | // Now the foreground window goes away |
| 3447 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 3448 | window->consumeMotionCancel(); |
| 3449 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 3450 | |
| 3451 | // If more events come in, there will be no more foreground window to send them to. This will |
| 3452 | // cause a cancel for the monitor, as well. |
| 3453 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 3454 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3455 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 3456 | << "Injection should fail because the window was removed"; |
| 3457 | window->assertNoEvents(); |
| 3458 | // Global monitor now gets the cancel |
| 3459 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 3460 | } |
| 3461 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3462 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3463 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3464 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3465 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3466 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3467 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3468 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3469 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3470 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3471 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3472 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3473 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3474 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3477 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
| 3478 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3479 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3480 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3481 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3482 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3483 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3484 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3485 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3486 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3487 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3488 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3489 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3490 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3491 | // Pilfer pointers from the monitor. |
| 3492 | // This should not do anything and the window should continue to receive events. |
| 3493 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3494 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3495 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3496 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3497 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3498 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3499 | |
| 3500 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 3501 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3504 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3505 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3506 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3507 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3508 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3509 | window->setWindowOffset(20, 40); |
| 3510 | window->setWindowTransform(0, 1, -1, 0); |
| 3511 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3512 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3513 | |
| 3514 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3515 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 3516 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3517 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3518 | MotionEvent* event = monitor.consumeMotion(); |
| 3519 | // Even though window has transform, gesture monitor must not. |
| 3520 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 3521 | } |
| 3522 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3523 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3524 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3525 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3526 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3527 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3528 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3529 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 3530 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3533 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3534 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3535 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3536 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3537 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3538 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3539 | |
| 3540 | NotifyMotionArgs motionArgs = |
| 3541 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3542 | ADISPLAY_ID_DEFAULT); |
| 3543 | |
| 3544 | mDispatcher->notifyMotion(&motionArgs); |
| 3545 | // Window should receive motion down event. |
| 3546 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3547 | |
| 3548 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3549 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3550 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 3551 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 3552 | motionArgs.pointerCoords[0].getX() - 10); |
| 3553 | |
| 3554 | mDispatcher->notifyMotion(&motionArgs); |
| 3555 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
| 3556 | 0 /*expectedFlags*/); |
| 3557 | } |
| 3558 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3559 | /** |
| 3560 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 3561 | * the device default right away. In the test scenario, we check both the default value, |
| 3562 | * and the action of enabling / disabling. |
| 3563 | */ |
| 3564 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3565 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3566 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3567 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 3568 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3569 | |
| 3570 | // Set focused application. |
| 3571 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3572 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3573 | |
| 3574 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3575 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3576 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3577 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 3578 | |
| 3579 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3580 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3581 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3582 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); |
| 3583 | |
| 3584 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 3585 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 3586 | true /*hasPermission*/, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3587 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3588 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3589 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3590 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3591 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); |
| 3592 | |
| 3593 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3594 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3595 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3596 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); |
| 3597 | |
| 3598 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 3599 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 3600 | true /*hasPermission*/, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3601 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3602 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3603 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3604 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3605 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 3606 | |
| 3607 | window->assertNoEvents(); |
| 3608 | } |
| 3609 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3610 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3611 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3612 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3613 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3614 | |
| 3615 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3616 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3617 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3618 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3619 | setFocusedWindow(window); |
| 3620 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3621 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 3622 | |
| 3623 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 3624 | mDispatcher->notifyKey(&keyArgs); |
| 3625 | |
| 3626 | InputEvent* event = window->consume(); |
| 3627 | ASSERT_NE(event, nullptr); |
| 3628 | |
| 3629 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 3630 | ASSERT_NE(verified, nullptr); |
| 3631 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 3632 | |
| 3633 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 3634 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 3635 | ASSERT_EQ(keyArgs.source, verified->source); |
| 3636 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 3637 | |
| 3638 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 3639 | |
| 3640 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3641 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 3642 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3643 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 3644 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 3645 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 3646 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 3647 | } |
| 3648 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3649 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3650 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3651 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3652 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3653 | |
| 3654 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3655 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3656 | ui::Transform transform; |
| 3657 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 3658 | |
| 3659 | gui::DisplayInfo displayInfo; |
| 3660 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 3661 | displayInfo.transform = transform; |
| 3662 | |
| 3663 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3664 | |
| 3665 | NotifyMotionArgs motionArgs = |
| 3666 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3667 | ADISPLAY_ID_DEFAULT); |
| 3668 | mDispatcher->notifyMotion(&motionArgs); |
| 3669 | |
| 3670 | InputEvent* event = window->consume(); |
| 3671 | ASSERT_NE(event, nullptr); |
| 3672 | |
| 3673 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 3674 | ASSERT_NE(verified, nullptr); |
| 3675 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 3676 | |
| 3677 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 3678 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 3679 | EXPECT_EQ(motionArgs.source, verified->source); |
| 3680 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 3681 | |
| 3682 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 3683 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3684 | const vec2 rawXY = |
| 3685 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 3686 | motionArgs.pointerCoords[0].getXYValue()); |
| 3687 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 3688 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3689 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3690 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 3691 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3692 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 3693 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 3694 | } |
| 3695 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3696 | /** |
| 3697 | * Ensure that separate calls to sign the same data are generating the same key. |
| 3698 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 3699 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 3700 | * tests. |
| 3701 | */ |
| 3702 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 3703 | KeyEvent event = getTestKeyEvent(); |
| 3704 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 3705 | |
| 3706 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 3707 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 3708 | ASSERT_EQ(hmac1, hmac2); |
| 3709 | } |
| 3710 | |
| 3711 | /** |
| 3712 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 3713 | */ |
| 3714 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 3715 | KeyEvent event = getTestKeyEvent(); |
| 3716 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 3717 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 3718 | |
| 3719 | verifiedEvent.deviceId += 1; |
| 3720 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3721 | |
| 3722 | verifiedEvent.source += 1; |
| 3723 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3724 | |
| 3725 | verifiedEvent.eventTimeNanos += 1; |
| 3726 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3727 | |
| 3728 | verifiedEvent.displayId += 1; |
| 3729 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3730 | |
| 3731 | verifiedEvent.action += 1; |
| 3732 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3733 | |
| 3734 | verifiedEvent.downTimeNanos += 1; |
| 3735 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3736 | |
| 3737 | verifiedEvent.flags += 1; |
| 3738 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3739 | |
| 3740 | verifiedEvent.keyCode += 1; |
| 3741 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3742 | |
| 3743 | verifiedEvent.scanCode += 1; |
| 3744 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3745 | |
| 3746 | verifiedEvent.metaState += 1; |
| 3747 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3748 | |
| 3749 | verifiedEvent.repeatCount += 1; |
| 3750 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3751 | } |
| 3752 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3753 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 3754 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3755 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3756 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3757 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3758 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3759 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3760 | |
| 3761 | // Top window is also focusable but is not granted focus. |
| 3762 | windowTop->setFocusable(true); |
| 3763 | windowSecond->setFocusable(true); |
| 3764 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 3765 | setFocusedWindow(windowSecond); |
| 3766 | |
| 3767 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3768 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3769 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3770 | |
| 3771 | // Focused window should receive event. |
| 3772 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 3773 | windowTop->assertNoEvents(); |
| 3774 | } |
| 3775 | |
| 3776 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 3777 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3778 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3779 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3780 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3781 | |
| 3782 | window->setFocusable(true); |
| 3783 | // Release channel for window is no longer valid. |
| 3784 | window->releaseChannel(); |
| 3785 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3786 | setFocusedWindow(window); |
| 3787 | |
| 3788 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3789 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3790 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3791 | |
| 3792 | // window channel is invalid, so it should not receive any input event. |
| 3793 | window->assertNoEvents(); |
| 3794 | } |
| 3795 | |
| 3796 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 3797 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3798 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3799 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3800 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3801 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3802 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3803 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3804 | setFocusedWindow(window); |
| 3805 | |
| 3806 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3807 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3808 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3809 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3810 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3811 | window->assertNoEvents(); |
| 3812 | } |
| 3813 | |
| 3814 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 3815 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3816 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3817 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3818 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3819 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3820 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3821 | |
| 3822 | windowTop->setFocusable(true); |
| 3823 | windowSecond->setFocusable(true); |
| 3824 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 3825 | setFocusedWindow(windowTop); |
| 3826 | windowTop->consumeFocusEvent(true); |
| 3827 | |
| 3828 | setFocusedWindow(windowSecond, windowTop); |
| 3829 | windowSecond->consumeFocusEvent(true); |
| 3830 | windowTop->consumeFocusEvent(false); |
| 3831 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3832 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3833 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3834 | |
| 3835 | // Focused window should receive event. |
| 3836 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 3837 | } |
| 3838 | |
| 3839 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { |
| 3840 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3841 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3842 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3843 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3844 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3845 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3846 | |
| 3847 | windowTop->setFocusable(true); |
| 3848 | windowSecond->setFocusable(true); |
| 3849 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 3850 | setFocusedWindow(windowSecond, windowTop); |
| 3851 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3852 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3853 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3854 | |
| 3855 | // Event should be dropped. |
| 3856 | windowTop->assertNoEvents(); |
| 3857 | windowSecond->assertNoEvents(); |
| 3858 | } |
| 3859 | |
| 3860 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 3861 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3862 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3863 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3864 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3865 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 3866 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3867 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3868 | |
| 3869 | window->setFocusable(true); |
| 3870 | previousFocusedWindow->setFocusable(true); |
| 3871 | window->setVisible(false); |
| 3872 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 3873 | setFocusedWindow(previousFocusedWindow); |
| 3874 | previousFocusedWindow->consumeFocusEvent(true); |
| 3875 | |
| 3876 | // Requesting focus on invisible window takes focus from currently focused window. |
| 3877 | setFocusedWindow(window); |
| 3878 | previousFocusedWindow->consumeFocusEvent(false); |
| 3879 | |
| 3880 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3881 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3882 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3883 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3884 | |
| 3885 | // Window does not get focus event or key down. |
| 3886 | window->assertNoEvents(); |
| 3887 | |
| 3888 | // Window becomes visible. |
| 3889 | window->setVisible(true); |
| 3890 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3891 | |
| 3892 | // Window receives focus event. |
| 3893 | window->consumeFocusEvent(true); |
| 3894 | // Focused window receives key down. |
| 3895 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3896 | } |
| 3897 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 3898 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 3899 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3900 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3901 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 3902 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3903 | |
| 3904 | // window is granted focus. |
| 3905 | window->setFocusable(true); |
| 3906 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3907 | setFocusedWindow(window); |
| 3908 | window->consumeFocusEvent(true); |
| 3909 | |
| 3910 | // When a display is removed window loses focus. |
| 3911 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 3912 | window->consumeFocusEvent(false); |
| 3913 | } |
| 3914 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3915 | /** |
| 3916 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 3917 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 3918 | * of the 'slipperyEnterWindow'. |
| 3919 | * |
| 3920 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 3921 | * a way so that the touched location is no longer covered by the top window. |
| 3922 | * |
| 3923 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 3924 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 3925 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 3926 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 3927 | * with ACTION_DOWN). |
| 3928 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 3929 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 3930 | * |
| 3931 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 3932 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 3933 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 3934 | * |
| 3935 | * In this test, we ensure that the event received by the bottom window has |
| 3936 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 3937 | */ |
| 3938 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 3939 | constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1; |
| 3940 | constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3941 | |
| 3942 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3943 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3944 | |
| 3945 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3946 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3947 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3948 | // Make sure this one overlaps the bottom window |
| 3949 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 3950 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 3951 | // one. Windows with the same owner are not considered to be occluding each other. |
| 3952 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 3953 | |
| 3954 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3955 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3956 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3957 | |
| 3958 | mDispatcher->setInputWindows( |
| 3959 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 3960 | |
| 3961 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
| 3962 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3963 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 3964 | mDispatcher->notifyMotion(&args); |
| 3965 | slipperyExitWindow->consumeMotionDown(); |
| 3966 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 3967 | mDispatcher->setInputWindows( |
| 3968 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 3969 | |
| 3970 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3971 | ADISPLAY_ID_DEFAULT, {{51, 51}}); |
| 3972 | mDispatcher->notifyMotion(&args); |
| 3973 | |
| 3974 | slipperyExitWindow->consumeMotionCancel(); |
| 3975 | |
| 3976 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 3977 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 3978 | } |
| 3979 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3980 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 3981 | protected: |
| 3982 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 3983 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 3984 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3985 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3986 | sp<FakeWindowHandle> mWindow; |
| 3987 | |
| 3988 | virtual void SetUp() override { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3989 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3990 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3991 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3992 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 3993 | ASSERT_EQ(OK, mDispatcher->start()); |
| 3994 | |
| 3995 | setUpWindow(); |
| 3996 | } |
| 3997 | |
| 3998 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3999 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4000 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4001 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4002 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4003 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4004 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4005 | mWindow->consumeFocusEvent(true); |
| 4006 | } |
| 4007 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4008 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4009 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4010 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4011 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
| 4012 | mDispatcher->notifyKey(&keyArgs); |
| 4013 | |
| 4014 | // Window should receive key down event. |
| 4015 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4016 | } |
| 4017 | |
| 4018 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 4019 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 4020 | InputEvent* repeatEvent = mWindow->consume(); |
| 4021 | ASSERT_NE(nullptr, repeatEvent); |
| 4022 | |
| 4023 | uint32_t eventType = repeatEvent->getType(); |
| 4024 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); |
| 4025 | |
| 4026 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 4027 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 4028 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 4029 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 4030 | } |
| 4031 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4032 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4033 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4034 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4035 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
| 4036 | mDispatcher->notifyKey(&keyArgs); |
| 4037 | |
| 4038 | // Window should receive key down event. |
| 4039 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 4040 | 0 /*expectedFlags*/); |
| 4041 | } |
| 4042 | }; |
| 4043 | |
| 4044 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4045 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 4046 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4047 | expectKeyRepeatOnce(repeatCount); |
| 4048 | } |
| 4049 | } |
| 4050 | |
| 4051 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
| 4052 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 4053 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4054 | expectKeyRepeatOnce(repeatCount); |
| 4055 | } |
| 4056 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 4057 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4058 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4059 | expectKeyRepeatOnce(repeatCount); |
| 4060 | } |
| 4061 | } |
| 4062 | |
| 4063 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4064 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4065 | expectKeyRepeatOnce(1 /*repeatCount*/); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4066 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 4067 | mWindow->assertNoEvents(); |
| 4068 | } |
| 4069 | |
| 4070 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
| 4071 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 4072 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4073 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 4074 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4075 | // Stale key up from device 1. |
| 4076 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 4077 | // Device 2 is still down, keep repeating |
| 4078 | expectKeyRepeatOnce(2 /*repeatCount*/); |
| 4079 | expectKeyRepeatOnce(3 /*repeatCount*/); |
| 4080 | // Device 2 key up |
| 4081 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 4082 | mWindow->assertNoEvents(); |
| 4083 | } |
| 4084 | |
| 4085 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
| 4086 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 4087 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4088 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 4089 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4090 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. |
| 4091 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 4092 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4093 | mWindow->assertNoEvents(); |
| 4094 | } |
| 4095 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 4096 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 4097 | sendAndConsumeKeyDown(DEVICE_ID); |
| 4098 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4099 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
| 4100 | mDispatcher->notifyDeviceReset(&args); |
| 4101 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 4102 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 4103 | mWindow->assertNoEvents(); |
| 4104 | } |
| 4105 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4106 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4107 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4108 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4109 | InputEvent* repeatEvent = mWindow->consume(); |
| 4110 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 4111 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 4112 | IdGenerator::getSource(repeatEvent->getId())); |
| 4113 | } |
| 4114 | } |
| 4115 | |
| 4116 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4117 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4118 | |
| 4119 | std::unordered_set<int32_t> idSet; |
| 4120 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4121 | InputEvent* repeatEvent = mWindow->consume(); |
| 4122 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 4123 | int32_t id = repeatEvent->getId(); |
| 4124 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 4125 | idSet.insert(id); |
| 4126 | } |
| 4127 | } |
| 4128 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4129 | /* Test InputDispatcher for MultiDisplay */ |
| 4130 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 4131 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4132 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4133 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4134 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4135 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4136 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4137 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4138 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4139 | // Set focus window for primary display, but focused display would be second one. |
| 4140 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4141 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4142 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4143 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4144 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4145 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4146 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4147 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4148 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4149 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4150 | // Set focus display to second one. |
| 4151 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 4152 | // Set focus window for second display. |
| 4153 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4154 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4155 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4156 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4157 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4158 | } |
| 4159 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4160 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4161 | InputDispatcherTest::TearDown(); |
| 4162 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4163 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4164 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4165 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4166 | windowInSecondary.clear(); |
| 4167 | } |
| 4168 | |
| 4169 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4170 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4171 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4172 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4173 | sp<FakeWindowHandle> windowInSecondary; |
| 4174 | }; |
| 4175 | |
| 4176 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 4177 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4178 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4179 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4180 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4181 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4182 | windowInSecondary->assertNoEvents(); |
| 4183 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4184 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4185 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4186 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4187 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4188 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4189 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4190 | } |
| 4191 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4192 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4193 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4194 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4195 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4196 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4197 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4198 | windowInSecondary->assertNoEvents(); |
| 4199 | |
| 4200 | // Test inject a key down without display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4201 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4202 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4203 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4204 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4205 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4206 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4207 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4208 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4209 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4210 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
| 4211 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4212 | |
| 4213 | // Test inject a key down, should timeout because of no target window. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4214 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4215 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4216 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4217 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4218 | windowInSecondary->assertNoEvents(); |
| 4219 | } |
| 4220 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4221 | // Test per-display input monitors for motion event. |
| 4222 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4223 | FakeMonitorReceiver monitorInPrimary = |
| 4224 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4225 | FakeMonitorReceiver monitorInSecondary = |
| 4226 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4227 | |
| 4228 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4229 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4230 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4231 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4232 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4233 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4234 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4235 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4236 | |
| 4237 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4238 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4239 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4240 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4241 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4242 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4243 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4244 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4245 | |
| 4246 | // Test inject a non-pointer motion event. |
| 4247 | // If specific a display, it will dispatch to the focused window of particular display, |
| 4248 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4249 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4250 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 4251 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4252 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4253 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4254 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4255 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4256 | } |
| 4257 | |
| 4258 | // Test per-display input monitors for key event. |
| 4259 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4260 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4261 | FakeMonitorReceiver monitorInPrimary = |
| 4262 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4263 | FakeMonitorReceiver monitorInSecondary = |
| 4264 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4265 | |
| 4266 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4267 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4268 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4269 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4270 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4271 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4272 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4273 | } |
| 4274 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4275 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 4276 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4277 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4278 | secondWindowInPrimary->setFocusable(true); |
| 4279 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 4280 | setFocusedWindow(secondWindowInPrimary); |
| 4281 | windowInPrimary->consumeFocusEvent(false); |
| 4282 | secondWindowInPrimary->consumeFocusEvent(true); |
| 4283 | |
| 4284 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4285 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 4286 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4287 | windowInPrimary->assertNoEvents(); |
| 4288 | windowInSecondary->assertNoEvents(); |
| 4289 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4290 | } |
| 4291 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 4292 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 4293 | FakeMonitorReceiver monitorInPrimary = |
| 4294 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4295 | FakeMonitorReceiver monitorInSecondary = |
| 4296 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 4297 | |
| 4298 | // Test touch down on primary display. |
| 4299 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4300 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4301 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4302 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4303 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4304 | |
| 4305 | // Test touch down on second display. |
| 4306 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4307 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4308 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4309 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4310 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 4311 | |
| 4312 | // Trigger cancel touch. |
| 4313 | mDispatcher->cancelCurrentTouch(); |
| 4314 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4315 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4316 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4317 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4318 | |
| 4319 | // Test inject a move motion event, no window/monitor should receive the event. |
| 4320 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4321 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4322 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 4323 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 4324 | windowInPrimary->assertNoEvents(); |
| 4325 | monitorInPrimary.assertNoEvents(); |
| 4326 | |
| 4327 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4328 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4329 | SECOND_DISPLAY_ID, {110, 200})) |
| 4330 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 4331 | windowInSecondary->assertNoEvents(); |
| 4332 | monitorInSecondary.assertNoEvents(); |
| 4333 | } |
| 4334 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4335 | class InputFilterTest : public InputDispatcherTest { |
| 4336 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4337 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 4338 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4339 | NotifyMotionArgs motionArgs; |
| 4340 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4341 | motionArgs = |
| 4342 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4343 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4344 | motionArgs = |
| 4345 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4346 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4347 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4348 | if (expectToBeFiltered) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4349 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); |
| 4350 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4351 | } else { |
| 4352 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 4353 | } |
| 4354 | } |
| 4355 | |
| 4356 | void testNotifyKey(bool expectToBeFiltered) { |
| 4357 | NotifyKeyArgs keyArgs; |
| 4358 | |
| 4359 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 4360 | mDispatcher->notifyKey(&keyArgs); |
| 4361 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 4362 | mDispatcher->notifyKey(&keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4363 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4364 | |
| 4365 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 4366 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4367 | } else { |
| 4368 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 4369 | } |
| 4370 | } |
| 4371 | }; |
| 4372 | |
| 4373 | // Test InputFilter for MotionEvent |
| 4374 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 4375 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 4376 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 4377 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 4378 | |
| 4379 | // Enable InputFilter |
| 4380 | mDispatcher->setInputFilterEnabled(true); |
| 4381 | // Test touch on both primary and second display, and check if both events are filtered. |
| 4382 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 4383 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 4384 | |
| 4385 | // Disable InputFilter |
| 4386 | mDispatcher->setInputFilterEnabled(false); |
| 4387 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 4388 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 4389 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 4390 | } |
| 4391 | |
| 4392 | // Test InputFilter for KeyEvent |
| 4393 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 4394 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 4395 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 4396 | |
| 4397 | // Enable InputFilter |
| 4398 | mDispatcher->setInputFilterEnabled(true); |
| 4399 | // Send a key event, and check if it is filtered. |
| 4400 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 4401 | |
| 4402 | // Disable InputFilter |
| 4403 | mDispatcher->setInputFilterEnabled(false); |
| 4404 | // Send a key event, and check if it isn't filtered. |
| 4405 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 4406 | } |
| 4407 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4408 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 4409 | // logical display coordinate space. |
| 4410 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 4411 | ui::Transform firstDisplayTransform; |
| 4412 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 4413 | ui::Transform secondDisplayTransform; |
| 4414 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 4415 | |
| 4416 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 4417 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 4418 | displayInfos[0].transform = firstDisplayTransform; |
| 4419 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 4420 | displayInfos[1].transform = secondDisplayTransform; |
| 4421 | |
| 4422 | mDispatcher->onWindowInfosChanged({}, displayInfos); |
| 4423 | |
| 4424 | // Enable InputFilter |
| 4425 | mDispatcher->setInputFilterEnabled(true); |
| 4426 | |
| 4427 | // Ensure the correct transforms are used for the displays. |
| 4428 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); |
| 4429 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); |
| 4430 | } |
| 4431 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4432 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 4433 | protected: |
| 4434 | virtual void SetUp() override { |
| 4435 | InputDispatcherTest::SetUp(); |
| 4436 | |
| 4437 | /** |
| 4438 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 4439 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 4440 | * on. |
| 4441 | */ |
| 4442 | mDispatcher->setInputFilterEnabled(true); |
| 4443 | |
| 4444 | std::shared_ptr<InputApplicationHandle> application = |
| 4445 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4446 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 4447 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4448 | |
| 4449 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4450 | mWindow->setFocusable(true); |
| 4451 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4452 | setFocusedWindow(mWindow); |
| 4453 | mWindow->consumeFocusEvent(true); |
| 4454 | } |
| 4455 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4456 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 4457 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4458 | KeyEvent event; |
| 4459 | |
| 4460 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4461 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 4462 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
| 4463 | KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime); |
| 4464 | const int32_t additionalPolicyFlags = |
| 4465 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 4466 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4467 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4468 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 4469 | policyFlags | additionalPolicyFlags)); |
| 4470 | |
| 4471 | InputEvent* received = mWindow->consume(); |
| 4472 | ASSERT_NE(nullptr, received); |
| 4473 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4474 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY); |
| 4475 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); |
| 4476 | ASSERT_EQ(flags, keyEvent.getFlags()); |
| 4477 | } |
| 4478 | |
| 4479 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 4480 | int32_t flags) { |
| 4481 | MotionEvent event; |
| 4482 | PointerProperties pointerProperties[1]; |
| 4483 | PointerCoords pointerCoords[1]; |
| 4484 | pointerProperties[0].clear(); |
| 4485 | pointerProperties[0].id = 0; |
| 4486 | pointerCoords[0].clear(); |
| 4487 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 4488 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 4489 | |
| 4490 | ui::Transform identityTransform; |
| 4491 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4492 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 4493 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 4494 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 4495 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4496 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 4497 | eventTime, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4498 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 4499 | |
| 4500 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 4501 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4502 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4503 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 4504 | policyFlags | additionalPolicyFlags)); |
| 4505 | |
| 4506 | InputEvent* received = mWindow->consume(); |
| 4507 | ASSERT_NE(nullptr, received); |
| 4508 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
| 4509 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 4510 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); |
| 4511 | ASSERT_EQ(flags, motionEvent.getFlags()); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | private: |
| 4515 | sp<FakeWindowHandle> mWindow; |
| 4516 | }; |
| 4517 | |
| 4518 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4519 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 4520 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 4521 | // injected device id will be overwritten. |
| 4522 | testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 4523 | 0 /*flags*/); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4526 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4527 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4528 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 4529 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 4530 | } |
| 4531 | |
| 4532 | TEST_F(InputFilterInjectionPolicyTest, |
| 4533 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 4534 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
| 4535 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 4536 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4537 | } |
| 4538 | |
| 4539 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
| 4540 | testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4541 | VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4542 | } |
| 4543 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4544 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4545 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4546 | InputDispatcherTest::SetUp(); |
| 4547 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4548 | std::shared_ptr<FakeApplicationHandle> application = |
| 4549 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4550 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4551 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4552 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4553 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4554 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4555 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4556 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4557 | |
| 4558 | // Set focused application. |
| 4559 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4560 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4561 | |
| 4562 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4563 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4564 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4565 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4566 | } |
| 4567 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4568 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4569 | InputDispatcherTest::TearDown(); |
| 4570 | |
| 4571 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4572 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4573 | } |
| 4574 | |
| 4575 | protected: |
| 4576 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4577 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4578 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4579 | }; |
| 4580 | |
| 4581 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 4582 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 4583 | // the onPointerDownOutsideFocus callback. |
| 4584 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4585 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4586 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4587 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4588 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4589 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4590 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4591 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4592 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 4593 | } |
| 4594 | |
| 4595 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 4596 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 4597 | // onPointerDownOutsideFocus callback. |
| 4598 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4599 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4600 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4601 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4602 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4603 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4604 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4605 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4606 | } |
| 4607 | |
| 4608 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 4609 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 4610 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4611 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4612 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4613 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4614 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4615 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4616 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4617 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4618 | } |
| 4619 | |
| 4620 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 4621 | // DOWN on the window that already has focus. Ensure no window received the |
| 4622 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4623 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4624 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4625 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4626 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4627 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4628 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4629 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4630 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4631 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4632 | } |
| 4633 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 4634 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 4635 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 4636 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 4637 | const MotionEvent event = |
| 4638 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4639 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 4640 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20)) |
| 4641 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 4642 | .build(); |
| 4643 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) |
| 4644 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4645 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 4646 | |
| 4647 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4648 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 4649 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 4650 | mUnfocusedWindow->assertNoEvents(); |
| 4651 | } |
| 4652 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4653 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 4654 | // windows that point to the same client token. |
| 4655 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 4656 | virtual void SetUp() override { |
| 4657 | InputDispatcherTest::SetUp(); |
| 4658 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4659 | std::shared_ptr<FakeApplicationHandle> application = |
| 4660 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4661 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 4662 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4663 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 4664 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4665 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", |
| 4666 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4667 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 4668 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4669 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4670 | } |
| 4671 | |
| 4672 | protected: |
| 4673 | sp<FakeWindowHandle> mWindow1; |
| 4674 | sp<FakeWindowHandle> mWindow2; |
| 4675 | |
| 4676 | // 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] | 4677 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4678 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 4679 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 4683 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4684 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4685 | InputEvent* event = window->consume(); |
| 4686 | |
| 4687 | ASSERT_NE(nullptr, event) << name.c_str() |
| 4688 | << ": consumer should have returned non-NULL event."; |
| 4689 | |
| 4690 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 4691 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 4692 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 4693 | |
| 4694 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4695 | assertMotionAction(expectedAction, motionEvent.getAction()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4696 | |
| 4697 | for (size_t i = 0; i < points.size(); i++) { |
| 4698 | float expectedX = points[i].x; |
| 4699 | float expectedY = points[i].y; |
| 4700 | |
| 4701 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 4702 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 4703 | << ", got " << motionEvent.getX(i); |
| 4704 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 4705 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 4706 | << ", got " << motionEvent.getY(i); |
| 4707 | } |
| 4708 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4709 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 4710 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4711 | std::vector<PointF> expectedPoints) { |
| 4712 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 4713 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 4714 | mDispatcher->notifyMotion(&motionArgs); |
| 4715 | |
| 4716 | // Always consume from window1 since it's the window that has the InputReceiver |
| 4717 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 4718 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4719 | }; |
| 4720 | |
| 4721 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 4722 | // Touch Window 1 |
| 4723 | PointF touchedPoint = {10, 10}; |
| 4724 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4725 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4726 | |
| 4727 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4728 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4729 | |
| 4730 | // Touch Window 2 |
| 4731 | touchedPoint = {150, 150}; |
| 4732 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4733 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4734 | } |
| 4735 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4736 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 4737 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4738 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4739 | |
| 4740 | // Touch Window 1 |
| 4741 | PointF touchedPoint = {10, 10}; |
| 4742 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4743 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4744 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4745 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4746 | |
| 4747 | // Touch Window 2 |
| 4748 | touchedPoint = {150, 150}; |
| 4749 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4750 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 4751 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4752 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4753 | // Update the transform so rotation is set |
| 4754 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4755 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 4756 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4757 | } |
| 4758 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4759 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4760 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4761 | |
| 4762 | // Touch Window 1 |
| 4763 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4764 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4765 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4766 | |
| 4767 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4768 | touchedPoints.push_back(PointF{150, 150}); |
| 4769 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4770 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4771 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4772 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4773 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4774 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4775 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4776 | // Update the transform so rotation is set for Window 2 |
| 4777 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4778 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4779 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4782 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4783 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4784 | |
| 4785 | // Touch Window 1 |
| 4786 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4787 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4788 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4789 | |
| 4790 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4791 | touchedPoints.push_back(PointF{150, 150}); |
| 4792 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4793 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4794 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4795 | |
| 4796 | // Move both windows |
| 4797 | touchedPoints = {{20, 20}, {175, 175}}; |
| 4798 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 4799 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 4800 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4801 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4802 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4803 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4804 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4805 | expectedPoints.pop_back(); |
| 4806 | |
| 4807 | // Touch Window 2 |
| 4808 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4809 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4810 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4811 | |
| 4812 | // Move both windows |
| 4813 | touchedPoints = {{20, 20}, {175, 175}}; |
| 4814 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 4815 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 4816 | |
| 4817 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4818 | } |
| 4819 | |
| 4820 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 4821 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 4822 | |
| 4823 | // Touch Window 1 |
| 4824 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4825 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4826 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4827 | |
| 4828 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4829 | touchedPoints.push_back(PointF{150, 150}); |
| 4830 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4831 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4832 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4833 | |
| 4834 | // Move both windows |
| 4835 | touchedPoints = {{20, 20}, {175, 175}}; |
| 4836 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 4837 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 4838 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4839 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4840 | } |
| 4841 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4842 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 4843 | virtual void SetUp() override { |
| 4844 | InputDispatcherTest::SetUp(); |
| 4845 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4846 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4847 | mApplication->setDispatchingTimeout(20ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4848 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 4849 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4850 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 4851 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4852 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4853 | |
| 4854 | // Set focused application. |
| 4855 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 4856 | |
| 4857 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4858 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4859 | mWindow->consumeFocusEvent(true); |
| 4860 | } |
| 4861 | |
| 4862 | virtual void TearDown() override { |
| 4863 | InputDispatcherTest::TearDown(); |
| 4864 | mWindow.clear(); |
| 4865 | } |
| 4866 | |
| 4867 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4868 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4869 | sp<FakeWindowHandle> mWindow; |
| 4870 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 4871 | |
| 4872 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4873 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4874 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4875 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4876 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4877 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4878 | WINDOW_LOCATION)); |
| 4879 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4880 | |
| 4881 | sp<FakeWindowHandle> addSpyWindow() { |
| 4882 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4883 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4884 | spy->setTrustedOverlay(true); |
| 4885 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4886 | spy->setSpy(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4887 | spy->setDispatchingTimeout(30ms); |
| 4888 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); |
| 4889 | return spy; |
| 4890 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4891 | }; |
| 4892 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4893 | // Send a tap and respond, which should not cause an ANR. |
| 4894 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 4895 | tapOnWindow(); |
| 4896 | mWindow->consumeMotionDown(); |
| 4897 | mWindow->consumeMotionUp(); |
| 4898 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4899 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4900 | } |
| 4901 | |
| 4902 | // Send a regular key and respond, which should not cause an ANR. |
| 4903 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4904 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4905 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 4906 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4907 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4908 | } |
| 4909 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4910 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 4911 | mWindow->setFocusable(false); |
| 4912 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4913 | mWindow->consumeFocusEvent(false); |
| 4914 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4915 | InputEventInjectionResult result = |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4916 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4917 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, |
| 4918 | false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4919 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4920 | // Key will not go to window because we have no focused window. |
| 4921 | // The 'no focused window' ANR timer should start instead. |
| 4922 | |
| 4923 | // Now, the focused application goes away. |
| 4924 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 4925 | // The key should get dropped and there should be no ANR. |
| 4926 | |
| 4927 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4928 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4929 | } |
| 4930 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4931 | // 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] | 4932 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 4933 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4934 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4935 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4936 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4937 | WINDOW_LOCATION)); |
| 4938 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4939 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 4940 | ASSERT_TRUE(sequenceNum); |
| 4941 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4942 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4943 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4944 | mWindow->finishEvent(*sequenceNum); |
| 4945 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 4946 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4947 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4948 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4949 | } |
| 4950 | |
| 4951 | // Send a key to the app and have the app not respond right away. |
| 4952 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 4953 | // Inject a key, and don't respond - expect that ANR is called. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4954 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4955 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 4956 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4957 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4958 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4959 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4960 | } |
| 4961 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4962 | // We have a focused application, but no focused window |
| 4963 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4964 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4965 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4966 | mWindow->consumeFocusEvent(false); |
| 4967 | |
| 4968 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4969 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4970 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4971 | WINDOW_LOCATION)); |
| 4972 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 4973 | mDispatcher->waitForIdle(); |
| 4974 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4975 | |
| 4976 | // Once a focused event arrives, we get an ANR for this application |
| 4977 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 4978 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4979 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4980 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4981 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4982 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4983 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 4984 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4985 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4986 | } |
| 4987 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 4988 | /** |
| 4989 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 4990 | * there will not be an ANR. |
| 4991 | */ |
| 4992 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 4993 | mWindow->setFocusable(false); |
| 4994 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4995 | mWindow->consumeFocusEvent(false); |
| 4996 | |
| 4997 | KeyEvent event; |
| 4998 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 4999 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 5000 | |
| 5001 | // Define a valid key down event that is stale (too old). |
| 5002 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 5003 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, |
| 5004 | AMETA_NONE, 1 /*repeatCount*/, eventTime, eventTime); |
| 5005 | |
| 5006 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 5007 | |
| 5008 | InputEventInjectionResult result = |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 5009 | mDispatcher->injectInputEvent(&event, {} /* targetUid */, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 5010 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 5011 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 5012 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 5013 | << "Injection should fail because the event is stale"; |
| 5014 | |
| 5015 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5016 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5017 | mWindow->assertNoEvents(); |
| 5018 | } |
| 5019 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5020 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5021 | // Make sure that we don't notify policy twice about the same ANR. |
| 5022 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5023 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5024 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5025 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5026 | |
| 5027 | // Once a focused event arrives, we get an ANR for this application |
| 5028 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 5029 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5030 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5031 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5032 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5033 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5034 | const std::chrono::duration appTimeout = |
| 5035 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 5036 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5037 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5038 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5039 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 5040 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5041 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5042 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 5043 | // '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] | 5044 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5045 | } |
| 5046 | |
| 5047 | // We have a focused application, but no focused window |
| 5048 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5049 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5050 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5051 | mWindow->consumeFocusEvent(false); |
| 5052 | |
| 5053 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5054 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5055 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5056 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 5057 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5058 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5059 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 5060 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5061 | |
| 5062 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5063 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5064 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5065 | mWindow->assertNoEvents(); |
| 5066 | } |
| 5067 | |
| 5068 | /** |
| 5069 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 5070 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 5071 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 5072 | * the ANR mechanism should still work. |
| 5073 | * |
| 5074 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 5075 | * DOWN event, while not responding on the second one. |
| 5076 | */ |
| 5077 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 5078 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5079 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5080 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 5081 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 5082 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5083 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5084 | |
| 5085 | // Now send ACTION_UP, with identical timestamp |
| 5086 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5087 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 5088 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 5089 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5090 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5091 | |
| 5092 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 5093 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5094 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5095 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5096 | } |
| 5097 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5098 | // A spy window can receive an ANR |
| 5099 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 5100 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 5101 | |
| 5102 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5103 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5104 | WINDOW_LOCATION)); |
| 5105 | mWindow->consumeMotionDown(); |
| 5106 | |
| 5107 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 5108 | ASSERT_TRUE(sequenceNum); |
| 5109 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5110 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5111 | |
| 5112 | spy->finishEvent(*sequenceNum); |
| 5113 | spy->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, |
| 5114 | 0 /*flags*/); |
| 5115 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5116 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5117 | } |
| 5118 | |
| 5119 | // 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] | 5120 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5121 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 5122 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5123 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5124 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5125 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5126 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5127 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5128 | |
| 5129 | // Stuck on the ACTION_UP |
| 5130 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5131 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5132 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5133 | // 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] | 5134 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5135 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5136 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5137 | |
| 5138 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 5139 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5140 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5141 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5142 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5143 | } |
| 5144 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5145 | // 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] | 5146 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5147 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 5148 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5149 | |
| 5150 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5151 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5152 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5153 | |
| 5154 | mWindow->consumeMotionDown(); |
| 5155 | // Stuck on the ACTION_UP |
| 5156 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5157 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5158 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5159 | // 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] | 5160 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5161 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5162 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5163 | |
| 5164 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 5165 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5166 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5167 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5168 | spy->assertNoEvents(); |
| 5169 | } |
| 5170 | |
| 5171 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
| 5172 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); |
| 5173 | |
| 5174 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5175 | |
| 5176 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5177 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5178 | WINDOW_LOCATION)); |
| 5179 | |
| 5180 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5181 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 5182 | ASSERT_TRUE(consumeSeq); |
| 5183 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5184 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5185 | |
| 5186 | monitor.finishEvent(*consumeSeq); |
| 5187 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5188 | |
| 5189 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5190 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5191 | } |
| 5192 | |
| 5193 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 5194 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 5195 | // get an ANR again. |
| 5196 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 5197 | // 2. consume all pending events (= queue becomes healthy again) |
| 5198 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 5199 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 5200 | tapOnWindow(); |
| 5201 | |
| 5202 | mWindow->consumeMotionDown(); |
| 5203 | // Block on ACTION_UP |
| 5204 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5205 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5206 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 5207 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5208 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5209 | mWindow->assertNoEvents(); |
| 5210 | |
| 5211 | tapOnWindow(); |
| 5212 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5213 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5214 | mWindow->consumeMotionUp(); |
| 5215 | |
| 5216 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5217 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5218 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5219 | mWindow->assertNoEvents(); |
| 5220 | } |
| 5221 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5222 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 5223 | // it. |
| 5224 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5225 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5226 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5227 | WINDOW_LOCATION)); |
| 5228 | |
| 5229 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5230 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5231 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5232 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 5233 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5234 | // 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] | 5235 | mWindow->consumeMotionDown(); |
| 5236 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 5237 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 5238 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5239 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5240 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5241 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5242 | } |
| 5243 | |
| 5244 | /** |
| 5245 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 5246 | * not to to the focused window until the motion is processed. |
| 5247 | * |
| 5248 | * Warning!!! |
| 5249 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 5250 | * and the injection timeout that we specify when injecting the key. |
| 5251 | * We must have the injection timeout (10ms) be smaller than |
| 5252 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 5253 | * |
| 5254 | * If that value changes, this test should also change. |
| 5255 | */ |
| 5256 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 5257 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 5258 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5259 | |
| 5260 | tapOnWindow(); |
| 5261 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 5262 | ASSERT_TRUE(downSequenceNum); |
| 5263 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 5264 | ASSERT_TRUE(upSequenceNum); |
| 5265 | // Don't finish the events yet, and send a key |
| 5266 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 5267 | // window even if motions are still being processed. But because the injection timeout is short, |
| 5268 | // we will receive INJECTION_TIMED_OUT as the result. |
| 5269 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5270 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5271 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5272 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 5273 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5274 | // Key will not be sent to the window, yet, because the window is still processing events |
| 5275 | // and the key remains pending, waiting for the touch events to be processed |
| 5276 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 5277 | ASSERT_FALSE(keySequenceNum); |
| 5278 | |
| 5279 | std::this_thread::sleep_for(500ms); |
| 5280 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 5281 | // to the focused window, even though we have not yet finished the motion event |
| 5282 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5283 | mWindow->finishEvent(*downSequenceNum); |
| 5284 | mWindow->finishEvent(*upSequenceNum); |
| 5285 | } |
| 5286 | |
| 5287 | /** |
| 5288 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 5289 | * not go to the focused window until the motion is processed. |
| 5290 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 5291 | * focused window right away. |
| 5292 | */ |
| 5293 | TEST_F(InputDispatcherSingleWindowAnr, |
| 5294 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 5295 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 5296 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5297 | |
| 5298 | tapOnWindow(); |
| 5299 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 5300 | ASSERT_TRUE(downSequenceNum); |
| 5301 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 5302 | ASSERT_TRUE(upSequenceNum); |
| 5303 | // Don't finish the events yet, and send a key |
| 5304 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5305 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5306 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5307 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5308 | // At this point, key is still pending, and should not be sent to the application yet. |
| 5309 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 5310 | ASSERT_FALSE(keySequenceNum); |
| 5311 | |
| 5312 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 5313 | tapOnWindow(); |
| 5314 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 5315 | // the other events yet. We can finish events in any order. |
| 5316 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 5317 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 5318 | mWindow->consumeMotionDown(); |
| 5319 | mWindow->consumeMotionUp(); |
| 5320 | mWindow->assertNoEvents(); |
| 5321 | } |
| 5322 | |
| 5323 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 5324 | virtual void SetUp() override { |
| 5325 | InputDispatcherTest::SetUp(); |
| 5326 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5327 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5328 | mApplication->setDispatchingTimeout(10ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5329 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 5330 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5331 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5332 | // 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] | 5333 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5334 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5335 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 5336 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 5337 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5338 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5339 | |
| 5340 | // Set focused application. |
| 5341 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5342 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5343 | |
| 5344 | // Expect one focus window exist in display. |
| 5345 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5346 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5347 | mFocusedWindow->consumeFocusEvent(true); |
| 5348 | } |
| 5349 | |
| 5350 | virtual void TearDown() override { |
| 5351 | InputDispatcherTest::TearDown(); |
| 5352 | |
| 5353 | mUnfocusedWindow.clear(); |
| 5354 | mFocusedWindow.clear(); |
| 5355 | } |
| 5356 | |
| 5357 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5358 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5359 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 5360 | sp<FakeWindowHandle> mFocusedWindow; |
| 5361 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 5362 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 5363 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 5364 | |
| 5365 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 5366 | |
| 5367 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 5368 | |
| 5369 | private: |
| 5370 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5371 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5372 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5373 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5374 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5375 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5376 | location)); |
| 5377 | } |
| 5378 | }; |
| 5379 | |
| 5380 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 5381 | // should be ANR'd first. |
| 5382 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5383 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5384 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5385 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5386 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5387 | mFocusedWindow->consumeMotionDown(); |
| 5388 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 5389 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 5390 | // We consumed all events, so no ANR |
| 5391 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5392 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5393 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5394 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5395 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5396 | FOCUSED_WINDOW_LOCATION)); |
| 5397 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 5398 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5399 | |
| 5400 | const std::chrono::duration timeout = |
| 5401 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5402 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5403 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 5404 | // sequence to make it consistent |
| 5405 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5406 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5407 | mFocusedWindow->consumeMotionDown(); |
| 5408 | // This cancel is generated because the connection was unresponsive |
| 5409 | mFocusedWindow->consumeMotionCancel(); |
| 5410 | mFocusedWindow->assertNoEvents(); |
| 5411 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5412 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5413 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 5414 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5415 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5416 | } |
| 5417 | |
| 5418 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 5419 | // it doesn't matter which order they should have ANR. |
| 5420 | // But we should receive ANR for both. |
| 5421 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 5422 | // Set the timeout for unfocused window to match the focused window |
| 5423 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 5424 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 5425 | |
| 5426 | tapOnFocusedWindow(); |
| 5427 | // 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] | 5428 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; |
| 5429 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); |
| 5430 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5431 | |
| 5432 | // 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] | 5433 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 5434 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 5435 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 5436 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5437 | |
| 5438 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5439 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5440 | |
| 5441 | mFocusedWindow->consumeMotionDown(); |
| 5442 | mFocusedWindow->consumeMotionUp(); |
| 5443 | mUnfocusedWindow->consumeMotionOutside(); |
| 5444 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5445 | sp<IBinder> responsiveToken1, responsiveToken2; |
| 5446 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); |
| 5447 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5448 | |
| 5449 | // Both applications should be marked as responsive, in any order |
| 5450 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 5451 | mFocusedWindow->getToken() == responsiveToken2); |
| 5452 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 5453 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 5454 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5455 | } |
| 5456 | |
| 5457 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 5458 | // We should also log an error to account for the dropped event (not tested here). |
| 5459 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 5460 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 5461 | tapOnFocusedWindow(); |
| 5462 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 5463 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 5464 | // Receive the events, but don't respond |
| 5465 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 5466 | ASSERT_TRUE(downEventSequenceNum); |
| 5467 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 5468 | ASSERT_TRUE(upEventSequenceNum); |
| 5469 | const std::chrono::duration timeout = |
| 5470 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5471 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5472 | |
| 5473 | // Tap once again |
| 5474 | // 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] | 5475 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5476 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5477 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5478 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5479 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5480 | FOCUSED_WINDOW_LOCATION)); |
| 5481 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 5482 | // valid touch target |
| 5483 | mUnfocusedWindow->assertNoEvents(); |
| 5484 | |
| 5485 | // Consume the first tap |
| 5486 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 5487 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 5488 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5489 | // The second tap did not go to the focused window |
| 5490 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5491 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5492 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 5493 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5494 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5495 | } |
| 5496 | |
| 5497 | // If you tap outside of all windows, there will not be ANR |
| 5498 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5499 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5500 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5501 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 5502 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5503 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5504 | } |
| 5505 | |
| 5506 | // Since the focused window is paused, tapping on it should not produce any events |
| 5507 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 5508 | mFocusedWindow->setPaused(true); |
| 5509 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 5510 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5511 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5512 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5513 | FOCUSED_WINDOW_LOCATION)); |
| 5514 | |
| 5515 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 5516 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5517 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 5518 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5519 | |
| 5520 | mFocusedWindow->assertNoEvents(); |
| 5521 | mUnfocusedWindow->assertNoEvents(); |
| 5522 | } |
| 5523 | |
| 5524 | /** |
| 5525 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 5526 | * not to to the focused window until the motion is processed. |
| 5527 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 5528 | * |
| 5529 | * Warning!!! |
| 5530 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 5531 | * and the injection timeout that we specify when injecting the key. |
| 5532 | * We must have the injection timeout (10ms) be smaller than |
| 5533 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 5534 | * |
| 5535 | * If that value changes, this test should also change. |
| 5536 | */ |
| 5537 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 5538 | // Set a long ANR timeout to prevent it from triggering |
| 5539 | mFocusedWindow->setDispatchingTimeout(2s); |
| 5540 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 5541 | |
| 5542 | tapOnUnfocusedWindow(); |
| 5543 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 5544 | ASSERT_TRUE(downSequenceNum); |
| 5545 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 5546 | ASSERT_TRUE(upSequenceNum); |
| 5547 | // Don't finish the events yet, and send a key |
| 5548 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 5549 | // window even if motions are still being processed. |
| 5550 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5551 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5552 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5553 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 5554 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5555 | // Key will not be sent to the window, yet, because the window is still processing events |
| 5556 | // and the key remains pending, waiting for the touch events to be processed |
| 5557 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 5558 | ASSERT_FALSE(keySequenceNum); |
| 5559 | |
| 5560 | // 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] | 5561 | mFocusedWindow->setFocusable(false); |
| 5562 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5563 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5564 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5565 | |
| 5566 | // Focus events should precede the key events |
| 5567 | mUnfocusedWindow->consumeFocusEvent(true); |
| 5568 | mFocusedWindow->consumeFocusEvent(false); |
| 5569 | |
| 5570 | // Finish the tap events, which should unblock dispatcher |
| 5571 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 5572 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 5573 | |
| 5574 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 5575 | // can finally go to the newly focused "mUnfocusedWindow". |
| 5576 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5577 | mFocusedWindow->assertNoEvents(); |
| 5578 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5579 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5580 | } |
| 5581 | |
| 5582 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 5583 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 5584 | // The other window should not be affected by that. |
| 5585 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 5586 | // Touch Window 1 |
| 5587 | NotifyMotionArgs motionArgs = |
| 5588 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5589 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); |
| 5590 | mDispatcher->notifyMotion(&motionArgs); |
| 5591 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 5592 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 5593 | |
| 5594 | // Touch Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5595 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5596 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5597 | mDispatcher->notifyMotion(&motionArgs); |
| 5598 | |
| 5599 | const std::chrono::duration timeout = |
| 5600 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5601 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5602 | |
| 5603 | mUnfocusedWindow->consumeMotionDown(); |
| 5604 | mFocusedWindow->consumeMotionDown(); |
| 5605 | // Focused window may or may not receive ACTION_MOVE |
| 5606 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 5607 | InputEvent* event; |
| 5608 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 5609 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 5610 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 5611 | ASSERT_NE(nullptr, event); |
| 5612 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 5613 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 5614 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 5615 | mFocusedWindow->consumeMotionCancel(); |
| 5616 | } else { |
| 5617 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 5618 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5619 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5620 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 5621 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5622 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5623 | mUnfocusedWindow->assertNoEvents(); |
| 5624 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5625 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5626 | } |
| 5627 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5628 | /** |
| 5629 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 5630 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 5631 | * |
| 5632 | * If the user touches another application during this time, the key should be dropped. |
| 5633 | * Next, if a new focused window comes in, without toggling the focused application, |
| 5634 | * then no ANR should occur. |
| 5635 | * |
| 5636 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 5637 | * but in some cases the policy may not update the focused application. |
| 5638 | */ |
| 5639 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 5640 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 5641 | std::make_shared<FakeApplicationHandle>(); |
| 5642 | focusedApplication->setDispatchingTimeout(60ms); |
| 5643 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 5644 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 5645 | mFocusedWindow->setFocusable(false); |
| 5646 | |
| 5647 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 5648 | mFocusedWindow->consumeFocusEvent(false); |
| 5649 | |
| 5650 | // Send a key. The ANR timer should start because there is no focused window. |
| 5651 | // 'focusedApplication' will get blamed if this timer completes. |
| 5652 | // 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] | 5653 | InputEventInjectionResult result = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5654 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5655 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, |
| 5656 | false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5657 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5658 | |
| 5659 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 5660 | // then the injected touches won't cause the focused event to get dropped. |
| 5661 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 5662 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 5663 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 5664 | // For this test, it means that the key would get delivered to the window once it becomes |
| 5665 | // focused. |
| 5666 | std::this_thread::sleep_for(10ms); |
| 5667 | |
| 5668 | // Touch unfocused window. This should force the pending key to get dropped. |
| 5669 | NotifyMotionArgs motionArgs = |
| 5670 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5671 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); |
| 5672 | mDispatcher->notifyMotion(&motionArgs); |
| 5673 | |
| 5674 | // We do not consume the motion right away, because that would require dispatcher to first |
| 5675 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 5676 | // Set the focused window first. |
| 5677 | mFocusedWindow->setFocusable(true); |
| 5678 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 5679 | setFocusedWindow(mFocusedWindow); |
| 5680 | mFocusedWindow->consumeFocusEvent(true); |
| 5681 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 5682 | // to another application. This could be a bug / behaviour in the policy. |
| 5683 | |
| 5684 | mUnfocusedWindow->consumeMotionDown(); |
| 5685 | |
| 5686 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5687 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 5688 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 5689 | } |
| 5690 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5691 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 5692 | // that has feature NO_INPUT_CHANNEL. |
| 5693 | // Layout: |
| 5694 | // Top (closest to user) |
| 5695 | // mNoInputWindow (above all windows) |
| 5696 | // mBottomWindow |
| 5697 | // Bottom (furthest from user) |
| 5698 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 5699 | virtual void SetUp() override { |
| 5700 | InputDispatcherTest::SetUp(); |
| 5701 | |
| 5702 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5703 | mNoInputWindow = |
| 5704 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 5705 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
| 5706 | std::make_optional<sp<IBinder>>(nullptr) /*token*/); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5707 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5708 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5709 | // It's perfectly valid for this window to not have an associated input channel |
| 5710 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5711 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 5712 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5713 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5714 | |
| 5715 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 5716 | } |
| 5717 | |
| 5718 | protected: |
| 5719 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 5720 | sp<FakeWindowHandle> mNoInputWindow; |
| 5721 | sp<FakeWindowHandle> mBottomWindow; |
| 5722 | }; |
| 5723 | |
| 5724 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 5725 | PointF touchedPoint = {10, 10}; |
| 5726 | |
| 5727 | NotifyMotionArgs motionArgs = |
| 5728 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5729 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 5730 | mDispatcher->notifyMotion(&motionArgs); |
| 5731 | |
| 5732 | mNoInputWindow->assertNoEvents(); |
| 5733 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 5734 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 5735 | // and therefore should prevent mBottomWindow from receiving touches |
| 5736 | mBottomWindow->assertNoEvents(); |
| 5737 | } |
| 5738 | |
| 5739 | /** |
| 5740 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 5741 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 5742 | */ |
| 5743 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 5744 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5745 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 5746 | "Window with input channel and NO_INPUT_CHANNEL", |
| 5747 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5748 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5749 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5750 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5751 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 5752 | |
| 5753 | PointF touchedPoint = {10, 10}; |
| 5754 | |
| 5755 | NotifyMotionArgs motionArgs = |
| 5756 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5757 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 5758 | mDispatcher->notifyMotion(&motionArgs); |
| 5759 | |
| 5760 | mNoInputWindow->assertNoEvents(); |
| 5761 | mBottomWindow->assertNoEvents(); |
| 5762 | } |
| 5763 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5764 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 5765 | protected: |
| 5766 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 5767 | sp<FakeWindowHandle> mWindow; |
| 5768 | sp<FakeWindowHandle> mMirror; |
| 5769 | |
| 5770 | virtual void SetUp() override { |
| 5771 | InputDispatcherTest::SetUp(); |
| 5772 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5773 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 5774 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 5775 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5776 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 5777 | mWindow->setFocusable(true); |
| 5778 | mMirror->setFocusable(true); |
| 5779 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5780 | } |
| 5781 | }; |
| 5782 | |
| 5783 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 5784 | // Request focus on a mirrored window |
| 5785 | setFocusedWindow(mMirror); |
| 5786 | |
| 5787 | // window gets focused |
| 5788 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5789 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5790 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5791 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5792 | } |
| 5793 | |
| 5794 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 5795 | // focusable. |
| 5796 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 5797 | setFocusedWindow(mMirror); |
| 5798 | |
| 5799 | // window gets focused |
| 5800 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5801 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5802 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5803 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5804 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5805 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5806 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5807 | |
| 5808 | mMirror->setFocusable(false); |
| 5809 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5810 | |
| 5811 | // window loses focus since one of the windows associated with the token in not focusable |
| 5812 | mWindow->consumeFocusEvent(false); |
| 5813 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5814 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5815 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5816 | mWindow->assertNoEvents(); |
| 5817 | } |
| 5818 | |
| 5819 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 5820 | // invisible. |
| 5821 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 5822 | setFocusedWindow(mMirror); |
| 5823 | |
| 5824 | // window gets focused |
| 5825 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5826 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5827 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5828 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5829 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5830 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5831 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5832 | |
| 5833 | mMirror->setVisible(false); |
| 5834 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5835 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5836 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5837 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5838 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5839 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5840 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5841 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5842 | |
| 5843 | mWindow->setVisible(false); |
| 5844 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5845 | |
| 5846 | // window loses focus only after all windows associated with the token become invisible. |
| 5847 | mWindow->consumeFocusEvent(false); |
| 5848 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5849 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5850 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5851 | mWindow->assertNoEvents(); |
| 5852 | } |
| 5853 | |
| 5854 | // A focused & mirrored window remains focused until both windows are removed. |
| 5855 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 5856 | setFocusedWindow(mMirror); |
| 5857 | |
| 5858 | // window gets focused |
| 5859 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5860 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5861 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5862 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5863 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5864 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5865 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5866 | |
| 5867 | // single window is removed but the window token remains focused |
| 5868 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 5869 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5870 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5871 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5872 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5873 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5874 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5875 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5876 | |
| 5877 | // Both windows are removed |
| 5878 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 5879 | mWindow->consumeFocusEvent(false); |
| 5880 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5881 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5882 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5883 | mWindow->assertNoEvents(); |
| 5884 | } |
| 5885 | |
| 5886 | // Focus request can be pending until one window becomes visible. |
| 5887 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 5888 | // Request focus on an invisible mirror. |
| 5889 | mWindow->setVisible(false); |
| 5890 | mMirror->setVisible(false); |
| 5891 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5892 | setFocusedWindow(mMirror); |
| 5893 | |
| 5894 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5895 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5896 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5897 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5898 | |
| 5899 | mMirror->setVisible(true); |
| 5900 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5901 | |
| 5902 | // window gets focused |
| 5903 | mWindow->consumeFocusEvent(true); |
| 5904 | // window gets the pending key event |
| 5905 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5906 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5907 | |
| 5908 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 5909 | protected: |
| 5910 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 5911 | sp<FakeWindowHandle> mWindow; |
| 5912 | sp<FakeWindowHandle> mSecondWindow; |
| 5913 | |
| 5914 | void SetUp() override { |
| 5915 | InputDispatcherTest::SetUp(); |
| 5916 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5917 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5918 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5919 | mSecondWindow = |
| 5920 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5921 | mSecondWindow->setFocusable(true); |
| 5922 | |
| 5923 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 5924 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 5925 | |
| 5926 | setFocusedWindow(mWindow); |
| 5927 | mWindow->consumeFocusEvent(true); |
| 5928 | } |
| 5929 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5930 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
| 5931 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5932 | mDispatcher->notifyPointerCaptureChanged(&args); |
| 5933 | } |
| 5934 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5935 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 5936 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5937 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5938 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 5939 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5940 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5941 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5942 | } |
| 5943 | }; |
| 5944 | |
| 5945 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 5946 | // Ensure that capture cannot be obtained for unfocused windows. |
| 5947 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 5948 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 5949 | mSecondWindow->assertNoEvents(); |
| 5950 | |
| 5951 | // Ensure that capture can be enabled from the focus window. |
| 5952 | requestAndVerifyPointerCapture(mWindow, true); |
| 5953 | |
| 5954 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 5955 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 5956 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 5957 | |
| 5958 | // Ensure that capture can be disabled from the window with capture. |
| 5959 | requestAndVerifyPointerCapture(mWindow, false); |
| 5960 | } |
| 5961 | |
| 5962 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5963 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5964 | |
| 5965 | setFocusedWindow(mSecondWindow); |
| 5966 | |
| 5967 | // Ensure that the capture disabled event was sent first. |
| 5968 | mWindow->consumeCaptureEvent(false); |
| 5969 | mWindow->consumeFocusEvent(false); |
| 5970 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5971 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5972 | |
| 5973 | // 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] | 5974 | notifyPointerCaptureChanged({}); |
| 5975 | notifyPointerCaptureChanged(request); |
| 5976 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5977 | mWindow->assertNoEvents(); |
| 5978 | mSecondWindow->assertNoEvents(); |
| 5979 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 5980 | } |
| 5981 | |
| 5982 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5983 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5984 | |
| 5985 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5986 | notifyPointerCaptureChanged({}); |
| 5987 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5988 | |
| 5989 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5990 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5991 | mWindow->consumeCaptureEvent(false); |
| 5992 | mWindow->assertNoEvents(); |
| 5993 | } |
| 5994 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 5995 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 5996 | requestAndVerifyPointerCapture(mWindow, true); |
| 5997 | |
| 5998 | // The first window loses focus. |
| 5999 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6000 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6001 | mWindow->consumeCaptureEvent(false); |
| 6002 | |
| 6003 | // Request Pointer Capture from the second window before the notification from InputReader |
| 6004 | // arrives. |
| 6005 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6006 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6007 | |
| 6008 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6009 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6010 | |
| 6011 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6012 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6013 | |
| 6014 | mSecondWindow->consumeFocusEvent(true); |
| 6015 | mSecondWindow->consumeCaptureEvent(true); |
| 6016 | } |
| 6017 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6018 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 6019 | // App repeatedly enables and disables capture. |
| 6020 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6021 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6022 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 6023 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 6024 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6025 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6026 | |
| 6027 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 6028 | // first request is now stale, this should do nothing. |
| 6029 | notifyPointerCaptureChanged(firstRequest); |
| 6030 | mWindow->assertNoEvents(); |
| 6031 | |
| 6032 | // InputReader notifies that the second request was enabled. |
| 6033 | notifyPointerCaptureChanged(secondRequest); |
| 6034 | mWindow->consumeCaptureEvent(true); |
| 6035 | } |
| 6036 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 6037 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 6038 | requestAndVerifyPointerCapture(mWindow, true); |
| 6039 | |
| 6040 | // App toggles pointer capture off and on. |
| 6041 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 6042 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 6043 | |
| 6044 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6045 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6046 | |
| 6047 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 6048 | // preceding "disable" request. |
| 6049 | notifyPointerCaptureChanged(enableRequest); |
| 6050 | |
| 6051 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 6052 | // any notifications. |
| 6053 | mWindow->assertNoEvents(); |
| 6054 | } |
| 6055 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6056 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 6057 | protected: |
| 6058 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6059 | |
| 6060 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 6061 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 6062 | |
| 6063 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 6064 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 6065 | |
| 6066 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 6067 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 6068 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 6069 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 6070 | MAXIMUM_OBSCURING_OPACITY); |
| 6071 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6072 | static const int32_t TOUCHED_APP_UID = 10001; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6073 | static const int32_t APP_B_UID = 10002; |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6074 | static const int32_t APP_C_UID = 10003; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6075 | |
| 6076 | sp<FakeWindowHandle> mTouchWindow; |
| 6077 | |
| 6078 | virtual void SetUp() override { |
| 6079 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6080 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6081 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 6082 | } |
| 6083 | |
| 6084 | virtual void TearDown() override { |
| 6085 | InputDispatcherTest::TearDown(); |
| 6086 | mTouchWindow.clear(); |
| 6087 | } |
| 6088 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6089 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, |
| 6090 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6091 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6092 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6093 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6094 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6095 | return window; |
| 6096 | } |
| 6097 | |
| 6098 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { |
| 6099 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 6100 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6101 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6102 | // Generate an arbitrary PID based on the UID |
| 6103 | window->setOwnerInfo(1777 + (uid % 10000), uid); |
| 6104 | return window; |
| 6105 | } |
| 6106 | |
| 6107 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
| 6108 | NotifyMotionArgs args = |
| 6109 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6110 | ADISPLAY_ID_DEFAULT, points); |
| 6111 | mDispatcher->notifyMotion(&args); |
| 6112 | } |
| 6113 | }; |
| 6114 | |
| 6115 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6116 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6117 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6118 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6119 | |
| 6120 | touch(); |
| 6121 | |
| 6122 | mTouchWindow->assertNoEvents(); |
| 6123 | } |
| 6124 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6125 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6126 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 6127 | const sp<FakeWindowHandle>& w = |
| 6128 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 6129 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6130 | |
| 6131 | touch(); |
| 6132 | |
| 6133 | mTouchWindow->assertNoEvents(); |
| 6134 | } |
| 6135 | |
| 6136 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6137 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 6138 | const sp<FakeWindowHandle>& w = |
| 6139 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6140 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6141 | |
| 6142 | touch(); |
| 6143 | |
| 6144 | w->assertNoEvents(); |
| 6145 | } |
| 6146 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6147 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6148 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 6149 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6150 | |
| 6151 | touch(); |
| 6152 | |
| 6153 | mTouchWindow->consumeAnyMotionDown(); |
| 6154 | } |
| 6155 | |
| 6156 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6157 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6158 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6159 | w->setFrame(Rect(0, 0, 50, 50)); |
| 6160 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6161 | |
| 6162 | touch({PointF{100, 100}}); |
| 6163 | |
| 6164 | mTouchWindow->consumeAnyMotionDown(); |
| 6165 | } |
| 6166 | |
| 6167 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6168 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6169 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6170 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6171 | |
| 6172 | touch(); |
| 6173 | |
| 6174 | mTouchWindow->consumeAnyMotionDown(); |
| 6175 | } |
| 6176 | |
| 6177 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 6178 | const sp<FakeWindowHandle>& w = |
| 6179 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 6180 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6181 | |
| 6182 | touch(); |
| 6183 | |
| 6184 | mTouchWindow->consumeAnyMotionDown(); |
| 6185 | } |
| 6186 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6187 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 6188 | const sp<FakeWindowHandle>& w = |
| 6189 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 6190 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6191 | |
| 6192 | touch(); |
| 6193 | |
| 6194 | w->assertNoEvents(); |
| 6195 | } |
| 6196 | |
| 6197 | /** |
| 6198 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 6199 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 6200 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 6201 | */ |
| 6202 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6203 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 6204 | const sp<FakeWindowHandle>& w = |
| 6205 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6206 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6207 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6208 | |
| 6209 | touch(); |
| 6210 | |
| 6211 | w->consumeMotionOutside(); |
| 6212 | } |
| 6213 | |
| 6214 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 6215 | const sp<FakeWindowHandle>& w = |
| 6216 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6217 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6218 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6219 | |
| 6220 | touch(); |
| 6221 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6222 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6223 | } |
| 6224 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6225 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6226 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6227 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6228 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6229 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6230 | |
| 6231 | touch(); |
| 6232 | |
| 6233 | mTouchWindow->consumeAnyMotionDown(); |
| 6234 | } |
| 6235 | |
| 6236 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 6237 | const sp<FakeWindowHandle>& w = |
| 6238 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6239 | MAXIMUM_OBSCURING_OPACITY); |
| 6240 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6241 | |
| 6242 | touch(); |
| 6243 | |
| 6244 | mTouchWindow->consumeAnyMotionDown(); |
| 6245 | } |
| 6246 | |
| 6247 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6248 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6249 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6250 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6251 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6252 | |
| 6253 | touch(); |
| 6254 | |
| 6255 | mTouchWindow->assertNoEvents(); |
| 6256 | } |
| 6257 | |
| 6258 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 6259 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 6260 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6261 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 6262 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6263 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6264 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 6265 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6266 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6267 | |
| 6268 | touch(); |
| 6269 | |
| 6270 | mTouchWindow->assertNoEvents(); |
| 6271 | } |
| 6272 | |
| 6273 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 6274 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 6275 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6276 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 6277 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6278 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6279 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 6280 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6281 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6282 | |
| 6283 | touch(); |
| 6284 | |
| 6285 | mTouchWindow->consumeAnyMotionDown(); |
| 6286 | } |
| 6287 | |
| 6288 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6289 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 6290 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6291 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6292 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6293 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6294 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6295 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6296 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 6297 | |
| 6298 | touch(); |
| 6299 | |
| 6300 | mTouchWindow->consumeAnyMotionDown(); |
| 6301 | } |
| 6302 | |
| 6303 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 6304 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6305 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6306 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6307 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6308 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6309 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6310 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6311 | |
| 6312 | touch(); |
| 6313 | |
| 6314 | mTouchWindow->assertNoEvents(); |
| 6315 | } |
| 6316 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6317 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6318 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 6319 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6320 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6321 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6322 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6323 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6324 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6325 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 6326 | |
| 6327 | touch(); |
| 6328 | |
| 6329 | mTouchWindow->assertNoEvents(); |
| 6330 | } |
| 6331 | |
| 6332 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6333 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 6334 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6335 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6336 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6337 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6338 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6339 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6340 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 6341 | |
| 6342 | touch(); |
| 6343 | |
| 6344 | mTouchWindow->consumeAnyMotionDown(); |
| 6345 | } |
| 6346 | |
| 6347 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 6348 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6349 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6350 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6351 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6352 | |
| 6353 | touch(); |
| 6354 | |
| 6355 | mTouchWindow->consumeAnyMotionDown(); |
| 6356 | } |
| 6357 | |
| 6358 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 6359 | const sp<FakeWindowHandle>& w = |
| 6360 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6361 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6362 | |
| 6363 | touch(); |
| 6364 | |
| 6365 | mTouchWindow->consumeAnyMotionDown(); |
| 6366 | } |
| 6367 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 6368 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6369 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 6370 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 6371 | const sp<FakeWindowHandle>& w = |
| 6372 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 6373 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6374 | |
| 6375 | touch(); |
| 6376 | |
| 6377 | mTouchWindow->assertNoEvents(); |
| 6378 | } |
| 6379 | |
| 6380 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 6381 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 6382 | const sp<FakeWindowHandle>& w = |
| 6383 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 6384 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6385 | |
| 6386 | touch(); |
| 6387 | |
| 6388 | mTouchWindow->consumeAnyMotionDown(); |
| 6389 | } |
| 6390 | |
| 6391 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6392 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 6393 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 6394 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6395 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6396 | OPACITY_ABOVE_THRESHOLD); |
| 6397 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6398 | |
| 6399 | touch(); |
| 6400 | |
| 6401 | mTouchWindow->consumeAnyMotionDown(); |
| 6402 | } |
| 6403 | |
| 6404 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6405 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 6406 | const sp<FakeWindowHandle>& w1 = |
| 6407 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 6408 | OPACITY_BELOW_THRESHOLD); |
| 6409 | const sp<FakeWindowHandle>& w2 = |
| 6410 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6411 | OPACITY_BELOW_THRESHOLD); |
| 6412 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6413 | |
| 6414 | touch(); |
| 6415 | |
| 6416 | mTouchWindow->assertNoEvents(); |
| 6417 | } |
| 6418 | |
| 6419 | /** |
| 6420 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 6421 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 6422 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 6423 | */ |
| 6424 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6425 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 6426 | const sp<FakeWindowHandle>& wB = |
| 6427 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 6428 | OPACITY_BELOW_THRESHOLD); |
| 6429 | const sp<FakeWindowHandle>& wC = |
| 6430 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6431 | OPACITY_BELOW_THRESHOLD); |
| 6432 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 6433 | |
| 6434 | touch(); |
| 6435 | |
| 6436 | mTouchWindow->assertNoEvents(); |
| 6437 | } |
| 6438 | |
| 6439 | /** |
| 6440 | * This test is testing that a window from a different UID but with same application token doesn't |
| 6441 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 6442 | */ |
| 6443 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6444 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 6445 | const sp<FakeWindowHandle>& w = |
| 6446 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6447 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 6448 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6449 | |
| 6450 | touch(); |
| 6451 | |
| 6452 | mTouchWindow->consumeAnyMotionDown(); |
| 6453 | } |
| 6454 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6455 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 6456 | protected: |
| 6457 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6458 | sp<FakeWindowHandle> mWindow; |
| 6459 | sp<FakeWindowHandle> mSecondWindow; |
| 6460 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6461 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6462 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 6463 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6464 | |
| 6465 | void SetUp() override { |
| 6466 | InputDispatcherTest::SetUp(); |
| 6467 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6468 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6469 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6470 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6471 | mSecondWindow = |
| 6472 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6473 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6474 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6475 | mSpyWindow = |
| 6476 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6477 | mSpyWindow->setSpy(true); |
| 6478 | mSpyWindow->setTrustedOverlay(true); |
| 6479 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 6480 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6481 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6482 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6483 | } |
| 6484 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6485 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 6486 | switch (fromSource) { |
| 6487 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 6488 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6489 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6490 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6491 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6492 | break; |
| 6493 | case AINPUT_SOURCE_STYLUS: |
| 6494 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6495 | injectMotionEvent( |
| 6496 | mDispatcher, |
| 6497 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 6498 | AINPUT_SOURCE_STYLUS) |
| 6499 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 6500 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6501 | .x(50) |
| 6502 | .y(50)) |
| 6503 | .build())); |
| 6504 | break; |
| 6505 | case AINPUT_SOURCE_MOUSE: |
| 6506 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6507 | injectMotionEvent( |
| 6508 | mDispatcher, |
| 6509 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 6510 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 6511 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 6512 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 6513 | .x(50) |
| 6514 | .y(50)) |
| 6515 | .build())); |
| 6516 | break; |
| 6517 | default: |
| 6518 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 6519 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6520 | |
| 6521 | // Window should receive motion event. |
| 6522 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6523 | // Spy window should also receive motion event |
| 6524 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6525 | } |
| 6526 | |
| 6527 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 6528 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 6529 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6530 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6531 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6532 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6533 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6534 | |
| 6535 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6536 | mDragWindow = |
| 6537 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6538 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6539 | mDispatcher->setInputWindows( |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6540 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6541 | |
| 6542 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6543 | bool transferred = |
| 6544 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
| 6545 | true /* isDragDrop */); |
| 6546 | if (transferred) { |
| 6547 | mWindow->consumeMotionCancel(); |
| 6548 | mDragWindow->consumeMotionDown(); |
| 6549 | } |
| 6550 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6551 | } |
| 6552 | }; |
| 6553 | |
| 6554 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6555 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6556 | |
| 6557 | // Move on window. |
| 6558 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6559 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6560 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6561 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6562 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6563 | mWindow->consumeDragEvent(false, 50, 50); |
| 6564 | mSecondWindow->assertNoEvents(); |
| 6565 | |
| 6566 | // Move to another window. |
| 6567 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6568 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6569 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 6570 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6571 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6572 | mWindow->consumeDragEvent(true, 150, 50); |
| 6573 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 6574 | |
| 6575 | // Move back to original window. |
| 6576 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6577 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6578 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6579 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6580 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6581 | mWindow->consumeDragEvent(false, 50, 50); |
| 6582 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 6583 | |
| 6584 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6585 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6586 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6587 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6588 | mWindow->assertNoEvents(); |
| 6589 | mSecondWindow->assertNoEvents(); |
| 6590 | } |
| 6591 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6592 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6593 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6594 | |
| 6595 | // No cancel event after drag start |
| 6596 | mSpyWindow->assertNoEvents(); |
| 6597 | |
| 6598 | const MotionEvent secondFingerDownEvent = |
| 6599 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6600 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6601 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6602 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(60).y(60)) |
| 6603 | .build(); |
| 6604 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6605 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6606 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6607 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6608 | |
| 6609 | // Receives cancel for first pointer after next pointer down |
| 6610 | mSpyWindow->consumeMotionCancel(); |
| 6611 | mSpyWindow->consumeMotionDown(); |
| 6612 | |
| 6613 | mSpyWindow->assertNoEvents(); |
| 6614 | } |
| 6615 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 6616 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6617 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 6618 | |
| 6619 | // Move on window. |
| 6620 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6621 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6622 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6623 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6624 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6625 | mWindow->consumeDragEvent(false, 50, 50); |
| 6626 | mSecondWindow->assertNoEvents(); |
| 6627 | |
| 6628 | // Move to another window. |
| 6629 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6630 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6631 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 6632 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6633 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6634 | mWindow->consumeDragEvent(true, 150, 50); |
| 6635 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 6636 | |
| 6637 | // drop to another window. |
| 6638 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6639 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6640 | {150, 50})) |
| 6641 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6642 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6643 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 6644 | mWindow->assertNoEvents(); |
| 6645 | mSecondWindow->assertNoEvents(); |
| 6646 | } |
| 6647 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 6648 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6649 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 6650 | |
| 6651 | // Move on window and keep button pressed. |
| 6652 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6653 | injectMotionEvent(mDispatcher, |
| 6654 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 6655 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 6656 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6657 | .x(50) |
| 6658 | .y(50)) |
| 6659 | .build())) |
| 6660 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6661 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6662 | mWindow->consumeDragEvent(false, 50, 50); |
| 6663 | mSecondWindow->assertNoEvents(); |
| 6664 | |
| 6665 | // Move to another window and release button, expect to drop item. |
| 6666 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6667 | injectMotionEvent(mDispatcher, |
| 6668 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 6669 | .buttonState(0) |
| 6670 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6671 | .x(150) |
| 6672 | .y(50)) |
| 6673 | .build())) |
| 6674 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6675 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6676 | mWindow->assertNoEvents(); |
| 6677 | mSecondWindow->assertNoEvents(); |
| 6678 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 6679 | |
| 6680 | // nothing to the window. |
| 6681 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6682 | injectMotionEvent(mDispatcher, |
| 6683 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 6684 | .buttonState(0) |
| 6685 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6686 | .x(150) |
| 6687 | .y(50)) |
| 6688 | .build())) |
| 6689 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6690 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6691 | mWindow->assertNoEvents(); |
| 6692 | mSecondWindow->assertNoEvents(); |
| 6693 | } |
| 6694 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6695 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6696 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 6697 | |
| 6698 | // Set second window invisible. |
| 6699 | mSecondWindow->setVisible(false); |
| 6700 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 6701 | |
| 6702 | // Move on window. |
| 6703 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6704 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6705 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6706 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6707 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6708 | mWindow->consumeDragEvent(false, 50, 50); |
| 6709 | mSecondWindow->assertNoEvents(); |
| 6710 | |
| 6711 | // Move to another window. |
| 6712 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6713 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6714 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 6715 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6716 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6717 | mWindow->consumeDragEvent(true, 150, 50); |
| 6718 | mSecondWindow->assertNoEvents(); |
| 6719 | |
| 6720 | // drop to another window. |
| 6721 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6722 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6723 | {150, 50})) |
| 6724 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6725 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6726 | mFakePolicy->assertDropTargetEquals(nullptr); |
| 6727 | mWindow->assertNoEvents(); |
| 6728 | mSecondWindow->assertNoEvents(); |
| 6729 | } |
| 6730 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6731 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6732 | // Ensure window could track pointerIds if it didn't support split touch. |
| 6733 | mWindow->setPreventSplitting(true); |
| 6734 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6735 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6736 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6737 | {50, 50})) |
| 6738 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6739 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6740 | |
| 6741 | const MotionEvent secondFingerDownEvent = |
| 6742 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6743 | .displayId(ADISPLAY_ID_DEFAULT) |
| 6744 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6745 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6746 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(75).y(50)) |
| 6747 | .build(); |
| 6748 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6749 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6750 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6751 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6752 | mWindow->consumeMotionPointerDown(1 /* pointerIndex */); |
| 6753 | |
| 6754 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6755 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6756 | } |
| 6757 | |
| 6758 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 6759 | // First down on second window. |
| 6760 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6761 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6762 | {150, 50})) |
| 6763 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6764 | |
| 6765 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6766 | |
| 6767 | // Second down on first window. |
| 6768 | const MotionEvent secondFingerDownEvent = |
| 6769 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6770 | .displayId(ADISPLAY_ID_DEFAULT) |
| 6771 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6772 | .pointer( |
| 6773 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 6774 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6775 | .build(); |
| 6776 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6777 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6778 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6779 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6780 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6781 | |
| 6782 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6783 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6784 | |
| 6785 | // Move on window. |
| 6786 | const MotionEvent secondFingerMoveEvent = |
| 6787 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6788 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6789 | .pointer( |
| 6790 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 6791 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6792 | .build(); |
| 6793 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6794 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 6795 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 6796 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6797 | mWindow->consumeDragEvent(false, 50, 50); |
| 6798 | mSecondWindow->consumeMotionMove(); |
| 6799 | |
| 6800 | // Release the drag pointer should perform drop. |
| 6801 | const MotionEvent secondFingerUpEvent = |
| 6802 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6803 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6804 | .pointer( |
| 6805 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 6806 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6807 | .build(); |
| 6808 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6809 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 6810 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 6811 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6812 | mFakePolicy->assertDropTargetEquals(mWindow->getToken()); |
| 6813 | mWindow->assertNoEvents(); |
| 6814 | mSecondWindow->consumeMotionMove(); |
| 6815 | } |
| 6816 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 6817 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6818 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 6819 | |
| 6820 | // Update window of second display. |
| 6821 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6822 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 6823 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 6824 | |
| 6825 | // Let second display has a touch state. |
| 6826 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6827 | injectMotionEvent(mDispatcher, |
| 6828 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 6829 | AINPUT_SOURCE_TOUCHSCREEN) |
| 6830 | .displayId(SECOND_DISPLAY_ID) |
| 6831 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 6832 | .x(100) |
| 6833 | .y(100)) |
| 6834 | .build())); |
| 6835 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 6836 | SECOND_DISPLAY_ID, 0 /* expectedFlag */); |
| 6837 | // Update window again. |
| 6838 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 6839 | |
| 6840 | // Move on window. |
| 6841 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6842 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6843 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6844 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6845 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6846 | mWindow->consumeDragEvent(false, 50, 50); |
| 6847 | mSecondWindow->assertNoEvents(); |
| 6848 | |
| 6849 | // Move to another window. |
| 6850 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6851 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6852 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 6853 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6854 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6855 | mWindow->consumeDragEvent(true, 150, 50); |
| 6856 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 6857 | |
| 6858 | // drop to another window. |
| 6859 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6860 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6861 | {150, 50})) |
| 6862 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6863 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6864 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 6865 | mWindow->assertNoEvents(); |
| 6866 | mSecondWindow->assertNoEvents(); |
| 6867 | } |
| 6868 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6869 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 6870 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 6871 | // Move on window. |
| 6872 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6873 | injectMotionEvent(mDispatcher, |
| 6874 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 6875 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 6876 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 6877 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 6878 | .x(50) |
| 6879 | .y(50)) |
| 6880 | .build())) |
| 6881 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6882 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6883 | mWindow->consumeDragEvent(false, 50, 50); |
| 6884 | mSecondWindow->assertNoEvents(); |
| 6885 | |
| 6886 | // Move to another window. |
| 6887 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6888 | injectMotionEvent(mDispatcher, |
| 6889 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 6890 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 6891 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 6892 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 6893 | .x(150) |
| 6894 | .y(50)) |
| 6895 | .build())) |
| 6896 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6897 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6898 | mWindow->consumeDragEvent(true, 150, 50); |
| 6899 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 6900 | |
| 6901 | // drop to another window. |
| 6902 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6903 | injectMotionEvent(mDispatcher, |
| 6904 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 6905 | .buttonState(0) |
| 6906 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 6907 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 6908 | .x(150) |
| 6909 | .y(50)) |
| 6910 | .build())) |
| 6911 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6912 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6913 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 6914 | mWindow->assertNoEvents(); |
| 6915 | mSecondWindow->assertNoEvents(); |
| 6916 | } |
| 6917 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6918 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 6919 | |
| 6920 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 6921 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6922 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6923 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6924 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6925 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6926 | window->setFocusable(true); |
| 6927 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 6928 | setFocusedWindow(window); |
| 6929 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 6930 | |
| 6931 | // With the flag set, window should not get any input |
| 6932 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 6933 | mDispatcher->notifyKey(&keyArgs); |
| 6934 | window->assertNoEvents(); |
| 6935 | |
| 6936 | NotifyMotionArgs motionArgs = |
| 6937 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6938 | ADISPLAY_ID_DEFAULT); |
| 6939 | mDispatcher->notifyMotion(&motionArgs); |
| 6940 | window->assertNoEvents(); |
| 6941 | |
| 6942 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6943 | window->setDropInput(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6944 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 6945 | |
| 6946 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 6947 | mDispatcher->notifyKey(&keyArgs); |
| 6948 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 6949 | |
| 6950 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6951 | ADISPLAY_ID_DEFAULT); |
| 6952 | mDispatcher->notifyMotion(&motionArgs); |
| 6953 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6954 | window->assertNoEvents(); |
| 6955 | } |
| 6956 | |
| 6957 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 6958 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 6959 | std::make_shared<FakeApplicationHandle>(); |
| 6960 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6961 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 6962 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6963 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 6964 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6965 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6966 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6967 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6968 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6969 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6970 | window->setOwnerInfo(222, 222); |
| 6971 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6972 | window->setFocusable(true); |
| 6973 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 6974 | setFocusedWindow(window); |
| 6975 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 6976 | |
| 6977 | // With the flag set, window should not get any input |
| 6978 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 6979 | mDispatcher->notifyKey(&keyArgs); |
| 6980 | window->assertNoEvents(); |
| 6981 | |
| 6982 | NotifyMotionArgs motionArgs = |
| 6983 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6984 | ADISPLAY_ID_DEFAULT); |
| 6985 | mDispatcher->notifyMotion(&motionArgs); |
| 6986 | window->assertNoEvents(); |
| 6987 | |
| 6988 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6989 | window->setDropInputIfObscured(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6990 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 6991 | |
| 6992 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 6993 | mDispatcher->notifyKey(&keyArgs); |
| 6994 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 6995 | |
| 6996 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6997 | ADISPLAY_ID_DEFAULT); |
| 6998 | mDispatcher->notifyMotion(&motionArgs); |
| 6999 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 7000 | window->assertNoEvents(); |
| 7001 | } |
| 7002 | |
| 7003 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 7004 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 7005 | std::make_shared<FakeApplicationHandle>(); |
| 7006 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7007 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 7008 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7009 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 7010 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7011 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7012 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7013 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7014 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7015 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7016 | window->setOwnerInfo(222, 222); |
| 7017 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7018 | window->setFocusable(true); |
| 7019 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 7020 | setFocusedWindow(window); |
| 7021 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 7022 | |
| 7023 | // With the flag set, window should not get any input |
| 7024 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 7025 | mDispatcher->notifyKey(&keyArgs); |
| 7026 | window->assertNoEvents(); |
| 7027 | |
| 7028 | NotifyMotionArgs motionArgs = |
| 7029 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7030 | ADISPLAY_ID_DEFAULT); |
| 7031 | mDispatcher->notifyMotion(&motionArgs); |
| 7032 | window->assertNoEvents(); |
| 7033 | |
| 7034 | // When the window is no longer obscured because it went on top, it should get input |
| 7035 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 7036 | |
| 7037 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 7038 | mDispatcher->notifyKey(&keyArgs); |
| 7039 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 7040 | |
| 7041 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7042 | ADISPLAY_ID_DEFAULT); |
| 7043 | mDispatcher->notifyMotion(&motionArgs); |
| 7044 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7045 | window->assertNoEvents(); |
| 7046 | } |
| 7047 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7048 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 7049 | protected: |
| 7050 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7051 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7052 | sp<FakeWindowHandle> mWindow; |
| 7053 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7054 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7055 | |
| 7056 | void SetUp() override { |
| 7057 | InputDispatcherTest::SetUp(); |
| 7058 | |
| 7059 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7060 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7061 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7062 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7063 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7064 | mSecondWindow = |
| 7065 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7066 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7067 | mThirdWindow = |
| 7068 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 7069 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 7070 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7071 | |
| 7072 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7073 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}, |
| 7074 | {SECOND_DISPLAY_ID, {mThirdWindow}}}); |
| 7075 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7076 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7077 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7078 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 7079 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7080 | WINDOW_UID, true /* hasPermission */, |
| 7081 | ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7082 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 7083 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7084 | mThirdWindow->assertNoEvents(); |
| 7085 | } |
| 7086 | |
| 7087 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 7088 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
| 7089 | SECONDARY_WINDOW_UID, true /* hasPermission */, |
| 7090 | SECOND_DISPLAY_ID)) { |
| 7091 | mWindow->assertNoEvents(); |
| 7092 | mSecondWindow->assertNoEvents(); |
| 7093 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7094 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7095 | } |
| 7096 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7097 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid, |
| 7098 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7099 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 7100 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7101 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 7102 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7103 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7104 | } |
| 7105 | }; |
| 7106 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7107 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7108 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7109 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 7110 | windowInfo.ownerPid, windowInfo.ownerUid, |
| 7111 | false /* hasPermission */); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7112 | } |
| 7113 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7114 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 7115 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7116 | int32_t ownerPid = windowInfo.ownerPid; |
| 7117 | int32_t ownerUid = windowInfo.ownerUid; |
| 7118 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
| 7119 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7120 | ownerUid, false /*hasPermission*/, |
| 7121 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7122 | mWindow->assertNoEvents(); |
| 7123 | mSecondWindow->assertNoEvents(); |
| 7124 | } |
| 7125 | |
| 7126 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 7127 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7128 | int32_t ownerPid = windowInfo.ownerPid; |
| 7129 | int32_t ownerUid = windowInfo.ownerUid; |
| 7130 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7131 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
| 7132 | ownerUid, true /*hasPermission*/); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7133 | } |
| 7134 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7135 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7136 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7137 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 7138 | windowInfo.ownerPid, windowInfo.ownerUid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7139 | true /*hasPermission*/, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7140 | mWindow->assertNoEvents(); |
| 7141 | mSecondWindow->assertNoEvents(); |
| 7142 | } |
| 7143 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7144 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 7145 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 7146 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 7147 | windowInfo.ownerPid, windowInfo.ownerUid, |
| 7148 | true /*hasPermission*/, SECOND_DISPLAY_ID)); |
| 7149 | mWindow->assertNoEvents(); |
| 7150 | mSecondWindow->assertNoEvents(); |
| 7151 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 7152 | } |
| 7153 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7154 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 7155 | // Interact with the window first. |
| 7156 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 7157 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 7158 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7159 | |
| 7160 | // Then remove focus. |
| 7161 | mWindow->setFocusable(false); |
| 7162 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 7163 | |
| 7164 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 7165 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7166 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 7167 | windowInfo.ownerPid, windowInfo.ownerUid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7168 | false /*hasPermission*/, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7169 | } |
| 7170 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7171 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 7172 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7173 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7174 | std::shared_ptr<FakeApplicationHandle> application = |
| 7175 | std::make_shared<FakeApplicationHandle>(); |
| 7176 | std::string name = "Fake Spy "; |
| 7177 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7178 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7179 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7180 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7181 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7182 | return spy; |
| 7183 | } |
| 7184 | |
| 7185 | sp<FakeWindowHandle> createForeground() { |
| 7186 | std::shared_ptr<FakeApplicationHandle> application = |
| 7187 | std::make_shared<FakeApplicationHandle>(); |
| 7188 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7189 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7190 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7191 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7192 | return window; |
| 7193 | } |
| 7194 | |
| 7195 | private: |
| 7196 | int mSpyCount{0}; |
| 7197 | }; |
| 7198 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7199 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7200 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7201 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 7202 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7203 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
| 7204 | ScopedSilentDeath _silentDeath; |
| 7205 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7206 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7207 | spy->setTrustedOverlay(false); |
| 7208 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), |
| 7209 | ".* not a trusted overlay"); |
| 7210 | } |
| 7211 | |
| 7212 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7213 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 7214 | */ |
| 7215 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7216 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7217 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); |
| 7218 | |
| 7219 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7220 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7221 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7222 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7223 | } |
| 7224 | |
| 7225 | /** |
| 7226 | * Verify the order in which different input windows receive events. The touched foreground window |
| 7227 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 7228 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 7229 | * receive events before ones belows it. |
| 7230 | * |
| 7231 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 7232 | * spy1, spy2, window, spy3. |
| 7233 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 7234 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 7235 | * window. |
| 7236 | */ |
| 7237 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 7238 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7239 | auto spy1 = createSpy(); |
| 7240 | auto spy2 = createSpy(); |
| 7241 | auto spy3 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7242 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); |
| 7243 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 7244 | const size_t numChannels = channels.size(); |
| 7245 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 7246 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7247 | if (!epollFd.ok()) { |
| 7248 | FAIL() << "Failed to create epoll fd"; |
| 7249 | } |
| 7250 | |
| 7251 | for (size_t i = 0; i < numChannels; i++) { |
| 7252 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 7253 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 7254 | FAIL() << "Failed to add fd to epoll"; |
| 7255 | } |
| 7256 | } |
| 7257 | |
| 7258 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7259 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7260 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7261 | |
| 7262 | std::vector<size_t> eventOrder; |
| 7263 | std::vector<struct epoll_event> events(numChannels); |
| 7264 | for (;;) { |
| 7265 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 7266 | (100ms).count()); |
| 7267 | if (nFds < 0) { |
| 7268 | FAIL() << "Failed to call epoll_wait"; |
| 7269 | } |
| 7270 | if (nFds == 0) { |
| 7271 | break; // epoll_wait timed out |
| 7272 | } |
| 7273 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 7274 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 7275 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7276 | channels[i]->consumeMotionDown(); |
| 7277 | } |
| 7278 | } |
| 7279 | |
| 7280 | // Verify the order in which the events were received. |
| 7281 | EXPECT_EQ(3u, eventOrder.size()); |
| 7282 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 7283 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 7284 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 7285 | } |
| 7286 | |
| 7287 | /** |
| 7288 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 7289 | */ |
| 7290 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 7291 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7292 | auto spy = createSpy(); |
| 7293 | spy->setTouchable(false); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7294 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7295 | |
| 7296 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7297 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7298 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7299 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7300 | spy->assertNoEvents(); |
| 7301 | } |
| 7302 | |
| 7303 | /** |
| 7304 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 7305 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 7306 | * to the window. |
| 7307 | */ |
| 7308 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 7309 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7310 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7311 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
| 7312 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7313 | |
| 7314 | // Inject an event outside the spy window's touchable region. |
| 7315 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7316 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7317 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7318 | window->consumeMotionDown(); |
| 7319 | spy->assertNoEvents(); |
| 7320 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7321 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7322 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7323 | window->consumeMotionUp(); |
| 7324 | spy->assertNoEvents(); |
| 7325 | |
| 7326 | // Inject an event inside the spy window's touchable region. |
| 7327 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7328 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7329 | {5, 10})) |
| 7330 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7331 | window->consumeMotionDown(); |
| 7332 | spy->consumeMotionDown(); |
| 7333 | } |
| 7334 | |
| 7335 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7336 | * 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] | 7337 | * 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] | 7338 | */ |
| 7339 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 7340 | auto window = createForeground(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7341 | window->setOwnerInfo(12, 34); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7342 | auto spy = createSpy(); |
| 7343 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7344 | spy->setOwnerInfo(56, 78); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7345 | spy->setFrame(Rect{0, 0, 20, 20}); |
| 7346 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7347 | |
| 7348 | // Inject an event outside the spy window's frame and touchable region. |
| 7349 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7350 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7351 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7352 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7353 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7354 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7355 | } |
| 7356 | |
| 7357 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7358 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 7359 | * pointers that are down within its bounds. |
| 7360 | */ |
| 7361 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 7362 | auto windowLeft = createForeground(); |
| 7363 | windowLeft->setFrame({0, 0, 100, 200}); |
| 7364 | auto windowRight = createForeground(); |
| 7365 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7366 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7367 | spy->setFrame({0, 0, 200, 200}); |
| 7368 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); |
| 7369 | |
| 7370 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7371 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7372 | {50, 50})) |
| 7373 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7374 | windowLeft->consumeMotionDown(); |
| 7375 | spy->consumeMotionDown(); |
| 7376 | |
| 7377 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7378 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7379 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7380 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7381 | .pointer( |
| 7382 | PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7383 | .build(); |
| 7384 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7385 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7386 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7387 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7388 | windowRight->consumeMotionDown(); |
| 7389 | spy->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 7390 | } |
| 7391 | |
| 7392 | /** |
| 7393 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 7394 | * the spy should receive the second pointer with ACTION_DOWN. |
| 7395 | */ |
| 7396 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 7397 | auto window = createForeground(); |
| 7398 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7399 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7400 | spyRight->setFrame({100, 0, 200, 200}); |
| 7401 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); |
| 7402 | |
| 7403 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7404 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7405 | {50, 50})) |
| 7406 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7407 | window->consumeMotionDown(); |
| 7408 | spyRight->assertNoEvents(); |
| 7409 | |
| 7410 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7411 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7412 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7413 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7414 | .pointer( |
| 7415 | PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7416 | .build(); |
| 7417 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7418 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7419 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7420 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7421 | window->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 7422 | spyRight->consumeMotionDown(); |
| 7423 | } |
| 7424 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7425 | /** |
| 7426 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 7427 | * windows should be allowed to control split touch. |
| 7428 | */ |
| 7429 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7430 | // 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] | 7431 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7432 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7433 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7434 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7435 | auto window = createForeground(); |
| 7436 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7437 | |
| 7438 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7439 | |
| 7440 | // First finger down, no window touched. |
| 7441 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7442 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7443 | {100, 200})) |
| 7444 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7445 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7446 | window->assertNoEvents(); |
| 7447 | |
| 7448 | // Second finger down on window, the window should receive touch down. |
| 7449 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7450 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7451 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7452 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7453 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7454 | .x(100) |
| 7455 | .y(200)) |
| 7456 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7457 | .build(); |
| 7458 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7459 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7460 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7461 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7462 | |
| 7463 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7464 | spy->consumeMotionPointerDown(1 /* pointerIndex */); |
| 7465 | } |
| 7466 | |
| 7467 | /** |
| 7468 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 7469 | * do not receive key events. |
| 7470 | */ |
| 7471 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7472 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7473 | spy->setFocusable(false); |
| 7474 | |
| 7475 | auto window = createForeground(); |
| 7476 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7477 | setFocusedWindow(window); |
| 7478 | window->consumeFocusEvent(true); |
| 7479 | |
| 7480 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7481 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 7482 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7483 | |
| 7484 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7485 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 7486 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7487 | |
| 7488 | spy->assertNoEvents(); |
| 7489 | } |
| 7490 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 7491 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 7492 | |
| 7493 | /** |
| 7494 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 7495 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 7496 | */ |
| 7497 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 7498 | auto window = createForeground(); |
| 7499 | auto spy1 = createSpy(); |
| 7500 | auto spy2 = createSpy(); |
| 7501 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); |
| 7502 | |
| 7503 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7504 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7505 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7506 | window->consumeMotionDown(); |
| 7507 | spy1->consumeMotionDown(); |
| 7508 | spy2->consumeMotionDown(); |
| 7509 | |
| 7510 | // Pilfer pointers from the second spy window. |
| 7511 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 7512 | spy2->assertNoEvents(); |
| 7513 | spy1->consumeMotionCancel(); |
| 7514 | window->consumeMotionCancel(); |
| 7515 | |
| 7516 | // The rest of the gesture should only be sent to the second spy window. |
| 7517 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7518 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7519 | ADISPLAY_ID_DEFAULT)) |
| 7520 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7521 | spy2->consumeMotionMove(); |
| 7522 | spy1->assertNoEvents(); |
| 7523 | window->assertNoEvents(); |
| 7524 | } |
| 7525 | |
| 7526 | /** |
| 7527 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 7528 | * in the middle of the gesture. |
| 7529 | */ |
| 7530 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 7531 | auto window = createForeground(); |
| 7532 | auto spy = createSpy(); |
| 7533 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7534 | |
| 7535 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7536 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7537 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7538 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7539 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7540 | |
| 7541 | window->releaseChannel(); |
| 7542 | |
| 7543 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 7544 | |
| 7545 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7546 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7547 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7548 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7549 | } |
| 7550 | |
| 7551 | /** |
| 7552 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 7553 | * the spy, but not to any other windows. |
| 7554 | */ |
| 7555 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 7556 | auto spy = createSpy(); |
| 7557 | auto window = createForeground(); |
| 7558 | |
| 7559 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7560 | |
| 7561 | // First finger down on the window and the spy. |
| 7562 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7563 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7564 | {100, 200})) |
| 7565 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7566 | spy->consumeMotionDown(); |
| 7567 | window->consumeMotionDown(); |
| 7568 | |
| 7569 | // Spy window pilfers the pointers. |
| 7570 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 7571 | window->consumeMotionCancel(); |
| 7572 | |
| 7573 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 7574 | const MotionEvent secondFingerDownEvent = |
| 7575 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7576 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7577 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7578 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7579 | .x(100) |
| 7580 | .y(200)) |
| 7581 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7582 | .build(); |
| 7583 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7584 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7585 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7586 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7587 | |
| 7588 | spy->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 7589 | |
| 7590 | // Third finger goes down outside all windows, so injection should fail. |
| 7591 | const MotionEvent thirdFingerDownEvent = |
| 7592 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7593 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7594 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7595 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7596 | .x(100) |
| 7597 | .y(200)) |
| 7598 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7599 | .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-5).y(-5)) |
| 7600 | .build(); |
| 7601 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 7602 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7603 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7604 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7605 | |
| 7606 | spy->assertNoEvents(); |
| 7607 | window->assertNoEvents(); |
| 7608 | } |
| 7609 | |
| 7610 | /** |
| 7611 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 7612 | */ |
| 7613 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 7614 | auto spy = createSpy(); |
| 7615 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 7616 | auto window = createForeground(); |
| 7617 | window->setFrame(Rect(0, 0, 200, 200)); |
| 7618 | |
| 7619 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7620 | |
| 7621 | // First finger down on the window only |
| 7622 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7623 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7624 | {150, 150})) |
| 7625 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7626 | window->consumeMotionDown(); |
| 7627 | |
| 7628 | // Second finger down on the spy and window |
| 7629 | const MotionEvent secondFingerDownEvent = |
| 7630 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7631 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7632 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7633 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7634 | .x(150) |
| 7635 | .y(150)) |
| 7636 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 7637 | .build(); |
| 7638 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7639 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7640 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7641 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7642 | spy->consumeMotionDown(); |
| 7643 | window->consumeMotionPointerDown(1); |
| 7644 | |
| 7645 | // Third finger down on the spy and window |
| 7646 | const MotionEvent thirdFingerDownEvent = |
| 7647 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7648 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7649 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7650 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7651 | .x(150) |
| 7652 | .y(150)) |
| 7653 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 7654 | .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7655 | .build(); |
| 7656 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7657 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7658 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7659 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7660 | spy->consumeMotionPointerDown(1); |
| 7661 | window->consumeMotionPointerDown(2); |
| 7662 | |
| 7663 | // Spy window pilfers the pointers. |
| 7664 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 7665 | window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 7666 | window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 7667 | |
| 7668 | spy->assertNoEvents(); |
| 7669 | window->assertNoEvents(); |
| 7670 | } |
| 7671 | |
| 7672 | /** |
| 7673 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 7674 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 7675 | * window, then that window should receive ACTION_CANCEL. |
| 7676 | */ |
| 7677 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 7678 | auto spy = createSpy(); |
| 7679 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 7680 | auto window = createForeground(); |
| 7681 | window->setFrame(Rect(0, 0, 200, 200)); |
| 7682 | |
| 7683 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7684 | |
| 7685 | // First finger down on both spy and window |
| 7686 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7687 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7688 | {10, 10})) |
| 7689 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7690 | window->consumeMotionDown(); |
| 7691 | spy->consumeMotionDown(); |
| 7692 | |
| 7693 | // Second finger down on the spy and window |
| 7694 | const MotionEvent secondFingerDownEvent = |
| 7695 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7696 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7697 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7698 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 7699 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7700 | .build(); |
| 7701 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7702 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7703 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7704 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7705 | spy->consumeMotionPointerDown(1); |
| 7706 | window->consumeMotionPointerDown(1); |
| 7707 | |
| 7708 | // Spy window pilfers the pointers. |
| 7709 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 7710 | window->consumeMotionCancel(); |
| 7711 | |
| 7712 | spy->assertNoEvents(); |
| 7713 | window->assertNoEvents(); |
| 7714 | } |
| 7715 | |
| 7716 | /** |
| 7717 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 7718 | * be sent to other windows |
| 7719 | */ |
| 7720 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 7721 | auto spy = createSpy(); |
| 7722 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 7723 | auto window = createForeground(); |
| 7724 | window->setFrame(Rect(0, 0, 200, 200)); |
| 7725 | |
| 7726 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7727 | |
| 7728 | // First finger down on both window and spy |
| 7729 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7730 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7731 | {10, 10})) |
| 7732 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7733 | window->consumeMotionDown(); |
| 7734 | spy->consumeMotionDown(); |
| 7735 | |
| 7736 | // Spy window pilfers the pointers. |
| 7737 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 7738 | window->consumeMotionCancel(); |
| 7739 | |
| 7740 | // Second finger down on the window only |
| 7741 | const MotionEvent secondFingerDownEvent = |
| 7742 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7743 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7744 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7745 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 7746 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7747 | .x(150) |
| 7748 | .y(150)) |
| 7749 | .build(); |
| 7750 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7751 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7752 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7753 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7754 | window->consumeMotionDown(); |
| 7755 | window->assertNoEvents(); |
| 7756 | |
| 7757 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 7758 | spy->consumeMotionMove(); |
| 7759 | spy->assertNoEvents(); |
| 7760 | } |
| 7761 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7762 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 7763 | public: |
| 7764 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 7765 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 7766 | std::make_shared<FakeApplicationHandle>(); |
| 7767 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7768 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 7769 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7770 | overlay->setFocusable(false); |
| 7771 | overlay->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7772 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7773 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7774 | overlay->setTrustedOverlay(true); |
| 7775 | |
| 7776 | std::shared_ptr<FakeApplicationHandle> application = |
| 7777 | std::make_shared<FakeApplicationHandle>(); |
| 7778 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7779 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 7780 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7781 | window->setFocusable(true); |
| 7782 | window->setOwnerInfo(222, 222); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7783 | |
| 7784 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7785 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 7786 | setFocusedWindow(window); |
| 7787 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 7788 | return {std::move(overlay), std::move(window)}; |
| 7789 | } |
| 7790 | |
| 7791 | void sendFingerEvent(int32_t action) { |
| 7792 | NotifyMotionArgs motionArgs = |
| 7793 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 7794 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}}); |
| 7795 | mDispatcher->notifyMotion(&motionArgs); |
| 7796 | } |
| 7797 | |
| 7798 | void sendStylusEvent(int32_t action) { |
| 7799 | NotifyMotionArgs motionArgs = |
| 7800 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 7801 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
| 7802 | motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 7803 | mDispatcher->notifyMotion(&motionArgs); |
| 7804 | } |
| 7805 | }; |
| 7806 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7807 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 7808 | |
| 7809 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
| 7810 | ScopedSilentDeath _silentDeath; |
| 7811 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7812 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 7813 | overlay->setTrustedOverlay(false); |
| 7814 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
| 7815 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), |
| 7816 | ".* not a trusted overlay"); |
| 7817 | } |
| 7818 | |
| 7819 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 7820 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 7821 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 7822 | |
| 7823 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 7824 | overlay->consumeMotionDown(); |
| 7825 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 7826 | overlay->consumeMotionUp(); |
| 7827 | |
| 7828 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 7829 | window->consumeMotionDown(); |
| 7830 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 7831 | window->consumeMotionUp(); |
| 7832 | |
| 7833 | overlay->assertNoEvents(); |
| 7834 | window->assertNoEvents(); |
| 7835 | } |
| 7836 | |
| 7837 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 7838 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7839 | overlay->setSpy(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7840 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 7841 | |
| 7842 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 7843 | overlay->consumeMotionDown(); |
| 7844 | window->consumeMotionDown(); |
| 7845 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 7846 | overlay->consumeMotionUp(); |
| 7847 | window->consumeMotionUp(); |
| 7848 | |
| 7849 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 7850 | window->consumeMotionDown(); |
| 7851 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 7852 | window->consumeMotionUp(); |
| 7853 | |
| 7854 | overlay->assertNoEvents(); |
| 7855 | window->assertNoEvents(); |
| 7856 | } |
| 7857 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 7858 | /** |
| 7859 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 7860 | * The scenario is as follows: |
| 7861 | * - The stylus interceptor overlay is configured as a spy window. |
| 7862 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 7863 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 7864 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 7865 | */ |
| 7866 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 7867 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 7868 | overlay->setSpy(true); |
| 7869 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 7870 | |
| 7871 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 7872 | overlay->consumeMotionDown(); |
| 7873 | window->consumeMotionDown(); |
| 7874 | |
| 7875 | // The interceptor pilfers the pointers. |
| 7876 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 7877 | window->consumeMotionCancel(); |
| 7878 | |
| 7879 | // The interceptor configures itself so that it is no longer a spy. |
| 7880 | overlay->setSpy(false); |
| 7881 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 7882 | |
| 7883 | // It continues to receive the rest of the stylus gesture. |
| 7884 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 7885 | overlay->consumeMotionMove(); |
| 7886 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 7887 | overlay->consumeMotionUp(); |
| 7888 | |
| 7889 | window->assertNoEvents(); |
| 7890 | } |
| 7891 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 7892 | struct User { |
| 7893 | int32_t mPid; |
| 7894 | int32_t mUid; |
| 7895 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 7896 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 7897 | |
| 7898 | User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid) |
| 7899 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 7900 | |
| 7901 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
| 7902 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
| 7903 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 7904 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 7905 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 7906 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 7907 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 7908 | } |
| 7909 | |
| 7910 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
| 7911 | return inputdispatcher::injectKey(mDispatcher, action, 0 /* repeatCount*/, ADISPLAY_ID_NONE, |
| 7912 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 7913 | INJECT_EVENT_TIMEOUT, false /*allowKeyRepeat*/, {mUid}, |
| 7914 | mPolicyFlags); |
| 7915 | } |
| 7916 | |
| 7917 | sp<FakeWindowHandle> createWindow() const { |
| 7918 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 7919 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7920 | sp<FakeWindowHandle> window = |
| 7921 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window", |
| 7922 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 7923 | window->setOwnerInfo(mPid, mUid); |
| 7924 | return window; |
| 7925 | } |
| 7926 | }; |
| 7927 | |
| 7928 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 7929 | |
| 7930 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
| 7931 | auto owner = User(mDispatcher, 10, 11); |
| 7932 | auto window = owner.createWindow(); |
| 7933 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 7934 | |
| 7935 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7936 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 7937 | window->consumeMotionDown(); |
| 7938 | |
| 7939 | setFocusedWindow(window); |
| 7940 | window->consumeFocusEvent(true); |
| 7941 | |
| 7942 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7943 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 7944 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7945 | } |
| 7946 | |
| 7947 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
| 7948 | auto owner = User(mDispatcher, 10, 11); |
| 7949 | auto window = owner.createWindow(); |
| 7950 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 7951 | |
| 7952 | auto rando = User(mDispatcher, 20, 21); |
| 7953 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 7954 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 7955 | |
| 7956 | setFocusedWindow(window); |
| 7957 | window->consumeFocusEvent(true); |
| 7958 | |
| 7959 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 7960 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 7961 | window->assertNoEvents(); |
| 7962 | } |
| 7963 | |
| 7964 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
| 7965 | auto owner = User(mDispatcher, 10, 11); |
| 7966 | auto window = owner.createWindow(); |
| 7967 | auto spy = owner.createWindow(); |
| 7968 | spy->setSpy(true); |
| 7969 | spy->setTrustedOverlay(true); |
| 7970 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7971 | |
| 7972 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7973 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 7974 | spy->consumeMotionDown(); |
| 7975 | window->consumeMotionDown(); |
| 7976 | } |
| 7977 | |
| 7978 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
| 7979 | auto owner = User(mDispatcher, 10, 11); |
| 7980 | auto window = owner.createWindow(); |
| 7981 | |
| 7982 | auto rando = User(mDispatcher, 20, 21); |
| 7983 | auto randosSpy = rando.createWindow(); |
| 7984 | randosSpy->setSpy(true); |
| 7985 | randosSpy->setTrustedOverlay(true); |
| 7986 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 7987 | |
| 7988 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 7989 | // not receive the event. |
| 7990 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7991 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 7992 | randosSpy->assertNoEvents(); |
| 7993 | window->consumeMotionDown(); |
| 7994 | } |
| 7995 | |
| 7996 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
| 7997 | auto owner = User(mDispatcher, 10, 11); |
| 7998 | auto window = owner.createWindow(); |
| 7999 | |
| 8000 | auto rando = User(mDispatcher, 20, 21); |
| 8001 | auto randosSpy = rando.createWindow(); |
| 8002 | randosSpy->setSpy(true); |
| 8003 | randosSpy->setTrustedOverlay(true); |
| 8004 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 8005 | |
| 8006 | // A user that has injection permission can inject into any window. |
| 8007 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8008 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8009 | ADISPLAY_ID_DEFAULT)); |
| 8010 | randosSpy->consumeMotionDown(); |
| 8011 | window->consumeMotionDown(); |
| 8012 | |
| 8013 | setFocusedWindow(randosSpy); |
| 8014 | randosSpy->consumeFocusEvent(true); |
| 8015 | |
| 8016 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
| 8017 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8018 | window->assertNoEvents(); |
| 8019 | } |
| 8020 | |
| 8021 | TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) { |
| 8022 | auto owner = User(mDispatcher, 10, 11); |
| 8023 | auto window = owner.createWindow(); |
| 8024 | |
| 8025 | auto rando = User(mDispatcher, 20, 21); |
| 8026 | auto randosWindow = rando.createWindow(); |
| 8027 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 8028 | randosWindow->setWatchOutsideTouch(true); |
| 8029 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); |
| 8030 | |
| 8031 | // We allow generation of ACTION_OUTSIDE events into windows owned by different uids. |
| 8032 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8033 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8034 | window->consumeMotionDown(); |
| 8035 | randosWindow->consumeMotionOutside(); |
| 8036 | } |
| 8037 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 8038 | } // namespace android::inputdispatcher |