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); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 65 | static constexpr int32_t POINTER_0_UP = |
| 66 | AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 67 | static constexpr int32_t POINTER_1_UP = |
| 68 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 69 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 70 | // 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] | 71 | static constexpr int32_t WINDOW_PID = 999; |
| 72 | static constexpr int32_t WINDOW_UID = 1001; |
| 73 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 74 | // The default pid and uid for the windows created on the secondary display by the test. |
| 75 | static constexpr int32_t SECONDARY_WINDOW_PID = 1010; |
| 76 | static constexpr int32_t SECONDARY_WINDOW_UID = 1012; |
| 77 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 78 | // The default policy flags to use for event injection by tests. |
| 79 | 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] | 80 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 81 | // An arbitrary pid of the gesture monitor window |
| 82 | static constexpr int32_t MONITOR_PID = 2001; |
| 83 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 84 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 85 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 86 | static constexpr int expectedWallpaperFlags = |
| 87 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 88 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 89 | struct PointF { |
| 90 | float x; |
| 91 | float y; |
| 92 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 93 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 94 | /** |
| 95 | * Return a DOWN key event with KEYCODE_A. |
| 96 | */ |
| 97 | static KeyEvent getTestKeyEvent() { |
| 98 | KeyEvent event; |
| 99 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 100 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 101 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 102 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 103 | return event; |
| 104 | } |
| 105 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 106 | static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) { |
| 107 | ASSERT_EQ(expectedAction, receivedAction) |
| 108 | << "expected " << MotionEvent::actionToString(expectedAction) << ", got " |
| 109 | << MotionEvent::actionToString(receivedAction); |
| 110 | } |
| 111 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 112 | MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") { |
| 113 | bool matches = action == arg.getAction(); |
| 114 | if (!matches) { |
| 115 | *result_listener << "expected action " << MotionEvent::actionToString(action) |
| 116 | << ", but got " << MotionEvent::actionToString(arg.getAction()); |
| 117 | } |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 118 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 119 | if (!matches) { |
| 120 | *result_listener << "; "; |
| 121 | } |
| 122 | *result_listener << "downTime should match eventTime for ACTION_DOWN events"; |
| 123 | matches &= arg.getDownTime() == arg.getEventTime(); |
| 124 | } |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 125 | if (action == AMOTION_EVENT_ACTION_CANCEL) { |
| 126 | if (!matches) { |
| 127 | *result_listener << "; "; |
| 128 | } |
| 129 | *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set"; |
| 130 | matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0; |
| 131 | } |
| 132 | return matches; |
| 133 | } |
| 134 | |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 135 | MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") { |
| 136 | return arg.getDownTime() == downTime; |
| 137 | } |
| 138 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 139 | MATCHER_P(WithSource, source, "InputEvent with specified source") { |
| 140 | *result_listener << "expected source " << inputEventSourceToString(source) << ", but got " |
| 141 | << inputEventSourceToString(arg.getSource()); |
| 142 | return arg.getSource() == source; |
| 143 | } |
| 144 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 145 | // --- FakeInputDispatcherPolicy --- |
| 146 | |
| 147 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 148 | InputDispatcherConfiguration mConfig; |
| 149 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 150 | using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>; |
| 151 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 152 | protected: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 153 | virtual ~FakeInputDispatcherPolicy() {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 154 | |
| 155 | public: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 156 | FakeInputDispatcherPolicy() {} |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 157 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 158 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 159 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
| 160 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_KEY); |
| 161 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 162 | |
| 163 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 164 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 165 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 166 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 167 | } |
| 168 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 169 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 170 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
| 171 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_MOTION); |
| 172 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 173 | |
| 174 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 175 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 176 | EXPECT_EQ(motionEvent.getAction(), args.action); |
| 177 | EXPECT_EQ(motionEvent.getX(0), point.x); |
| 178 | EXPECT_EQ(motionEvent.getY(0), point.y); |
| 179 | EXPECT_EQ(motionEvent.getRawX(0), point.x); |
| 180 | EXPECT_EQ(motionEvent.getRawY(0), point.y); |
| 181 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 182 | } |
| 183 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 184 | void assertFilterInputEventWasNotCalled() { |
| 185 | std::scoped_lock lock(mLock); |
| 186 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 187 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 188 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 189 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 190 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 191 | ASSERT_TRUE(mConfigurationChangedTime) |
| 192 | << "Timed out waiting for configuration changed call"; |
| 193 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 194 | mConfigurationChangedTime = std::nullopt; |
| 195 | } |
| 196 | |
| 197 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 198 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 199 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 200 | // 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] | 201 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 202 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 203 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 204 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 205 | mLastNotifySwitch = std::nullopt; |
| 206 | } |
| 207 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 208 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 209 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 210 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 211 | mOnPointerDownToken.clear(); |
| 212 | } |
| 213 | |
| 214 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 215 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 216 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 217 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 220 | // This function must be called soon after the expected ANR timer starts, |
| 221 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 222 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 223 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 224 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 225 | std::unique_lock lock(mLock); |
| 226 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 227 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 228 | ASSERT_NO_FATAL_FAILURE( |
| 229 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 230 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 233 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 234 | const sp<WindowInfoHandle>& window) { |
| 235 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 236 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 237 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 238 | } |
| 239 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 240 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 241 | const sp<IBinder>& expectedToken, |
| 242 | int32_t expectedPid) { |
| 243 | std::unique_lock lock(mLock); |
| 244 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 245 | AnrResult result; |
| 246 | ASSERT_NO_FATAL_FAILURE(result = |
| 247 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
| 248 | const auto& [token, pid] = result; |
| 249 | ASSERT_EQ(expectedToken, token); |
| 250 | ASSERT_EQ(expectedPid, pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 251 | } |
| 252 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 253 | /** 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] | 254 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 255 | std::unique_lock lock(mLock); |
| 256 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 257 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 258 | const auto& [token, _] = result; |
| 259 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 262 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
| 263 | int32_t expectedPid) { |
| 264 | std::unique_lock lock(mLock); |
| 265 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 266 | AnrResult result; |
| 267 | ASSERT_NO_FATAL_FAILURE( |
| 268 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
| 269 | const auto& [token, pid] = result; |
| 270 | ASSERT_EQ(expectedToken, token); |
| 271 | ASSERT_EQ(expectedPid, pid); |
| 272 | } |
| 273 | |
| 274 | /** 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] | 275 | sp<IBinder> getResponsiveWindowToken() { |
| 276 | std::unique_lock lock(mLock); |
| 277 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 278 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 279 | const auto& [token, _] = result; |
| 280 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void assertNotifyAnrWasNotCalled() { |
| 284 | std::scoped_lock lock(mLock); |
| 285 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 286 | ASSERT_TRUE(mAnrWindows.empty()); |
| 287 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 288 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 289 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 292 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 293 | mConfig.keyRepeatTimeout = timeout; |
| 294 | mConfig.keyRepeatDelay = delay; |
| 295 | } |
| 296 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 297 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 298 | std::unique_lock lock(mLock); |
| 299 | base::ScopedLockAssertion assumeLocked(mLock); |
| 300 | |
| 301 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 302 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 303 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 304 | enabled; |
| 305 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 306 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 307 | << ") to be called."; |
| 308 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 309 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 310 | auto request = *mPointerCaptureRequest; |
| 311 | mPointerCaptureRequest.reset(); |
| 312 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | void assertSetPointerCaptureNotCalled() { |
| 316 | std::unique_lock lock(mLock); |
| 317 | base::ScopedLockAssertion assumeLocked(mLock); |
| 318 | |
| 319 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 320 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 321 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 322 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 323 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 324 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 325 | } |
| 326 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 327 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { |
| 328 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 329 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 330 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 331 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 332 | } |
| 333 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 334 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 335 | std::unique_lock lock(mLock); |
| 336 | base::ScopedLockAssertion assumeLocked(mLock); |
| 337 | std::optional<sp<IBinder>> receivedToken = |
| 338 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 339 | mNotifyInputChannelBroken); |
| 340 | ASSERT_TRUE(receivedToken.has_value()); |
| 341 | ASSERT_EQ(token, *receivedToken); |
| 342 | } |
| 343 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 344 | /** |
| 345 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 346 | */ |
| 347 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 348 | mInterceptKeyTimeout = timeout; |
| 349 | } |
| 350 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 351 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 352 | std::mutex mLock; |
| 353 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 354 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 355 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 356 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 357 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 358 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 359 | |
| 360 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 361 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 362 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 363 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 364 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 365 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 366 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 367 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 368 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 369 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 370 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 371 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 372 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 373 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 374 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 375 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 376 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 377 | // first entry from the container and erase it. |
| 378 | template <class T> |
| 379 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 380 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 381 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 382 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 383 | // before checking if ANR was called. |
| 384 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 385 | // to provide it some time to act. 100ms seems reasonable. |
| 386 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 387 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 388 | std::optional<T> token = |
| 389 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 390 | if (!token.has_value()) { |
| 391 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 392 | return {}; |
| 393 | } |
| 394 | |
| 395 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 396 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 397 | // the dispatcher started counting before this function was called |
| 398 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 399 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 400 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 401 | << "ms, but waited " |
| 402 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 403 | << "ms instead"; |
| 404 | } |
| 405 | return *token; |
| 406 | } |
| 407 | |
| 408 | template <class T> |
| 409 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 410 | std::queue<T>& storage, |
| 411 | std::unique_lock<std::mutex>& lock, |
| 412 | std::condition_variable& condition) |
| 413 | REQUIRES(mLock) { |
| 414 | condition.wait_for(lock, timeout, |
| 415 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 416 | if (storage.empty()) { |
| 417 | ADD_FAILURE() << "Did not receive the expected callback"; |
| 418 | return std::nullopt; |
| 419 | } |
| 420 | T item = storage.front(); |
| 421 | storage.pop(); |
| 422 | return std::make_optional(item); |
| 423 | } |
| 424 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 425 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 426 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 427 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 430 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid, |
| 431 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 432 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 433 | ASSERT_TRUE(pid.has_value()); |
| 434 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 435 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 436 | } |
| 437 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 438 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
| 439 | std::optional<int32_t> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 440 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 441 | ASSERT_TRUE(pid.has_value()); |
| 442 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 443 | mNotifyAnr.notify_all(); |
| 444 | } |
| 445 | |
| 446 | void notifyNoFocusedWindowAnr( |
| 447 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 448 | std::scoped_lock lock(mLock); |
| 449 | mAnrApplications.push(applicationHandle); |
| 450 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 453 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 454 | std::scoped_lock lock(mLock); |
| 455 | mBrokenInputChannels.push(connectionToken); |
| 456 | mNotifyInputChannelBroken.notify_all(); |
| 457 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 458 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 459 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 460 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 461 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 462 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 463 | const std::vector<float>& values) override {} |
| 464 | |
| 465 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 466 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 467 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 468 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 469 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 470 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 471 | *outConfig = mConfig; |
| 472 | } |
| 473 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 474 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 475 | std::scoped_lock lock(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 476 | switch (inputEvent->getType()) { |
| 477 | case AINPUT_EVENT_TYPE_KEY: { |
| 478 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 479 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 480 | break; |
| 481 | } |
| 482 | |
| 483 | case AINPUT_EVENT_TYPE_MOTION: { |
| 484 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 485 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 486 | break; |
| 487 | } |
| 488 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 489 | return true; |
| 490 | } |
| 491 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 492 | void interceptKeyBeforeQueueing(const KeyEvent* inputEvent, uint32_t&) override { |
| 493 | if (inputEvent->getAction() == AKEY_EVENT_ACTION_UP) { |
| 494 | // Clear intercept state when we handled the event. |
| 495 | mInterceptKeyTimeout = 0ms; |
| 496 | } |
| 497 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 498 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 499 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 500 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 501 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 502 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 503 | // Clear intercept state so we could dispatch the event in next wake. |
| 504 | mInterceptKeyTimeout = 0ms; |
| 505 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 508 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 509 | return false; |
| 510 | } |
| 511 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 512 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 513 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 514 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 515 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 516 | * essentially a passthrough for notifySwitch. |
| 517 | */ |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 518 | mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 519 | } |
| 520 | |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 521 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 522 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 523 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 524 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 525 | mOnPointerDownToken = newToken; |
| 526 | } |
| 527 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 528 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 529 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 530 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 531 | mPointerCaptureChangedCondition.notify_all(); |
| 532 | } |
| 533 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 534 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 535 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 536 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 537 | mDropTargetWindowToken = token; |
| 538 | } |
| 539 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 540 | void assertFilterInputEventWasCalledInternal( |
| 541 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 542 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 543 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 544 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 545 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 546 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 547 | }; |
| 548 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 549 | // --- InputDispatcherTest --- |
| 550 | |
| 551 | class InputDispatcherTest : public testing::Test { |
| 552 | protected: |
| 553 | sp<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 554 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 555 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 556 | void SetUp() override { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 557 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 558 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy, STALE_EVENT_TIMEOUT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 559 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 560 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 561 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 564 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 565 | ASSERT_EQ(OK, mDispatcher->stop()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 566 | mFakePolicy.clear(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 567 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 568 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 569 | |
| 570 | /** |
| 571 | * Used for debugging when writing the test |
| 572 | */ |
| 573 | void dumpDispatcherState() { |
| 574 | std::string dump; |
| 575 | mDispatcher->dump(dump); |
| 576 | std::stringstream ss(dump); |
| 577 | std::string to; |
| 578 | |
| 579 | while (std::getline(ss, to, '\n')) { |
| 580 | ALOGE("%s", to.c_str()); |
| 581 | } |
| 582 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 583 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 584 | void setFocusedWindow(const sp<WindowInfoHandle>& window, |
| 585 | const sp<WindowInfoHandle>& focusedWindow = nullptr) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 586 | FocusRequest request; |
| 587 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 588 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 589 | if (focusedWindow) { |
| 590 | request.focusedToken = focusedWindow->getToken(); |
| 591 | } |
| 592 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 593 | request.displayId = window->getInfo()->displayId; |
| 594 | mDispatcher->setFocusedWindow(request); |
| 595 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 596 | }; |
| 597 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 598 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 599 | KeyEvent event; |
| 600 | |
| 601 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 602 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 603 | INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 604 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 605 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 606 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 607 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 608 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 609 | << "Should reject key events with undefined action."; |
| 610 | |
| 611 | // 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] | 612 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 613 | 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] | 614 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 615 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 616 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 617 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 618 | << "Should reject key events with ACTION_MULTIPLE."; |
| 619 | } |
| 620 | |
| 621 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 622 | MotionEvent event; |
| 623 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 624 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 625 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 626 | pointerProperties[i].clear(); |
| 627 | pointerProperties[i].id = i; |
| 628 | pointerCoords[i].clear(); |
| 629 | } |
| 630 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 631 | // Some constants commonly used below |
| 632 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 633 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 634 | constexpr int32_t metaState = AMETA_NONE; |
| 635 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 636 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 637 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 638 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 639 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 640 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, |
| 641 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 642 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 643 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 644 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 645 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 646 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 647 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 648 | << "Should reject motion events with undefined action."; |
| 649 | |
| 650 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 651 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 652 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 653 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 654 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 655 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 656 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 657 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 658 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 659 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 660 | << "Should reject motion events with pointer down index too large."; |
| 661 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 662 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 663 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 664 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 665 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 666 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 667 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 668 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 669 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 670 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 671 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 672 | << "Should reject motion events with pointer down index too small."; |
| 673 | |
| 674 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 675 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 676 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 677 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 678 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 679 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 680 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 681 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 682 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 683 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 684 | << "Should reject motion events with pointer up index too large."; |
| 685 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 686 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 687 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 688 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 689 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 690 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 691 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 692 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 693 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 694 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 695 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 696 | << "Should reject motion events with pointer up index too small."; |
| 697 | |
| 698 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 699 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 700 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 701 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 702 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 703 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 704 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 705 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 706 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 707 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 708 | << "Should reject motion events with 0 pointers."; |
| 709 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 710 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 711 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 712 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 713 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 714 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 715 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 716 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 717 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 718 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 719 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 720 | |
| 721 | // Rejects motion events with invalid pointer ids. |
| 722 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 723 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 724 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 725 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 726 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 727 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 728 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 729 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 730 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 731 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 732 | << "Should reject motion events with pointer ids less than 0."; |
| 733 | |
| 734 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 735 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 736 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 737 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 738 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 739 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 740 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 741 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 742 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 743 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 744 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 745 | |
| 746 | // Rejects motion events with duplicate pointer ids. |
| 747 | pointerProperties[0].id = 1; |
| 748 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 749 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 750 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 751 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 752 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 753 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 754 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 755 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 756 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 757 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 758 | << "Should reject motion events with duplicate pointer ids."; |
| 759 | } |
| 760 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 761 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 762 | |
| 763 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 764 | constexpr nsecs_t eventTime = 20; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 765 | NotifyConfigurationChangedArgs args(10 /*id*/, eventTime); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 766 | mDispatcher->notifyConfigurationChanged(&args); |
| 767 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 768 | |
| 769 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 770 | } |
| 771 | |
| 772 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 773 | NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/, |
| 774 | 2 /*switchMask*/); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 775 | mDispatcher->notifySwitch(&args); |
| 776 | |
| 777 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 778 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 779 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 780 | } |
| 781 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 782 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 783 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 784 | // Default input dispatching timeout if there is no focused application or paused window |
| 785 | // from which to determine an appropriate dispatching timeout. |
| 786 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 787 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 788 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 789 | |
| 790 | class FakeApplicationHandle : public InputApplicationHandle { |
| 791 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 792 | FakeApplicationHandle() { |
| 793 | mInfo.name = "Fake Application"; |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 794 | mInfo.token = sp<BBinder>::make(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 795 | mInfo.dispatchingTimeoutMillis = |
| 796 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 797 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 798 | virtual ~FakeApplicationHandle() {} |
| 799 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 800 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 801 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 802 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 803 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 804 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 805 | }; |
| 806 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 807 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 808 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 809 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 810 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 811 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 812 | } |
| 813 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 814 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 815 | InputEvent* event; |
| 816 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 817 | if (!consumeSeq) { |
| 818 | return nullptr; |
| 819 | } |
| 820 | finishEvent(*consumeSeq); |
| 821 | return event; |
| 822 | } |
| 823 | |
| 824 | /** |
| 825 | * Receive an event without acknowledging it. |
| 826 | * Return the sequence number that could later be used to send finished signal. |
| 827 | */ |
| 828 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 829 | uint32_t consumeSeq; |
| 830 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 831 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 832 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 833 | status_t status = WOULD_BLOCK; |
| 834 | while (status == WOULD_BLOCK) { |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 835 | status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 836 | &event); |
| 837 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 838 | if (elapsed > 100ms) { |
| 839 | break; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | if (status == WOULD_BLOCK) { |
| 844 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 845 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | if (status != OK) { |
| 849 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 850 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 851 | } |
| 852 | if (event == nullptr) { |
| 853 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 854 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 855 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 856 | if (outEvent != nullptr) { |
| 857 | *outEvent = event; |
| 858 | } |
| 859 | return consumeSeq; |
| 860 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 861 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 862 | /** |
| 863 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 864 | */ |
| 865 | void finishEvent(uint32_t consumeSeq) { |
| 866 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 867 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 868 | } |
| 869 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 870 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 871 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); |
| 872 | ASSERT_EQ(OK, status); |
| 873 | } |
| 874 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 875 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 876 | std::optional<int32_t> expectedDisplayId, |
| 877 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 878 | InputEvent* event = consume(); |
| 879 | |
| 880 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 881 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 882 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 883 | << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType) |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 884 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 885 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 886 | if (expectedDisplayId.has_value()) { |
| 887 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 888 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 889 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 890 | switch (expectedEventType) { |
| 891 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 892 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 893 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 894 | if (expectedFlags.has_value()) { |
| 895 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 896 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 897 | break; |
| 898 | } |
| 899 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 900 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 901 | assertMotionAction(expectedAction, motionEvent.getAction()); |
| 902 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 903 | if (expectedFlags.has_value()) { |
| 904 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 905 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 906 | break; |
| 907 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 908 | case AINPUT_EVENT_TYPE_FOCUS: { |
| 909 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 910 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 911 | case AINPUT_EVENT_TYPE_CAPTURE: { |
| 912 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 913 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 914 | case AINPUT_EVENT_TYPE_TOUCH_MODE: { |
| 915 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 916 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 917 | case AINPUT_EVENT_TYPE_DRAG: { |
| 918 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 919 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 920 | default: { |
| 921 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; |
| 922 | } |
| 923 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 924 | } |
| 925 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 926 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 927 | InputEvent* event = consume(); |
| 928 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 929 | << ": consumer should have returned non-NULL event."; |
| 930 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType()) |
| 931 | << "Got " << inputEventTypeToString(event->getType()) |
| 932 | << " event instead of FOCUS event"; |
| 933 | |
| 934 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 935 | << mName.c_str() << ": event displayId should always be NONE."; |
| 936 | |
| 937 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 938 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 939 | } |
| 940 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 941 | void consumeCaptureEvent(bool hasCapture) { |
| 942 | const InputEvent* event = consume(); |
| 943 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 944 | << ": consumer should have returned non-NULL event."; |
| 945 | ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType()) |
| 946 | << "Got " << inputEventTypeToString(event->getType()) |
| 947 | << " event instead of CAPTURE event"; |
| 948 | |
| 949 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 950 | << mName.c_str() << ": event displayId should always be NONE."; |
| 951 | |
| 952 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 953 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 954 | } |
| 955 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 956 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 957 | const InputEvent* event = consume(); |
| 958 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 959 | << ": consumer should have returned non-NULL event."; |
| 960 | ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType()) |
| 961 | << "Got " << inputEventTypeToString(event->getType()) |
| 962 | << " event instead of DRAG event"; |
| 963 | |
| 964 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 965 | << mName.c_str() << ": event displayId should always be NONE."; |
| 966 | |
| 967 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 968 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 969 | EXPECT_EQ(x, dragEvent.getX()); |
| 970 | EXPECT_EQ(y, dragEvent.getY()); |
| 971 | } |
| 972 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 973 | void consumeTouchModeEvent(bool inTouchMode) { |
| 974 | const InputEvent* event = consume(); |
| 975 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 976 | << ": consumer should have returned non-NULL event."; |
| 977 | ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType()) |
| 978 | << "Got " << inputEventTypeToString(event->getType()) |
| 979 | << " event instead of TOUCH_MODE event"; |
| 980 | |
| 981 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 982 | << mName.c_str() << ": event displayId should always be NONE."; |
| 983 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 984 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 985 | } |
| 986 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 987 | void assertNoEvents() { |
| 988 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 989 | if (event == nullptr) { |
| 990 | return; |
| 991 | } |
| 992 | if (event->getType() == AINPUT_EVENT_TYPE_KEY) { |
| 993 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 994 | ADD_FAILURE() << "Received key event " |
| 995 | << KeyEvent::actionToString(keyEvent.getAction()); |
| 996 | } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) { |
| 997 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 998 | ADD_FAILURE() << "Received motion event " |
| 999 | << MotionEvent::actionToString(motionEvent.getAction()); |
| 1000 | } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) { |
| 1001 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1002 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1003 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1004 | } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) { |
| 1005 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1006 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1007 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1008 | } else if (event->getType() == AINPUT_EVENT_TYPE_TOUCH_MODE) { |
| 1009 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1010 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1011 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1012 | } |
| 1013 | FAIL() << mName.c_str() |
| 1014 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 1018 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1019 | int getChannelFd() { return mConsumer->getChannel()->getFd().get(); } |
| 1020 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1021 | protected: |
| 1022 | std::unique_ptr<InputConsumer> mConsumer; |
| 1023 | PreallocatedInputEventFactory mEventFactory; |
| 1024 | |
| 1025 | std::string mName; |
| 1026 | }; |
| 1027 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1028 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1029 | public: |
| 1030 | static const int32_t WIDTH = 600; |
| 1031 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1032 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1033 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1034 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1035 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1036 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1037 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1038 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1039 | dispatcher->createInputChannel(name); |
| 1040 | token = (*channel)->getConnectionToken(); |
| 1041 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | inputApplicationHandle->updateInfo(); |
| 1045 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1046 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1047 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1048 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1049 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1050 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1051 | mInfo.alpha = 1.0; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1052 | mInfo.frameLeft = 0; |
| 1053 | mInfo.frameTop = 0; |
| 1054 | mInfo.frameRight = WIDTH; |
| 1055 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1056 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1057 | mInfo.globalScaleFactor = 1.0; |
| 1058 | mInfo.touchableRegion.clear(); |
| 1059 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1060 | mInfo.ownerPid = WINDOW_PID; |
| 1061 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1062 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1063 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1064 | } |
| 1065 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1066 | sp<FakeWindowHandle> clone( |
| 1067 | const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1068 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1069 | sp<FakeWindowHandle> handle = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1070 | sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher, |
| 1071 | mInfo.name + "(Mirror)", displayId, mInfo.token); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1072 | return handle; |
| 1073 | } |
| 1074 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1075 | void setTouchable(bool touchable) { |
| 1076 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1077 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1078 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1079 | void setFocusable(bool focusable) { |
| 1080 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1081 | } |
| 1082 | |
| 1083 | void setVisible(bool visible) { |
| 1084 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1085 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1086 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1087 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1088 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1091 | void setPaused(bool paused) { |
| 1092 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1093 | } |
| 1094 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1095 | void setPreventSplitting(bool preventSplitting) { |
| 1096 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | void setSlippery(bool slippery) { |
| 1100 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1101 | } |
| 1102 | |
| 1103 | void setWatchOutsideTouch(bool watchOutside) { |
| 1104 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1105 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1106 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1107 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1108 | |
| 1109 | void setInterceptsStylus(bool interceptsStylus) { |
| 1110 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1111 | } |
| 1112 | |
| 1113 | void setDropInput(bool dropInput) { |
| 1114 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1115 | } |
| 1116 | |
| 1117 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1118 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1119 | } |
| 1120 | |
| 1121 | void setNoInputChannel(bool noInputChannel) { |
| 1122 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1123 | } |
| 1124 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1125 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1126 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1127 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1128 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1129 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1130 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1131 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1132 | mInfo.frameLeft = frame.left; |
| 1133 | mInfo.frameTop = frame.top; |
| 1134 | mInfo.frameRight = frame.right; |
| 1135 | mInfo.frameBottom = frame.bottom; |
| 1136 | mInfo.touchableRegion.clear(); |
| 1137 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1138 | |
| 1139 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1140 | ui::Transform translate; |
| 1141 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1142 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1143 | } |
| 1144 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1145 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1146 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1147 | void setIsWallpaper(bool isWallpaper) { |
| 1148 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1149 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1150 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1151 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1152 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1153 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1154 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1155 | void setTrustedOverlay(bool trustedOverlay) { |
| 1156 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1157 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1158 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1159 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1160 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1161 | } |
| 1162 | |
| 1163 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1164 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1165 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1166 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1167 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1168 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1169 | expectedFlags); |
| 1170 | } |
| 1171 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1172 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1173 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
| 1174 | } |
| 1175 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1176 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1177 | int32_t expectedFlags = 0) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1178 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId, |
| 1179 | expectedFlags); |
| 1180 | } |
| 1181 | |
| 1182 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1183 | int32_t expectedFlags = 0) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1184 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId, |
| 1185 | expectedFlags); |
| 1186 | } |
| 1187 | |
| 1188 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1189 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1190 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1191 | } |
| 1192 | |
| 1193 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1194 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1195 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1196 | expectedFlags); |
| 1197 | } |
| 1198 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1199 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1200 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1201 | int32_t expectedFlags = 0) { |
| 1202 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1203 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1204 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1205 | } |
| 1206 | |
| 1207 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1208 | int32_t expectedFlags = 0) { |
| 1209 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1210 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1211 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1212 | } |
| 1213 | |
| 1214 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1215 | int32_t expectedFlags = 0) { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1216 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
| 1217 | expectedFlags); |
| 1218 | } |
| 1219 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1220 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1221 | int32_t expectedFlags = 0) { |
| 1222 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
| 1223 | expectedFlags); |
| 1224 | } |
| 1225 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1226 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1227 | int32_t expectedFlags = 0) { |
| 1228 | InputEvent* event = consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 1229 | ASSERT_NE(nullptr, event); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1230 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()); |
| 1231 | const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1232 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked()); |
| 1233 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX()); |
| 1234 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY()); |
| 1235 | } |
| 1236 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1237 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1238 | ASSERT_NE(mInputReceiver, nullptr) |
| 1239 | << "Cannot consume events from a window with no receiver"; |
| 1240 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1241 | } |
| 1242 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1243 | void consumeCaptureEvent(bool hasCapture) { |
| 1244 | ASSERT_NE(mInputReceiver, nullptr) |
| 1245 | << "Cannot consume events from a window with no receiver"; |
| 1246 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1247 | } |
| 1248 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1249 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1250 | MotionEvent* motionEvent = consumeMotion(); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 1251 | 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] | 1252 | ASSERT_THAT(*motionEvent, matcher); |
| 1253 | } |
| 1254 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1255 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 1256 | std::optional<int32_t> expectedDisplayId, |
| 1257 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1258 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1259 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1260 | expectedFlags); |
| 1261 | } |
| 1262 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1263 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1264 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1265 | } |
| 1266 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1267 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1268 | ASSERT_NE(mInputReceiver, nullptr) |
| 1269 | << "Cannot consume events from a window with no receiver"; |
| 1270 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1271 | } |
| 1272 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1273 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1274 | if (mInputReceiver == nullptr) { |
| 1275 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1276 | return std::nullopt; |
| 1277 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1278 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | void finishEvent(uint32_t sequenceNum) { |
| 1282 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1283 | mInputReceiver->finishEvent(sequenceNum); |
| 1284 | } |
| 1285 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1286 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1287 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1288 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1289 | } |
| 1290 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1291 | InputEvent* consume() { |
| 1292 | if (mInputReceiver == nullptr) { |
| 1293 | return nullptr; |
| 1294 | } |
| 1295 | return mInputReceiver->consume(); |
| 1296 | } |
| 1297 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1298 | MotionEvent* consumeMotion() { |
| 1299 | InputEvent* event = consume(); |
| 1300 | if (event == nullptr) { |
| 1301 | ADD_FAILURE() << "Consume failed : no event"; |
| 1302 | return nullptr; |
| 1303 | } |
| 1304 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 1305 | ADD_FAILURE() << "Instead of motion event, got " |
| 1306 | << inputEventTypeToString(event->getType()); |
| 1307 | return nullptr; |
| 1308 | } |
| 1309 | return static_cast<MotionEvent*>(event); |
| 1310 | } |
| 1311 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1312 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1313 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1314 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1315 | return; // Can't receive events if the window does not have input channel |
| 1316 | } |
| 1317 | ASSERT_NE(nullptr, mInputReceiver) |
| 1318 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1319 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1320 | } |
| 1321 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1322 | sp<IBinder> getToken() { return mInfo.token; } |
| 1323 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1324 | const std::string& getName() { return mName; } |
| 1325 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1326 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { |
| 1327 | mInfo.ownerPid = ownerPid; |
| 1328 | mInfo.ownerUid = ownerUid; |
| 1329 | } |
| 1330 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1331 | int32_t getPid() const { return mInfo.ownerPid; } |
| 1332 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1333 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1334 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1335 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1336 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1337 | private: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1338 | const std::string mName; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1339 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1340 | 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] | 1341 | }; |
| 1342 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1343 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1344 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1345 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1346 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1347 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1348 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1349 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1350 | bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {}, |
| 1351 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1352 | KeyEvent event; |
| 1353 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1354 | |
| 1355 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1356 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1357 | INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE, |
| 1358 | repeatCount, currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1359 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1360 | if (!allowKeyRepeat) { |
| 1361 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1362 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1363 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1364 | return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1365 | } |
| 1366 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1367 | static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1368 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1369 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId); |
| 1370 | } |
| 1371 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1372 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1373 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1374 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1375 | static InputEventInjectionResult injectKeyDownNoRepeat( |
| 1376 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1377 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId, |
| 1378 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
| 1379 | /* allowKeyRepeat */ false); |
| 1380 | } |
| 1381 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1382 | static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1383 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1384 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId); |
| 1385 | } |
| 1386 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1387 | class PointerBuilder { |
| 1388 | public: |
| 1389 | PointerBuilder(int32_t id, int32_t toolType) { |
| 1390 | mProperties.clear(); |
| 1391 | mProperties.id = id; |
| 1392 | mProperties.toolType = toolType; |
| 1393 | mCoords.clear(); |
| 1394 | } |
| 1395 | |
| 1396 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } |
| 1397 | |
| 1398 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } |
| 1399 | |
| 1400 | PointerBuilder& axis(int32_t axis, float value) { |
| 1401 | mCoords.setAxisValue(axis, value); |
| 1402 | return *this; |
| 1403 | } |
| 1404 | |
| 1405 | PointerProperties buildProperties() const { return mProperties; } |
| 1406 | |
| 1407 | PointerCoords buildCoords() const { return mCoords; } |
| 1408 | |
| 1409 | private: |
| 1410 | PointerProperties mProperties; |
| 1411 | PointerCoords mCoords; |
| 1412 | }; |
| 1413 | |
| 1414 | class MotionEventBuilder { |
| 1415 | public: |
| 1416 | MotionEventBuilder(int32_t action, int32_t source) { |
| 1417 | mAction = action; |
| 1418 | mSource = source; |
| 1419 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1420 | } |
| 1421 | |
| 1422 | MotionEventBuilder& eventTime(nsecs_t eventTime) { |
| 1423 | mEventTime = eventTime; |
| 1424 | return *this; |
| 1425 | } |
| 1426 | |
| 1427 | MotionEventBuilder& displayId(int32_t displayId) { |
| 1428 | mDisplayId = displayId; |
| 1429 | return *this; |
| 1430 | } |
| 1431 | |
| 1432 | MotionEventBuilder& actionButton(int32_t actionButton) { |
| 1433 | mActionButton = actionButton; |
| 1434 | return *this; |
| 1435 | } |
| 1436 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1437 | MotionEventBuilder& buttonState(int32_t buttonState) { |
| 1438 | mButtonState = buttonState; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1439 | return *this; |
| 1440 | } |
| 1441 | |
| 1442 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 1443 | mRawXCursorPosition = rawXCursorPosition; |
| 1444 | return *this; |
| 1445 | } |
| 1446 | |
| 1447 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 1448 | mRawYCursorPosition = rawYCursorPosition; |
| 1449 | return *this; |
| 1450 | } |
| 1451 | |
| 1452 | MotionEventBuilder& pointer(PointerBuilder pointer) { |
| 1453 | mPointers.push_back(pointer); |
| 1454 | return *this; |
| 1455 | } |
| 1456 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1457 | MotionEventBuilder& addFlag(uint32_t flags) { |
| 1458 | mFlags |= flags; |
| 1459 | return *this; |
| 1460 | } |
| 1461 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1462 | MotionEvent build() { |
| 1463 | std::vector<PointerProperties> pointerProperties; |
| 1464 | std::vector<PointerCoords> pointerCoords; |
| 1465 | for (const PointerBuilder& pointer : mPointers) { |
| 1466 | pointerProperties.push_back(pointer.buildProperties()); |
| 1467 | pointerCoords.push_back(pointer.buildCoords()); |
| 1468 | } |
| 1469 | |
| 1470 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 1471 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 1472 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 1473 | mPointers.size() == 1) { |
| 1474 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 1475 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 1476 | } |
| 1477 | |
| 1478 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1479 | ui::Transform identityTransform; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1480 | event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC, |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1481 | mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1482 | mButtonState, MotionClassification::NONE, identityTransform, |
| 1483 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 1484 | mRawYCursorPosition, identityTransform, mEventTime, mEventTime, |
| 1485 | mPointers.size(), pointerProperties.data(), pointerCoords.data()); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1486 | |
| 1487 | return event; |
| 1488 | } |
| 1489 | |
| 1490 | private: |
| 1491 | int32_t mAction; |
| 1492 | int32_t mSource; |
| 1493 | nsecs_t mEventTime; |
| 1494 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1495 | int32_t mActionButton{0}; |
| 1496 | int32_t mButtonState{0}; |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1497 | int32_t mFlags{0}; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1498 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1499 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1500 | |
| 1501 | std::vector<PointerBuilder> mPointers; |
| 1502 | }; |
| 1503 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1504 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1505 | const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1506 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1507 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
| 1508 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
| 1509 | return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1510 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1513 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1514 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1515 | int32_t displayId, const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1516 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1517 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1518 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1519 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1520 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
| 1521 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1522 | MotionEvent event = MotionEventBuilder(action, source) |
| 1523 | .displayId(displayId) |
| 1524 | .eventTime(eventTime) |
| 1525 | .rawXCursorPosition(cursorPosition.x) |
| 1526 | .rawYCursorPosition(cursorPosition.y) |
| 1527 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1528 | .x(position.x) |
| 1529 | .y(position.y)) |
| 1530 | .build(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1531 | |
| 1532 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1533 | return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid, |
| 1534 | policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1535 | } |
| 1536 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1537 | static InputEventInjectionResult injectMotionDown( |
| 1538 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId, |
| 1539 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1540 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1543 | static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1544 | int32_t source, int32_t displayId, |
| 1545 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1546 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1549 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1550 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1551 | // Define a valid key event. |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1552 | NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 1553 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, |
| 1554 | KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1555 | |
| 1556 | return args; |
| 1557 | } |
| 1558 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1559 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, |
| 1560 | const std::vector<PointF>& points) { |
| 1561 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1562 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1563 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1564 | } |
| 1565 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1566 | PointerProperties pointerProperties[pointerCount]; |
| 1567 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1568 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1569 | for (size_t i = 0; i < pointerCount; i++) { |
| 1570 | pointerProperties[i].clear(); |
| 1571 | pointerProperties[i].id = i; |
| 1572 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1573 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1574 | pointerCoords[i].clear(); |
| 1575 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1576 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1577 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1578 | |
| 1579 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1580 | // Define a valid motion event. |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1581 | NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1582 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 1583 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1584 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 1585 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1586 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 1587 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1588 | |
| 1589 | return args; |
| 1590 | } |
| 1591 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1592 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1593 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1594 | } |
| 1595 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1596 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1597 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1598 | } |
| 1599 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1600 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1601 | const PointerCaptureRequest& request) { |
| 1602 | return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1603 | } |
| 1604 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1605 | /** |
| 1606 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1607 | * broken channel. |
| 1608 | */ |
| 1609 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1610 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1611 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1612 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1613 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1614 | |
| 1615 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1616 | |
| 1617 | // Window closes its channel, but the window remains. |
| 1618 | window->destroyReceiver(); |
| 1619 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1620 | } |
| 1621 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1622 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1623 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1624 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1625 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1626 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1627 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1628 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1629 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1630 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1631 | |
| 1632 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1633 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1634 | } |
| 1635 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1636 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1637 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1638 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1639 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1640 | |
| 1641 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1642 | // Inject a MotionEvent to an unknown display. |
| 1643 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1644 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
| 1645 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1646 | |
| 1647 | // Window should receive motion event. |
| 1648 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1649 | } |
| 1650 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1651 | /** |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1652 | * Calling setInputWindows once should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1653 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1654 | * called twice. |
| 1655 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1656 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1657 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1658 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1659 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1660 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1661 | |
| 1662 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1663 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1664 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1665 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1666 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1667 | |
| 1668 | // Window should receive motion event. |
| 1669 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1670 | } |
| 1671 | |
| 1672 | /** |
| 1673 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1674 | */ |
| 1675 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1676 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1677 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1678 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1679 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1680 | |
| 1681 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1682 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1683 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1684 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1685 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1686 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1687 | |
| 1688 | // Window should receive motion event. |
| 1689 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1690 | } |
| 1691 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1692 | // The foreground window should receive the first touch down event. |
| 1693 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1694 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1695 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1696 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1697 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1698 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1699 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1700 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1701 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1702 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1703 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1704 | |
| 1705 | // 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] | 1706 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1707 | windowSecond->assertNoEvents(); |
| 1708 | } |
| 1709 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1710 | /** |
| 1711 | * Two windows: A top window, and a wallpaper behind the window. |
| 1712 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1713 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1714 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1715 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1716 | */ |
| 1717 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1718 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1719 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1720 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1721 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1722 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1723 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1724 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1725 | |
| 1726 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1727 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1728 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1729 | {100, 200})) |
| 1730 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1731 | |
| 1732 | // Both foreground window and its wallpaper should receive the touch down |
| 1733 | foregroundWindow->consumeMotionDown(); |
| 1734 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1735 | |
| 1736 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1737 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1738 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1739 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1740 | |
| 1741 | foregroundWindow->consumeMotionMove(); |
| 1742 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1743 | |
| 1744 | // Now the foreground window goes away, but the wallpaper stays |
| 1745 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1746 | foregroundWindow->consumeMotionCancel(); |
| 1747 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1748 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1749 | } |
| 1750 | |
| 1751 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1752 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1753 | * with the following differences: |
| 1754 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1755 | * clean up the connection. |
| 1756 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1757 | * Ensure that there's no crash in the dispatcher. |
| 1758 | */ |
| 1759 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1760 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1761 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1762 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1763 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1764 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1765 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1766 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1767 | |
| 1768 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1769 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1770 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1771 | {100, 200})) |
| 1772 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1773 | |
| 1774 | // Both foreground window and its wallpaper should receive the touch down |
| 1775 | foregroundWindow->consumeMotionDown(); |
| 1776 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1777 | |
| 1778 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1779 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1780 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1781 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1782 | |
| 1783 | foregroundWindow->consumeMotionMove(); |
| 1784 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1785 | |
| 1786 | // Wallpaper closes its channel, but the window remains. |
| 1787 | wallpaperWindow->destroyReceiver(); |
| 1788 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1789 | |
| 1790 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1791 | // is no longer valid. |
| 1792 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1793 | foregroundWindow->consumeMotionCancel(); |
| 1794 | } |
| 1795 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 1796 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1797 | public ::testing::WithParamInterface<bool> {}; |
| 1798 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1799 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1800 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1801 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1802 | * The top window gets a multitouch gesture. |
| 1803 | * Ensure that wallpaper gets the same gesture. |
| 1804 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 1805 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1806 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 1807 | sp<FakeWindowHandle> foregroundWindow = |
| 1808 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 1809 | foregroundWindow->setDupTouchToWallpaper(true); |
| 1810 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1811 | |
| 1812 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1813 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1814 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1815 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 1816 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1817 | |
| 1818 | // Touch down on top window |
| 1819 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1820 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1821 | {100, 100})) |
| 1822 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1823 | |
| 1824 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 1825 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1826 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1827 | |
| 1828 | // Second finger down on the top window |
| 1829 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1830 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1831 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1832 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1833 | .x(100) |
| 1834 | .y(100)) |
| 1835 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1836 | .x(150) |
| 1837 | .y(150)) |
| 1838 | .build(); |
| 1839 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1840 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1841 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1842 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1843 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 1844 | foregroundWindow->consumeMotionPointerDown(1 /* pointerIndex */); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1845 | wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT, |
| 1846 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 1847 | |
| 1848 | const MotionEvent secondFingerUpEvent = |
| 1849 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1850 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1851 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1852 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1853 | .x(100) |
| 1854 | .y(100)) |
| 1855 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1856 | .x(150) |
| 1857 | .y(150)) |
| 1858 | .build(); |
| 1859 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1860 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 1861 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1862 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1863 | foregroundWindow->consumeMotionPointerUp(0); |
| 1864 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1865 | |
| 1866 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1867 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1868 | {100, 100})) |
| 1869 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1870 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1871 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | /** |
| 1875 | * Two windows: a window on the left and window on the right. |
| 1876 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1877 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1878 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1879 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1880 | * ACTION_POINTER_DOWN(1). |
| 1881 | */ |
| 1882 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1883 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1884 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1885 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1886 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1887 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1888 | |
| 1889 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1890 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1891 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1892 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1893 | |
| 1894 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1895 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1896 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1897 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1898 | |
| 1899 | mDispatcher->setInputWindows( |
| 1900 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
| 1901 | |
| 1902 | // Touch down on left window |
| 1903 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1904 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1905 | {100, 100})) |
| 1906 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1907 | |
| 1908 | // Both foreground window and its wallpaper should receive the touch down |
| 1909 | leftWindow->consumeMotionDown(); |
| 1910 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1911 | |
| 1912 | // Second finger down on the right window |
| 1913 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1914 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1915 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1916 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1917 | .x(100) |
| 1918 | .y(100)) |
| 1919 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1920 | .x(300) |
| 1921 | .y(100)) |
| 1922 | .build(); |
| 1923 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1924 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1925 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1926 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1927 | |
| 1928 | leftWindow->consumeMotionMove(); |
| 1929 | // Since the touch is split, right window gets ACTION_DOWN |
| 1930 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1931 | wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT, |
| 1932 | expectedWallpaperFlags); |
| 1933 | |
| 1934 | // Now, leftWindow, which received the first finger, disappears. |
| 1935 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); |
| 1936 | leftWindow->consumeMotionCancel(); |
| 1937 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1938 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1939 | |
| 1940 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 1941 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 1942 | const MotionEvent secondFingerMoveEvent = |
| 1943 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1944 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1945 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1946 | .x(100) |
| 1947 | .y(100)) |
| 1948 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1949 | .x(310) |
| 1950 | .y(110)) |
| 1951 | .build(); |
| 1952 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1953 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 1954 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 1955 | rightWindow->consumeMotionMove(); |
| 1956 | |
| 1957 | leftWindow->assertNoEvents(); |
| 1958 | rightWindow->assertNoEvents(); |
| 1959 | wallpaperWindow->assertNoEvents(); |
| 1960 | } |
| 1961 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 1962 | /** |
| 1963 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 1964 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 1965 | * The right window should receive ACTION_DOWN. |
| 1966 | */ |
| 1967 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1968 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 1969 | sp<FakeWindowHandle> leftWindow = |
| 1970 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1971 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1972 | leftWindow->setDupTouchToWallpaper(true); |
| 1973 | leftWindow->setSlippery(true); |
| 1974 | |
| 1975 | sp<FakeWindowHandle> rightWindow = |
| 1976 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1977 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1978 | |
| 1979 | sp<FakeWindowHandle> wallpaperWindow = |
| 1980 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 1981 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1982 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 1983 | mDispatcher->setInputWindows( |
| 1984 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1985 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 1986 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1987 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1988 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 1989 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1990 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 1991 | |
| 1992 | // Both foreground window and its wallpaper should receive the touch down |
| 1993 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1994 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1995 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 1996 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1997 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 1998 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1999 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2000 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2001 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 2002 | leftWindow->consumeMotionCancel(); |
| 2003 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2004 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2007 | /** |
| 2008 | * On the display, have a single window, and also an area where there's no window. |
| 2009 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 2010 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 2011 | */ |
| 2012 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 2013 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2014 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2015 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2016 | |
| 2017 | mDispatcher->setInputWindows({{DISPLAY_ID, {window}}}); |
| 2018 | NotifyMotionArgs args; |
| 2019 | |
| 2020 | // Touch down on the empty space |
| 2021 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}))); |
| 2022 | |
| 2023 | mDispatcher->waitForIdle(); |
| 2024 | window->assertNoEvents(); |
| 2025 | |
| 2026 | // Now touch down on the window with another pointer |
| 2027 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}))); |
| 2028 | mDispatcher->waitForIdle(); |
| 2029 | window->consumeMotionDown(); |
| 2030 | } |
| 2031 | |
| 2032 | /** |
| 2033 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 2034 | * non-touchable window. |
| 2035 | */ |
| 2036 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 2037 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2038 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2039 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2040 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2041 | window1->setTouchable(false); |
| 2042 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2043 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2044 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2045 | |
| 2046 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2047 | |
| 2048 | NotifyMotionArgs args; |
| 2049 | // Touch down on the non-touchable window |
| 2050 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); |
| 2051 | |
| 2052 | mDispatcher->waitForIdle(); |
| 2053 | window1->assertNoEvents(); |
| 2054 | window2->assertNoEvents(); |
| 2055 | |
| 2056 | // Now touch down on the window with another pointer |
| 2057 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); |
| 2058 | mDispatcher->waitForIdle(); |
| 2059 | window2->consumeMotionDown(); |
| 2060 | } |
| 2061 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2062 | /** |
| 2063 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 2064 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 2065 | */ |
| 2066 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 2067 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2068 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2069 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2070 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2071 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2072 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2073 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2074 | |
| 2075 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2076 | |
| 2077 | NotifyMotionArgs args; |
| 2078 | // Touch down on the first window |
| 2079 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); |
| 2080 | |
| 2081 | mDispatcher->waitForIdle(); |
| 2082 | InputEvent* inputEvent1 = window1->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2083 | ASSERT_NE(inputEvent1, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2084 | window2->assertNoEvents(); |
| 2085 | MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1); |
| 2086 | nsecs_t downTimeForWindow1 = motionEvent1.getDownTime(); |
| 2087 | ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime()); |
| 2088 | |
| 2089 | // Now touch down on the window with another pointer |
| 2090 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); |
| 2091 | mDispatcher->waitForIdle(); |
| 2092 | InputEvent* inputEvent2 = window2->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2093 | ASSERT_NE(inputEvent2, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2094 | MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2); |
| 2095 | nsecs_t downTimeForWindow2 = motionEvent2.getDownTime(); |
| 2096 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
| 2097 | ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime()); |
| 2098 | |
| 2099 | // Now move the pointer on the second window |
| 2100 | mDispatcher->notifyMotion( |
| 2101 | &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}))); |
| 2102 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2103 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2104 | |
| 2105 | // Now add new touch down on the second window |
| 2106 | mDispatcher->notifyMotion( |
| 2107 | &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}))); |
| 2108 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2109 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2110 | |
| 2111 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 2112 | window1->consumeMotionMove(); |
| 2113 | window1->assertNoEvents(); |
| 2114 | |
| 2115 | // Now move the pointer on the first window |
| 2116 | mDispatcher->notifyMotion( |
| 2117 | &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}}))); |
| 2118 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2119 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2120 | |
| 2121 | mDispatcher->notifyMotion(&( |
| 2122 | args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}))); |
| 2123 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2124 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2127 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2128 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2129 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2130 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2131 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2132 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2133 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2134 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2135 | |
| 2136 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2137 | |
| 2138 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 2139 | |
| 2140 | // 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] | 2141 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2142 | injectMotionEvent(mDispatcher, |
| 2143 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2144 | AINPUT_SOURCE_MOUSE) |
| 2145 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2146 | .x(900) |
| 2147 | .y(400)) |
| 2148 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2149 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2150 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2151 | |
| 2152 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2153 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2154 | injectMotionEvent(mDispatcher, |
| 2155 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2156 | AINPUT_SOURCE_MOUSE) |
| 2157 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2158 | .x(300) |
| 2159 | .y(400)) |
| 2160 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2161 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2162 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2163 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2164 | |
| 2165 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2166 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2167 | injectMotionEvent(mDispatcher, |
| 2168 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2169 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2170 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2171 | .x(300) |
| 2172 | .y(400)) |
| 2173 | .build())); |
| 2174 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2175 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2176 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2177 | injectMotionEvent(mDispatcher, |
| 2178 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 2179 | AINPUT_SOURCE_MOUSE) |
| 2180 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2181 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2182 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2183 | .x(300) |
| 2184 | .y(400)) |
| 2185 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2186 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2187 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2188 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2189 | injectMotionEvent(mDispatcher, |
| 2190 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2191 | AINPUT_SOURCE_MOUSE) |
| 2192 | .buttonState(0) |
| 2193 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2194 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2195 | .x(300) |
| 2196 | .y(400)) |
| 2197 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2198 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2199 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2200 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2201 | injectMotionEvent(mDispatcher, |
| 2202 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 2203 | .buttonState(0) |
| 2204 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2205 | .x(300) |
| 2206 | .y(400)) |
| 2207 | .build())); |
| 2208 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2209 | |
| 2210 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2211 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2212 | injectMotionEvent(mDispatcher, |
| 2213 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2214 | AINPUT_SOURCE_MOUSE) |
| 2215 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2216 | .x(900) |
| 2217 | .y(400)) |
| 2218 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2219 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2220 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2221 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2222 | |
| 2223 | // No more events |
| 2224 | windowLeft->assertNoEvents(); |
| 2225 | windowRight->assertNoEvents(); |
| 2226 | } |
| 2227 | |
| 2228 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 2229 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2230 | |
| 2231 | sp<FakeWindowHandle> spyWindow = |
| 2232 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2233 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 2234 | spyWindow->setTrustedOverlay(true); |
| 2235 | spyWindow->setSpy(true); |
| 2236 | sp<FakeWindowHandle> window = |
| 2237 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2238 | window->setFrame(Rect(0, 0, 600, 800)); |
| 2239 | |
| 2240 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2241 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2242 | |
| 2243 | // Send mouse cursor to the window |
| 2244 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2245 | injectMotionEvent(mDispatcher, |
| 2246 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2247 | AINPUT_SOURCE_MOUSE) |
| 2248 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2249 | .x(100) |
| 2250 | .y(100)) |
| 2251 | .build())); |
| 2252 | |
| 2253 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2254 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2255 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2256 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2257 | |
| 2258 | window->assertNoEvents(); |
| 2259 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 2263 | // directly in this test. |
| 2264 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2265 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2266 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2267 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2268 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2269 | |
| 2270 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2271 | |
| 2272 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2273 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2274 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2275 | injectMotionEvent(mDispatcher, |
| 2276 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2277 | AINPUT_SOURCE_MOUSE) |
| 2278 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2279 | .x(300) |
| 2280 | .y(400)) |
| 2281 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2282 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2283 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2284 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2285 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2286 | injectMotionEvent(mDispatcher, |
| 2287 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2288 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2289 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2290 | .x(300) |
| 2291 | .y(400)) |
| 2292 | .build())); |
| 2293 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2294 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2295 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2296 | injectMotionEvent(mDispatcher, |
| 2297 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 2298 | AINPUT_SOURCE_MOUSE) |
| 2299 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2300 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2301 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2302 | .x(300) |
| 2303 | .y(400)) |
| 2304 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2305 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2306 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2307 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2308 | injectMotionEvent(mDispatcher, |
| 2309 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2310 | AINPUT_SOURCE_MOUSE) |
| 2311 | .buttonState(0) |
| 2312 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2313 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2314 | .x(300) |
| 2315 | .y(400)) |
| 2316 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2317 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2318 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2319 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2320 | injectMotionEvent(mDispatcher, |
| 2321 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 2322 | .buttonState(0) |
| 2323 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2324 | .x(300) |
| 2325 | .y(400)) |
| 2326 | .build())); |
| 2327 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2328 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2329 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2330 | injectMotionEvent(mDispatcher, |
| 2331 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 2332 | AINPUT_SOURCE_MOUSE) |
| 2333 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2334 | .x(300) |
| 2335 | .y(400)) |
| 2336 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2337 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2338 | } |
| 2339 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2340 | /** |
| 2341 | * Inject a mouse hover event followed by a tap from touchscreen. |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2342 | * In the current implementation, the tap does not cause a HOVER_EXIT event. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2343 | */ |
| 2344 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 2345 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2346 | sp<FakeWindowHandle> window = |
| 2347 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2348 | window->setFrame(Rect(0, 0, 100, 100)); |
| 2349 | |
| 2350 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2351 | |
| 2352 | // Inject a hover_move from mouse. |
| 2353 | NotifyMotionArgs motionArgs = |
| 2354 | generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE, |
| 2355 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 2356 | motionArgs.xCursorPosition = 50; |
| 2357 | motionArgs.yCursorPosition = 50; |
| 2358 | mDispatcher->notifyMotion(&motionArgs); |
| 2359 | ASSERT_NO_FATAL_FAILURE( |
| 2360 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2361 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2362 | ASSERT_NO_FATAL_FAILURE( |
| 2363 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 2364 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2365 | |
| 2366 | // Tap on the window |
| 2367 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2368 | ADISPLAY_ID_DEFAULT, {{10, 10}}); |
| 2369 | mDispatcher->notifyMotion(&motionArgs); |
| 2370 | ASSERT_NO_FATAL_FAILURE( |
| 2371 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2372 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 2373 | |
| 2374 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2375 | ADISPLAY_ID_DEFAULT, {{10, 10}}); |
| 2376 | mDispatcher->notifyMotion(&motionArgs); |
| 2377 | ASSERT_NO_FATAL_FAILURE( |
| 2378 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 2379 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 2380 | } |
| 2381 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2382 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 2383 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2384 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 2385 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 2386 | ADISPLAY_ID_DEFAULT); |
| 2387 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 2388 | sp<FakeWindowHandle> windowSecondDisplay = |
| 2389 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 2390 | SECOND_DISPLAY_ID); |
| 2391 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 2392 | |
| 2393 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 2394 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 2395 | |
| 2396 | // Set cursor position in window in default display and check that hover enter and move |
| 2397 | // events are generated. |
| 2398 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2399 | injectMotionEvent(mDispatcher, |
| 2400 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2401 | AINPUT_SOURCE_MOUSE) |
| 2402 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2403 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2404 | .x(300) |
| 2405 | .y(600)) |
| 2406 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2407 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2408 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2409 | |
| 2410 | // Remove all windows in secondary display and check that no event happens on window in |
| 2411 | // primary display. |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2412 | mDispatcher->setInputWindows( |
| 2413 | {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2414 | windowDefaultDisplay->assertNoEvents(); |
| 2415 | |
| 2416 | // Move cursor position in window in default display and check that only hover move |
| 2417 | // event is generated and not hover enter event. |
| 2418 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 2419 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 2420 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2421 | injectMotionEvent(mDispatcher, |
| 2422 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2423 | AINPUT_SOURCE_MOUSE) |
| 2424 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2425 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2426 | .x(400) |
| 2427 | .y(700)) |
| 2428 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2429 | windowDefaultDisplay->consumeMotionEvent( |
| 2430 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 2431 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2432 | windowDefaultDisplay->assertNoEvents(); |
| 2433 | } |
| 2434 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2435 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2436 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2437 | |
| 2438 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2439 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2440 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2441 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2442 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2443 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2444 | |
| 2445 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2446 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2447 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2448 | |
| 2449 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 2450 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2451 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2452 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2453 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2454 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2455 | windowRight->assertNoEvents(); |
| 2456 | } |
| 2457 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2458 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2459 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2460 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2461 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2462 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2463 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2464 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2465 | setFocusedWindow(window); |
| 2466 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2467 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2468 | |
| 2469 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2470 | mDispatcher->notifyKey(&keyArgs); |
| 2471 | |
| 2472 | // Window should receive key down event. |
| 2473 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2474 | |
| 2475 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 2476 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2477 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2478 | mDispatcher->notifyDeviceReset(&args); |
| 2479 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 2480 | AKEY_EVENT_FLAG_CANCELED); |
| 2481 | } |
| 2482 | |
| 2483 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2484 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2485 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2486 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2487 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2488 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2489 | |
| 2490 | NotifyMotionArgs motionArgs = |
| 2491 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2492 | ADISPLAY_ID_DEFAULT); |
| 2493 | mDispatcher->notifyMotion(&motionArgs); |
| 2494 | |
| 2495 | // Window should receive motion down event. |
| 2496 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2497 | |
| 2498 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 2499 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2500 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2501 | mDispatcher->notifyDeviceReset(&args); |
| 2502 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, |
| 2503 | 0 /*expectedFlags*/); |
| 2504 | } |
| 2505 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2506 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 2507 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2508 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2509 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2510 | window->setFocusable(true); |
| 2511 | |
| 2512 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2513 | setFocusedWindow(window); |
| 2514 | |
| 2515 | window->consumeFocusEvent(true); |
| 2516 | |
| 2517 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2518 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 2519 | const nsecs_t injectTime = keyArgs.eventTime; |
| 2520 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
| 2521 | mDispatcher->notifyKey(&keyArgs); |
| 2522 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 2523 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2524 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 2525 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 2526 | } |
| 2527 | |
| 2528 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 2529 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2530 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2531 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2532 | window->setFocusable(true); |
| 2533 | |
| 2534 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2535 | setFocusedWindow(window); |
| 2536 | |
| 2537 | window->consumeFocusEvent(true); |
| 2538 | |
| 2539 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2540 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 2541 | mFakePolicy->setInterceptKeyTimeout(150ms); |
| 2542 | mDispatcher->notifyKey(&keyDown); |
| 2543 | mDispatcher->notifyKey(&keyUp); |
| 2544 | |
| 2545 | // Window should receive key event immediately when same key up. |
| 2546 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2547 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 2548 | } |
| 2549 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2550 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2551 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 2552 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 2553 | * ACTION_OUTSIDE event is sent per gesture. |
| 2554 | */ |
| 2555 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 2556 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 2557 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2558 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2559 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2560 | window->setWatchOutsideTouch(true); |
| 2561 | window->setFrame(Rect{0, 0, 100, 100}); |
| 2562 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2563 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 2564 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2565 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 2566 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2567 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 2568 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2569 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
| 2570 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}}); |
| 2571 | |
| 2572 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
| 2573 | NotifyMotionArgs motionArgs = |
| 2574 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2575 | ADISPLAY_ID_DEFAULT, {PointF{-10, -10}}); |
| 2576 | mDispatcher->notifyMotion(&motionArgs); |
| 2577 | window->assertNoEvents(); |
| 2578 | secondWindow->assertNoEvents(); |
| 2579 | |
| 2580 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 2581 | // Now, `window` should get ACTION_OUTSIDE. |
| 2582 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2583 | {PointF{-10, -10}, PointF{105, 105}}); |
| 2584 | mDispatcher->notifyMotion(&motionArgs); |
| 2585 | window->consumeMotionOutside(); |
| 2586 | secondWindow->consumeMotionDown(); |
| 2587 | thirdWindow->assertNoEvents(); |
| 2588 | |
| 2589 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 2590 | // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture. |
| 2591 | motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2592 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}); |
| 2593 | mDispatcher->notifyMotion(&motionArgs); |
| 2594 | window->assertNoEvents(); |
| 2595 | secondWindow->consumeMotionMove(); |
| 2596 | thirdWindow->consumeMotionDown(); |
| 2597 | } |
| 2598 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 2599 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 2600 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2601 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2602 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 2603 | window->setFocusable(true); |
| 2604 | |
| 2605 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); |
| 2606 | setFocusedWindow(window); |
| 2607 | |
| 2608 | window->consumeFocusEvent(true); |
| 2609 | |
| 2610 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2611 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 2612 | mDispatcher->notifyKey(&keyDown); |
| 2613 | mDispatcher->notifyKey(&keyUp); |
| 2614 | |
| 2615 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2616 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 2617 | |
| 2618 | // All windows are removed from the display. Ensure that we can no longer dispatch to it. |
| 2619 | mDispatcher->onWindowInfosChanged({}, {}); |
| 2620 | |
| 2621 | window->consumeFocusEvent(false); |
| 2622 | |
| 2623 | mDispatcher->notifyKey(&keyDown); |
| 2624 | mDispatcher->notifyKey(&keyUp); |
| 2625 | window->assertNoEvents(); |
| 2626 | } |
| 2627 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2628 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 2629 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2630 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2631 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2632 | // Ensure window is non-split and have some transform. |
| 2633 | window->setPreventSplitting(true); |
| 2634 | window->setWindowOffset(20, 40); |
| 2635 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); |
| 2636 | |
| 2637 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2638 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2639 | {50, 50})) |
| 2640 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2641 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2642 | |
| 2643 | const MotionEvent secondFingerDownEvent = |
| 2644 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2645 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2646 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 2647 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 2648 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2649 | .x(-30) |
| 2650 | .y(-50)) |
| 2651 | .build(); |
| 2652 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2653 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 2654 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2655 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2656 | |
| 2657 | const MotionEvent* event = window->consumeMotion(); |
| 2658 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 2659 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 2660 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 2661 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 2662 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
| 2663 | } |
| 2664 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2665 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2666 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 2667 | * |
| 2668 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 2669 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 2670 | * space. |
| 2671 | */ |
| 2672 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 2673 | public: |
| 2674 | void SetUp() override { |
| 2675 | InputDispatcherTest::SetUp(); |
| 2676 | mDisplayInfos.clear(); |
| 2677 | mWindowInfos.clear(); |
| 2678 | } |
| 2679 | |
| 2680 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 2681 | gui::DisplayInfo info; |
| 2682 | info.displayId = displayId; |
| 2683 | info.transform = transform; |
| 2684 | mDisplayInfos.push_back(std::move(info)); |
| 2685 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 2686 | } |
| 2687 | |
| 2688 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 2689 | mWindowInfos.push_back(*windowHandle->getInfo()); |
| 2690 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 2691 | } |
| 2692 | |
| 2693 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 2694 | // on the display. |
| 2695 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 2696 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 2697 | // respectively. |
| 2698 | ui::Transform displayTransform; |
| 2699 | displayTransform.set(2, 0, 0, 4); |
| 2700 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 2701 | |
| 2702 | std::shared_ptr<FakeApplicationHandle> application = |
| 2703 | std::make_shared<FakeApplicationHandle>(); |
| 2704 | |
| 2705 | // Add two windows to the display. Their frames are represented in the display space. |
| 2706 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2707 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 2708 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2709 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 2710 | addWindow(firstWindow); |
| 2711 | |
| 2712 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2713 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 2714 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2715 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 2716 | addWindow(secondWindow); |
| 2717 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 2718 | } |
| 2719 | |
| 2720 | private: |
| 2721 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 2722 | std::vector<gui::WindowInfo> mWindowInfos; |
| 2723 | }; |
| 2724 | |
| 2725 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) { |
| 2726 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2727 | // Send down to the first window. The point is represented in the display space. The point is |
| 2728 | // selected so that if the hit test was done with the transform applied to it, then it would |
| 2729 | // end up in the incorrect window. |
| 2730 | NotifyMotionArgs downMotionArgs = |
| 2731 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2732 | ADISPLAY_ID_DEFAULT, {PointF{75, 55}}); |
| 2733 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2734 | |
| 2735 | firstWindow->consumeMotionDown(); |
| 2736 | secondWindow->assertNoEvents(); |
| 2737 | } |
| 2738 | |
| 2739 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 2740 | // the event should be treated as being in the logical display space. |
| 2741 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 2742 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2743 | // Send down to the first window. The point is represented in the logical display space. The |
| 2744 | // point is selected so that if the hit test was done in logical display space, then it would |
| 2745 | // end up in the incorrect window. |
| 2746 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2747 | PointF{75 * 2, 55 * 4}); |
| 2748 | |
| 2749 | firstWindow->consumeMotionDown(); |
| 2750 | secondWindow->assertNoEvents(); |
| 2751 | } |
| 2752 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 2753 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 2754 | // event should be treated as being in the logical display space. |
| 2755 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 2756 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2757 | |
| 2758 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 2759 | ui::Transform injectedEventTransform; |
| 2760 | injectedEventTransform.set(matrix); |
| 2761 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 2762 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 2763 | |
| 2764 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2765 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2766 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 2767 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2768 | .x(untransformedPoint.x) |
| 2769 | .y(untransformedPoint.y)) |
| 2770 | .build(); |
| 2771 | event.transform(matrix); |
| 2772 | |
| 2773 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, |
| 2774 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 2775 | |
| 2776 | firstWindow->consumeMotionDown(); |
| 2777 | secondWindow->assertNoEvents(); |
| 2778 | } |
| 2779 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2780 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 2781 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2782 | |
| 2783 | // Send down to the second window. |
| 2784 | NotifyMotionArgs downMotionArgs = |
| 2785 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2786 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}}); |
| 2787 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2788 | |
| 2789 | firstWindow->assertNoEvents(); |
| 2790 | const MotionEvent* event = secondWindow->consumeMotion(); |
| 2791 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 2792 | |
| 2793 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 2794 | EXPECT_EQ(300, event->getRawX(0)); |
| 2795 | EXPECT_EQ(880, event->getRawY(0)); |
| 2796 | |
| 2797 | // Ensure that the x and y values are in the window's coordinate space. |
| 2798 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 2799 | // the logical display space. This will be the origin of the window space. |
| 2800 | EXPECT_EQ(100, event->getX(0)); |
| 2801 | EXPECT_EQ(80, event->getY(0)); |
| 2802 | } |
| 2803 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2804 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 2805 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2806 | |
| 2807 | class TransferTouchFixture : public InputDispatcherTest, |
| 2808 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 2809 | |
| 2810 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2811 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2812 | |
| 2813 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2814 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2815 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 2816 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 2817 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2818 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2819 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 2820 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 2821 | sp<FakeWindowHandle> wallpaper = |
| 2822 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2823 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2824 | // Add the windows to the dispatcher |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 2825 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2826 | |
| 2827 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2828 | NotifyMotionArgs downMotionArgs = |
| 2829 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2830 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2831 | mDispatcher->notifyMotion(&downMotionArgs); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 2832 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2833 | // Only the first window should get the down event |
| 2834 | firstWindow->consumeMotionDown(); |
| 2835 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 2836 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2837 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2838 | // Transfer touch to the second window |
| 2839 | TransferFunction f = GetParam(); |
| 2840 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 2841 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2842 | // The first window gets cancel and the second gets down |
| 2843 | firstWindow->consumeMotionCancel(); |
| 2844 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 2845 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2846 | |
| 2847 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2848 | NotifyMotionArgs upMotionArgs = |
| 2849 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2850 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2851 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2852 | // The first window gets no events and the second gets up |
| 2853 | firstWindow->assertNoEvents(); |
| 2854 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 2855 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2856 | } |
| 2857 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2858 | /** |
| 2859 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 2860 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 2861 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 2862 | * natural to the user. |
| 2863 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 2864 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 2865 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 2866 | * the first window to the second. |
| 2867 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 2868 | * the other API, as well. |
| 2869 | */ |
| 2870 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 2871 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2872 | |
| 2873 | // Create a couple of windows + a spy window |
| 2874 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2875 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2876 | spyWindow->setTrustedOverlay(true); |
| 2877 | spyWindow->setSpy(true); |
| 2878 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2879 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2880 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2881 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2882 | |
| 2883 | // Add the windows to the dispatcher |
| 2884 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}}); |
| 2885 | |
| 2886 | // Send down to the first window |
| 2887 | NotifyMotionArgs downMotionArgs = |
| 2888 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2889 | ADISPLAY_ID_DEFAULT); |
| 2890 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2891 | // Only the first window and spy should get the down event |
| 2892 | spyWindow->consumeMotionDown(); |
| 2893 | firstWindow->consumeMotionDown(); |
| 2894 | |
| 2895 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 2896 | // if f === 'transferTouch'. |
| 2897 | TransferFunction f = GetParam(); |
| 2898 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 2899 | ASSERT_TRUE(success); |
| 2900 | // The first window gets cancel and the second gets down |
| 2901 | firstWindow->consumeMotionCancel(); |
| 2902 | secondWindow->consumeMotionDown(); |
| 2903 | |
| 2904 | // Send up event to the second window |
| 2905 | NotifyMotionArgs upMotionArgs = |
| 2906 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2907 | ADISPLAY_ID_DEFAULT); |
| 2908 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2909 | // The first window gets no events and the second+spy get up |
| 2910 | firstWindow->assertNoEvents(); |
| 2911 | spyWindow->consumeMotionUp(); |
| 2912 | secondWindow->consumeMotionUp(); |
| 2913 | } |
| 2914 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2915 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2916 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2917 | |
| 2918 | PointF touchPoint = {10, 10}; |
| 2919 | |
| 2920 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2921 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2922 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 2923 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 2924 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2925 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2926 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 2927 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 2928 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2929 | |
| 2930 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2931 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2932 | |
| 2933 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2934 | NotifyMotionArgs downMotionArgs = |
| 2935 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2936 | ADISPLAY_ID_DEFAULT, {touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2937 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2938 | // Only the first window should get the down event |
| 2939 | firstWindow->consumeMotionDown(); |
| 2940 | secondWindow->assertNoEvents(); |
| 2941 | |
| 2942 | // Send pointer down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2943 | NotifyMotionArgs pointerDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2944 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2945 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2946 | mDispatcher->notifyMotion(&pointerDownMotionArgs); |
| 2947 | // Only the first window should get the pointer down event |
| 2948 | firstWindow->consumeMotionPointerDown(1); |
| 2949 | secondWindow->assertNoEvents(); |
| 2950 | |
| 2951 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2952 | TransferFunction f = GetParam(); |
| 2953 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 2954 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2955 | // The first window gets cancel and the second gets down and pointer down |
| 2956 | firstWindow->consumeMotionCancel(); |
| 2957 | secondWindow->consumeMotionDown(); |
| 2958 | secondWindow->consumeMotionPointerDown(1); |
| 2959 | |
| 2960 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2961 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2962 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2963 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2964 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 2965 | // The first window gets nothing and the second gets pointer up |
| 2966 | firstWindow->assertNoEvents(); |
| 2967 | secondWindow->consumeMotionPointerUp(1); |
| 2968 | |
| 2969 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2970 | NotifyMotionArgs upMotionArgs = |
| 2971 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2972 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2973 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2974 | // The first window gets nothing and the second gets up |
| 2975 | firstWindow->assertNoEvents(); |
| 2976 | secondWindow->consumeMotionUp(); |
| 2977 | } |
| 2978 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame^] | 2979 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 2980 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2981 | |
| 2982 | // Create a couple of windows |
| 2983 | sp<FakeWindowHandle> firstWindow = |
| 2984 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 2985 | ADISPLAY_ID_DEFAULT); |
| 2986 | firstWindow->setDupTouchToWallpaper(true); |
| 2987 | sp<FakeWindowHandle> secondWindow = |
| 2988 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 2989 | ADISPLAY_ID_DEFAULT); |
| 2990 | secondWindow->setDupTouchToWallpaper(true); |
| 2991 | |
| 2992 | sp<FakeWindowHandle> wallpaper1 = |
| 2993 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 2994 | wallpaper1->setIsWallpaper(true); |
| 2995 | |
| 2996 | sp<FakeWindowHandle> wallpaper2 = |
| 2997 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 2998 | wallpaper2->setIsWallpaper(true); |
| 2999 | // Add the windows to the dispatcher |
| 3000 | mDispatcher->setInputWindows( |
| 3001 | {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}}); |
| 3002 | |
| 3003 | // Send down to the first window |
| 3004 | NotifyMotionArgs downMotionArgs = |
| 3005 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3006 | ADISPLAY_ID_DEFAULT); |
| 3007 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3008 | |
| 3009 | // Only the first window should get the down event |
| 3010 | firstWindow->consumeMotionDown(); |
| 3011 | secondWindow->assertNoEvents(); |
| 3012 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3013 | wallpaper2->assertNoEvents(); |
| 3014 | |
| 3015 | // Transfer touch focus to the second window |
| 3016 | TransferFunction f = GetParam(); |
| 3017 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 3018 | ASSERT_TRUE(success); |
| 3019 | |
| 3020 | // The first window gets cancel and the second gets down |
| 3021 | firstWindow->consumeMotionCancel(); |
| 3022 | secondWindow->consumeMotionDown(); |
| 3023 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3024 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3025 | |
| 3026 | // Send up event to the second window |
| 3027 | NotifyMotionArgs upMotionArgs = |
| 3028 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3029 | ADISPLAY_ID_DEFAULT); |
| 3030 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3031 | // The first window gets no events and the second gets up |
| 3032 | firstWindow->assertNoEvents(); |
| 3033 | secondWindow->consumeMotionUp(); |
| 3034 | wallpaper1->assertNoEvents(); |
| 3035 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3036 | } |
| 3037 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3038 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 3039 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 3040 | // for the case where there are multiple pointers split across several windows. |
| 3041 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 3042 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3043 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 3044 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3045 | return dispatcher->transferTouch(destChannelToken, |
| 3046 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3047 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3048 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 3049 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3050 | return dispatcher->transferTouchFocus(from, to, |
| 3051 | false /*isDragAndDrop*/); |
| 3052 | })); |
| 3053 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3054 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3055 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3056 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3057 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3058 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3059 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3060 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3061 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3062 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3063 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3064 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3065 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3066 | |
| 3067 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3068 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3069 | |
| 3070 | PointF pointInFirst = {300, 200}; |
| 3071 | PointF pointInSecond = {300, 600}; |
| 3072 | |
| 3073 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3074 | NotifyMotionArgs firstDownMotionArgs = |
| 3075 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3076 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3077 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 3078 | // Only the first window should get the down event |
| 3079 | firstWindow->consumeMotionDown(); |
| 3080 | secondWindow->assertNoEvents(); |
| 3081 | |
| 3082 | // Send down to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3083 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3084 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3085 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3086 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3087 | // The first window gets a move and the second a down |
| 3088 | firstWindow->consumeMotionMove(); |
| 3089 | secondWindow->consumeMotionDown(); |
| 3090 | |
| 3091 | // Transfer touch focus to the second window |
| 3092 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 3093 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 3094 | firstWindow->consumeMotionCancel(); |
| 3095 | secondWindow->consumeMotionPointerDown(1); |
| 3096 | |
| 3097 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3098 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3099 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3100 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3101 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3102 | // The first window gets nothing and the second gets pointer up |
| 3103 | firstWindow->assertNoEvents(); |
| 3104 | secondWindow->consumeMotionPointerUp(1); |
| 3105 | |
| 3106 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3107 | NotifyMotionArgs upMotionArgs = |
| 3108 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3109 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3110 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3111 | // The first window gets nothing and the second gets up |
| 3112 | firstWindow->assertNoEvents(); |
| 3113 | secondWindow->consumeMotionUp(); |
| 3114 | } |
| 3115 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3116 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 3117 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 3118 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 3119 | // window should get nothing. |
| 3120 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 3121 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3122 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3123 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3124 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3125 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3126 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3127 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3128 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3129 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3130 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3131 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3132 | |
| 3133 | // Add the windows to the dispatcher |
| 3134 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 3135 | |
| 3136 | PointF pointInFirst = {300, 200}; |
| 3137 | PointF pointInSecond = {300, 600}; |
| 3138 | |
| 3139 | // Send down to the first window |
| 3140 | NotifyMotionArgs firstDownMotionArgs = |
| 3141 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3142 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 3143 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 3144 | // Only the first window should get the down event |
| 3145 | firstWindow->consumeMotionDown(); |
| 3146 | secondWindow->assertNoEvents(); |
| 3147 | |
| 3148 | // Send down to the second window |
| 3149 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3150 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3151 | {pointInFirst, pointInSecond}); |
| 3152 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3153 | // The first window gets a move and the second a down |
| 3154 | firstWindow->consumeMotionMove(); |
| 3155 | secondWindow->consumeMotionDown(); |
| 3156 | |
| 3157 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3158 | const bool transferred = |
| 3159 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3160 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 3161 | ASSERT_FALSE(transferred); |
| 3162 | firstWindow->assertNoEvents(); |
| 3163 | secondWindow->assertNoEvents(); |
| 3164 | |
| 3165 | // The rest of the dispatch should proceed as normal |
| 3166 | // Send pointer up to the second window |
| 3167 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3168 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3169 | {pointInFirst, pointInSecond}); |
| 3170 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3171 | // The first window gets MOVE and the second gets pointer up |
| 3172 | firstWindow->consumeMotionMove(); |
| 3173 | secondWindow->consumeMotionUp(); |
| 3174 | |
| 3175 | // Send up event to the first window |
| 3176 | NotifyMotionArgs upMotionArgs = |
| 3177 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3178 | ADISPLAY_ID_DEFAULT); |
| 3179 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3180 | // The first window gets nothing and the second gets up |
| 3181 | firstWindow->consumeMotionUp(); |
| 3182 | secondWindow->assertNoEvents(); |
| 3183 | } |
| 3184 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3185 | // This case will create two windows and one mirrored window on the default display and mirror |
| 3186 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 3187 | // the windows info of second display before default display. |
| 3188 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 3189 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3190 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3191 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3192 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3193 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3194 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3195 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3196 | |
| 3197 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 3198 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 3199 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3200 | |
| 3201 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 3202 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3203 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3204 | |
| 3205 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 3206 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3207 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3208 | |
| 3209 | // Update window info, let it find window handle of second display first. |
| 3210 | mDispatcher->setInputWindows( |
| 3211 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 3212 | {ADISPLAY_ID_DEFAULT, |
| 3213 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 3214 | |
| 3215 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3216 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3217 | {50, 50})) |
| 3218 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3219 | |
| 3220 | // Window should receive motion event. |
| 3221 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3222 | |
| 3223 | // Transfer touch focus |
| 3224 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 3225 | secondWindowInPrimary->getToken())); |
| 3226 | // The first window gets cancel. |
| 3227 | firstWindowInPrimary->consumeMotionCancel(); |
| 3228 | secondWindowInPrimary->consumeMotionDown(); |
| 3229 | |
| 3230 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3231 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3232 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 3233 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3234 | firstWindowInPrimary->assertNoEvents(); |
| 3235 | secondWindowInPrimary->consumeMotionMove(); |
| 3236 | |
| 3237 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3238 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3239 | {150, 50})) |
| 3240 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3241 | firstWindowInPrimary->assertNoEvents(); |
| 3242 | secondWindowInPrimary->consumeMotionUp(); |
| 3243 | } |
| 3244 | |
| 3245 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 3246 | // 'transferTouch' api. |
| 3247 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 3248 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3249 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3250 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3251 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3252 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3253 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3254 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3255 | |
| 3256 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 3257 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 3258 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3259 | |
| 3260 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 3261 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3262 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3263 | |
| 3264 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 3265 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3266 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3267 | |
| 3268 | // Update window info, let it find window handle of second display first. |
| 3269 | mDispatcher->setInputWindows( |
| 3270 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 3271 | {ADISPLAY_ID_DEFAULT, |
| 3272 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 3273 | |
| 3274 | // Touch on second display. |
| 3275 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3276 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) |
| 3277 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3278 | |
| 3279 | // Window should receive motion event. |
| 3280 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 3281 | |
| 3282 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3283 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3284 | |
| 3285 | // The first window gets cancel. |
| 3286 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 3287 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 3288 | |
| 3289 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3290 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3291 | SECOND_DISPLAY_ID, {150, 50})) |
| 3292 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3293 | firstWindowInPrimary->assertNoEvents(); |
| 3294 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 3295 | |
| 3296 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3297 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
| 3298 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3299 | firstWindowInPrimary->assertNoEvents(); |
| 3300 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 3301 | } |
| 3302 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3303 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3304 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3305 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3306 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3307 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3308 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3309 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3310 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3311 | |
| 3312 | window->consumeFocusEvent(true); |
| 3313 | |
| 3314 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3315 | mDispatcher->notifyKey(&keyArgs); |
| 3316 | |
| 3317 | // Window should receive key down event. |
| 3318 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3319 | } |
| 3320 | |
| 3321 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3322 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3323 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3324 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3325 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3326 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3327 | |
| 3328 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3329 | mDispatcher->notifyKey(&keyArgs); |
| 3330 | mDispatcher->waitForIdle(); |
| 3331 | |
| 3332 | window->assertNoEvents(); |
| 3333 | } |
| 3334 | |
| 3335 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 3336 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3337 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3338 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3339 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3340 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3341 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3342 | |
| 3343 | // Send key |
| 3344 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3345 | mDispatcher->notifyKey(&keyArgs); |
| 3346 | // Send motion |
| 3347 | NotifyMotionArgs motionArgs = |
| 3348 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3349 | ADISPLAY_ID_DEFAULT); |
| 3350 | mDispatcher->notifyMotion(&motionArgs); |
| 3351 | |
| 3352 | // Window should receive only the motion event |
| 3353 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3354 | window->assertNoEvents(); // Key event or focus event will not be received |
| 3355 | } |
| 3356 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3357 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 3358 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3359 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3360 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3361 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3362 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3363 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3364 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3365 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3366 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3367 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3368 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3369 | |
| 3370 | // Add the windows to the dispatcher |
| 3371 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 3372 | |
| 3373 | PointF pointInFirst = {300, 200}; |
| 3374 | PointF pointInSecond = {300, 600}; |
| 3375 | |
| 3376 | // Send down to the first window |
| 3377 | NotifyMotionArgs firstDownMotionArgs = |
| 3378 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3379 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 3380 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 3381 | // Only the first window should get the down event |
| 3382 | firstWindow->consumeMotionDown(); |
| 3383 | secondWindow->assertNoEvents(); |
| 3384 | |
| 3385 | // Send down to the second window |
| 3386 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3387 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3388 | {pointInFirst, pointInSecond}); |
| 3389 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3390 | // The first window gets a move and the second a down |
| 3391 | firstWindow->consumeMotionMove(); |
| 3392 | secondWindow->consumeMotionDown(); |
| 3393 | |
| 3394 | // Send pointer cancel to the second window |
| 3395 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3396 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3397 | {pointInFirst, pointInSecond}); |
| 3398 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3399 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3400 | // The first window gets move and the second gets cancel. |
| 3401 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 3402 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 3403 | |
| 3404 | // Send up event. |
| 3405 | NotifyMotionArgs upMotionArgs = |
| 3406 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3407 | ADISPLAY_ID_DEFAULT); |
| 3408 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3409 | // The first window gets up and the second gets nothing. |
| 3410 | firstWindow->consumeMotionUp(); |
| 3411 | secondWindow->assertNoEvents(); |
| 3412 | } |
| 3413 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3414 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 3415 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3416 | |
| 3417 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3418 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3419 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3420 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 3421 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 3422 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 3423 | |
| 3424 | window->sendTimeline(1 /*inputEventId*/, graphicsTimeline); |
| 3425 | window->assertNoEvents(); |
| 3426 | mDispatcher->waitForIdle(); |
| 3427 | } |
| 3428 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3429 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3430 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3431 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3432 | int32_t displayId) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3433 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3434 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3435 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3438 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 3439 | |
| 3440 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3441 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, |
| 3442 | expectedDisplayId, expectedFlags); |
| 3443 | } |
| 3444 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3445 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 3446 | |
| 3447 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 3448 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3449 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3450 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 3451 | expectedDisplayId, expectedFlags); |
| 3452 | } |
| 3453 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3454 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3455 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 3456 | expectedDisplayId, expectedFlags); |
| 3457 | } |
| 3458 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3459 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3460 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, |
| 3461 | expectedDisplayId, expectedFlags); |
| 3462 | } |
| 3463 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3464 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3465 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 3466 | expectedDisplayId, expectedFlags); |
| 3467 | } |
| 3468 | |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 3469 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 3470 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 3471 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 3472 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT, |
| 3473 | 0 /*expectedFlags*/); |
| 3474 | } |
| 3475 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3476 | MotionEvent* consumeMotion() { |
| 3477 | InputEvent* event = mInputReceiver->consume(); |
| 3478 | if (!event) { |
| 3479 | ADD_FAILURE() << "No event was produced"; |
| 3480 | return nullptr; |
| 3481 | } |
| 3482 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 3483 | ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion"; |
| 3484 | return nullptr; |
| 3485 | } |
| 3486 | return static_cast<MotionEvent*>(event); |
| 3487 | } |
| 3488 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3489 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 3490 | |
| 3491 | private: |
| 3492 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3493 | }; |
| 3494 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3495 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 3496 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3497 | /** |
| 3498 | * Two entities that receive touch: A window, and a global monitor. |
| 3499 | * The touch goes to the window, and then the window disappears. |
| 3500 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 3501 | * for the monitor, as well. |
| 3502 | * 1. foregroundWindow |
| 3503 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 3504 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3505 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3506 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3507 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3508 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3509 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3510 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3511 | |
| 3512 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3513 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3514 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3515 | {100, 200})) |
| 3516 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3517 | |
| 3518 | // Both the foreground window and the global monitor should receive the touch down |
| 3519 | window->consumeMotionDown(); |
| 3520 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3521 | |
| 3522 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3523 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3524 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 3525 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3526 | |
| 3527 | window->consumeMotionMove(); |
| 3528 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 3529 | |
| 3530 | // Now the foreground window goes away |
| 3531 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 3532 | window->consumeMotionCancel(); |
| 3533 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 3534 | |
| 3535 | // If more events come in, there will be no more foreground window to send them to. This will |
| 3536 | // cause a cancel for the monitor, as well. |
| 3537 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 3538 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3539 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 3540 | << "Injection should fail because the window was removed"; |
| 3541 | window->assertNoEvents(); |
| 3542 | // Global monitor now gets the cancel |
| 3543 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 3544 | } |
| 3545 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3546 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3547 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3548 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3549 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3550 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3551 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3552 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3553 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3554 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3555 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3556 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3557 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3558 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3559 | } |
| 3560 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3561 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
| 3562 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3563 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3564 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3565 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3566 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3567 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3568 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3569 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3570 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3571 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3572 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3573 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3574 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3575 | // Pilfer pointers from the monitor. |
| 3576 | // This should not do anything and the window should continue to receive events. |
| 3577 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3578 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3579 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3580 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3581 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3582 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3583 | |
| 3584 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 3585 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3588 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3589 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3590 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3591 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3592 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3593 | window->setWindowOffset(20, 40); |
| 3594 | window->setWindowTransform(0, 1, -1, 0); |
| 3595 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3596 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3597 | |
| 3598 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3599 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 3600 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3601 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3602 | MotionEvent* event = monitor.consumeMotion(); |
| 3603 | // Even though window has transform, gesture monitor must not. |
| 3604 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 3605 | } |
| 3606 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3607 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3608 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3609 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3610 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3611 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3612 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3613 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 3614 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3615 | } |
| 3616 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3617 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3618 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3619 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3620 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3621 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3622 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3623 | |
| 3624 | NotifyMotionArgs motionArgs = |
| 3625 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3626 | ADISPLAY_ID_DEFAULT); |
| 3627 | |
| 3628 | mDispatcher->notifyMotion(&motionArgs); |
| 3629 | // Window should receive motion down event. |
| 3630 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3631 | |
| 3632 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3633 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3634 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 3635 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 3636 | motionArgs.pointerCoords[0].getX() - 10); |
| 3637 | |
| 3638 | mDispatcher->notifyMotion(&motionArgs); |
| 3639 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
| 3640 | 0 /*expectedFlags*/); |
| 3641 | } |
| 3642 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3643 | /** |
| 3644 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 3645 | * the device default right away. In the test scenario, we check both the default value, |
| 3646 | * and the action of enabling / disabling. |
| 3647 | */ |
| 3648 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3649 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3650 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3651 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 3652 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3653 | |
| 3654 | // Set focused application. |
| 3655 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3656 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3657 | |
| 3658 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3659 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3660 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3661 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 3662 | |
| 3663 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3664 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3665 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3666 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); |
| 3667 | |
| 3668 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 3669 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 3670 | true /*hasPermission*/, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3671 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3672 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3673 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3674 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3675 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); |
| 3676 | |
| 3677 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3678 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3679 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3680 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); |
| 3681 | |
| 3682 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 3683 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 3684 | true /*hasPermission*/, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3685 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3686 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3687 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3688 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3689 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 3690 | |
| 3691 | window->assertNoEvents(); |
| 3692 | } |
| 3693 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3694 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3695 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3696 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3697 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3698 | |
| 3699 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3700 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3701 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3702 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3703 | setFocusedWindow(window); |
| 3704 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3705 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 3706 | |
| 3707 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 3708 | mDispatcher->notifyKey(&keyArgs); |
| 3709 | |
| 3710 | InputEvent* event = window->consume(); |
| 3711 | ASSERT_NE(event, nullptr); |
| 3712 | |
| 3713 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 3714 | ASSERT_NE(verified, nullptr); |
| 3715 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 3716 | |
| 3717 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 3718 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 3719 | ASSERT_EQ(keyArgs.source, verified->source); |
| 3720 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 3721 | |
| 3722 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 3723 | |
| 3724 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3725 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 3726 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3727 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 3728 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 3729 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 3730 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 3731 | } |
| 3732 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3733 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3734 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3735 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3736 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3737 | |
| 3738 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3739 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3740 | ui::Transform transform; |
| 3741 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 3742 | |
| 3743 | gui::DisplayInfo displayInfo; |
| 3744 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 3745 | displayInfo.transform = transform; |
| 3746 | |
| 3747 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3748 | |
| 3749 | NotifyMotionArgs motionArgs = |
| 3750 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3751 | ADISPLAY_ID_DEFAULT); |
| 3752 | mDispatcher->notifyMotion(&motionArgs); |
| 3753 | |
| 3754 | InputEvent* event = window->consume(); |
| 3755 | ASSERT_NE(event, nullptr); |
| 3756 | |
| 3757 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 3758 | ASSERT_NE(verified, nullptr); |
| 3759 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 3760 | |
| 3761 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 3762 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 3763 | EXPECT_EQ(motionArgs.source, verified->source); |
| 3764 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 3765 | |
| 3766 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 3767 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3768 | const vec2 rawXY = |
| 3769 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 3770 | motionArgs.pointerCoords[0].getXYValue()); |
| 3771 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 3772 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3773 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3774 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 3775 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3776 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 3777 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 3778 | } |
| 3779 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3780 | /** |
| 3781 | * Ensure that separate calls to sign the same data are generating the same key. |
| 3782 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 3783 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 3784 | * tests. |
| 3785 | */ |
| 3786 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 3787 | KeyEvent event = getTestKeyEvent(); |
| 3788 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 3789 | |
| 3790 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 3791 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 3792 | ASSERT_EQ(hmac1, hmac2); |
| 3793 | } |
| 3794 | |
| 3795 | /** |
| 3796 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 3797 | */ |
| 3798 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 3799 | KeyEvent event = getTestKeyEvent(); |
| 3800 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 3801 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 3802 | |
| 3803 | verifiedEvent.deviceId += 1; |
| 3804 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3805 | |
| 3806 | verifiedEvent.source += 1; |
| 3807 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3808 | |
| 3809 | verifiedEvent.eventTimeNanos += 1; |
| 3810 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3811 | |
| 3812 | verifiedEvent.displayId += 1; |
| 3813 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3814 | |
| 3815 | verifiedEvent.action += 1; |
| 3816 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3817 | |
| 3818 | verifiedEvent.downTimeNanos += 1; |
| 3819 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3820 | |
| 3821 | verifiedEvent.flags += 1; |
| 3822 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3823 | |
| 3824 | verifiedEvent.keyCode += 1; |
| 3825 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3826 | |
| 3827 | verifiedEvent.scanCode += 1; |
| 3828 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3829 | |
| 3830 | verifiedEvent.metaState += 1; |
| 3831 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3832 | |
| 3833 | verifiedEvent.repeatCount += 1; |
| 3834 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3835 | } |
| 3836 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3837 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 3838 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3839 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3840 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3841 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3842 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3843 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3844 | |
| 3845 | // Top window is also focusable but is not granted focus. |
| 3846 | windowTop->setFocusable(true); |
| 3847 | windowSecond->setFocusable(true); |
| 3848 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 3849 | setFocusedWindow(windowSecond); |
| 3850 | |
| 3851 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3852 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3853 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3854 | |
| 3855 | // Focused window should receive event. |
| 3856 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 3857 | windowTop->assertNoEvents(); |
| 3858 | } |
| 3859 | |
| 3860 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 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 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3865 | |
| 3866 | window->setFocusable(true); |
| 3867 | // Release channel for window is no longer valid. |
| 3868 | window->releaseChannel(); |
| 3869 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3870 | setFocusedWindow(window); |
| 3871 | |
| 3872 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3873 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3874 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3875 | |
| 3876 | // window channel is invalid, so it should not receive any input event. |
| 3877 | window->assertNoEvents(); |
| 3878 | } |
| 3879 | |
| 3880 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 3881 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3882 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3883 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3884 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3885 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3886 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3887 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3888 | setFocusedWindow(window); |
| 3889 | |
| 3890 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3891 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3892 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3893 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3894 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3895 | window->assertNoEvents(); |
| 3896 | } |
| 3897 | |
| 3898 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 3899 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3900 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3901 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3902 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3903 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3904 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3905 | |
| 3906 | windowTop->setFocusable(true); |
| 3907 | windowSecond->setFocusable(true); |
| 3908 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 3909 | setFocusedWindow(windowTop); |
| 3910 | windowTop->consumeFocusEvent(true); |
| 3911 | |
| 3912 | setFocusedWindow(windowSecond, windowTop); |
| 3913 | windowSecond->consumeFocusEvent(true); |
| 3914 | windowTop->consumeFocusEvent(false); |
| 3915 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3916 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3917 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3918 | |
| 3919 | // Focused window should receive event. |
| 3920 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 3921 | } |
| 3922 | |
| 3923 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { |
| 3924 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3925 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3926 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3927 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3928 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3929 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3930 | |
| 3931 | windowTop->setFocusable(true); |
| 3932 | windowSecond->setFocusable(true); |
| 3933 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 3934 | setFocusedWindow(windowSecond, windowTop); |
| 3935 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3936 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3937 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3938 | |
| 3939 | // Event should be dropped. |
| 3940 | windowTop->assertNoEvents(); |
| 3941 | windowSecond->assertNoEvents(); |
| 3942 | } |
| 3943 | |
| 3944 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 3945 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3946 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3947 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3948 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3949 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 3950 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3951 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3952 | |
| 3953 | window->setFocusable(true); |
| 3954 | previousFocusedWindow->setFocusable(true); |
| 3955 | window->setVisible(false); |
| 3956 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 3957 | setFocusedWindow(previousFocusedWindow); |
| 3958 | previousFocusedWindow->consumeFocusEvent(true); |
| 3959 | |
| 3960 | // Requesting focus on invisible window takes focus from currently focused window. |
| 3961 | setFocusedWindow(window); |
| 3962 | previousFocusedWindow->consumeFocusEvent(false); |
| 3963 | |
| 3964 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3965 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3966 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3967 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3968 | |
| 3969 | // Window does not get focus event or key down. |
| 3970 | window->assertNoEvents(); |
| 3971 | |
| 3972 | // Window becomes visible. |
| 3973 | window->setVisible(true); |
| 3974 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3975 | |
| 3976 | // Window receives focus event. |
| 3977 | window->consumeFocusEvent(true); |
| 3978 | // Focused window receives key down. |
| 3979 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3980 | } |
| 3981 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 3982 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 3983 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3984 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3985 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 3986 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3987 | |
| 3988 | // window is granted focus. |
| 3989 | window->setFocusable(true); |
| 3990 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3991 | setFocusedWindow(window); |
| 3992 | window->consumeFocusEvent(true); |
| 3993 | |
| 3994 | // When a display is removed window loses focus. |
| 3995 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 3996 | window->consumeFocusEvent(false); |
| 3997 | } |
| 3998 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3999 | /** |
| 4000 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 4001 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 4002 | * of the 'slipperyEnterWindow'. |
| 4003 | * |
| 4004 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 4005 | * a way so that the touched location is no longer covered by the top window. |
| 4006 | * |
| 4007 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 4008 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 4009 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 4010 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 4011 | * with ACTION_DOWN). |
| 4012 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 4013 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 4014 | * |
| 4015 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 4016 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 4017 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 4018 | * |
| 4019 | * In this test, we ensure that the event received by the bottom window has |
| 4020 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 4021 | */ |
| 4022 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4023 | constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1; |
| 4024 | constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4025 | |
| 4026 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4027 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4028 | |
| 4029 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4030 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4031 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4032 | // Make sure this one overlaps the bottom window |
| 4033 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 4034 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 4035 | // one. Windows with the same owner are not considered to be occluding each other. |
| 4036 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 4037 | |
| 4038 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4039 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4040 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4041 | |
| 4042 | mDispatcher->setInputWindows( |
| 4043 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 4044 | |
| 4045 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
| 4046 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4047 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 4048 | mDispatcher->notifyMotion(&args); |
| 4049 | slipperyExitWindow->consumeMotionDown(); |
| 4050 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 4051 | mDispatcher->setInputWindows( |
| 4052 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 4053 | |
| 4054 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4055 | ADISPLAY_ID_DEFAULT, {{51, 51}}); |
| 4056 | mDispatcher->notifyMotion(&args); |
| 4057 | |
| 4058 | slipperyExitWindow->consumeMotionCancel(); |
| 4059 | |
| 4060 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 4061 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 4062 | } |
| 4063 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4064 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 4065 | protected: |
| 4066 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 4067 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 4068 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4069 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4070 | sp<FakeWindowHandle> mWindow; |
| 4071 | |
| 4072 | virtual void SetUp() override { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4073 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4074 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4075 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4076 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 4077 | ASSERT_EQ(OK, mDispatcher->start()); |
| 4078 | |
| 4079 | setUpWindow(); |
| 4080 | } |
| 4081 | |
| 4082 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4083 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4084 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4085 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4086 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4087 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4088 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4089 | mWindow->consumeFocusEvent(true); |
| 4090 | } |
| 4091 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4092 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4093 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4094 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4095 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
| 4096 | mDispatcher->notifyKey(&keyArgs); |
| 4097 | |
| 4098 | // Window should receive key down event. |
| 4099 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4100 | } |
| 4101 | |
| 4102 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 4103 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 4104 | InputEvent* repeatEvent = mWindow->consume(); |
| 4105 | ASSERT_NE(nullptr, repeatEvent); |
| 4106 | |
| 4107 | uint32_t eventType = repeatEvent->getType(); |
| 4108 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); |
| 4109 | |
| 4110 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 4111 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 4112 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 4113 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 4114 | } |
| 4115 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4116 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4117 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4118 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4119 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
| 4120 | mDispatcher->notifyKey(&keyArgs); |
| 4121 | |
| 4122 | // Window should receive key down event. |
| 4123 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 4124 | 0 /*expectedFlags*/); |
| 4125 | } |
| 4126 | }; |
| 4127 | |
| 4128 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4129 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 4130 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4131 | expectKeyRepeatOnce(repeatCount); |
| 4132 | } |
| 4133 | } |
| 4134 | |
| 4135 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
| 4136 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 4137 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4138 | expectKeyRepeatOnce(repeatCount); |
| 4139 | } |
| 4140 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 4141 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4142 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4143 | expectKeyRepeatOnce(repeatCount); |
| 4144 | } |
| 4145 | } |
| 4146 | |
| 4147 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4148 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4149 | expectKeyRepeatOnce(1 /*repeatCount*/); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4150 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 4151 | mWindow->assertNoEvents(); |
| 4152 | } |
| 4153 | |
| 4154 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
| 4155 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 4156 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4157 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 4158 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4159 | // Stale key up from device 1. |
| 4160 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 4161 | // Device 2 is still down, keep repeating |
| 4162 | expectKeyRepeatOnce(2 /*repeatCount*/); |
| 4163 | expectKeyRepeatOnce(3 /*repeatCount*/); |
| 4164 | // Device 2 key up |
| 4165 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 4166 | mWindow->assertNoEvents(); |
| 4167 | } |
| 4168 | |
| 4169 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
| 4170 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 4171 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4172 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 4173 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4174 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. |
| 4175 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 4176 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4177 | mWindow->assertNoEvents(); |
| 4178 | } |
| 4179 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 4180 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 4181 | sendAndConsumeKeyDown(DEVICE_ID); |
| 4182 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4183 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
| 4184 | mDispatcher->notifyDeviceReset(&args); |
| 4185 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 4186 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 4187 | mWindow->assertNoEvents(); |
| 4188 | } |
| 4189 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4190 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4191 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4192 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4193 | InputEvent* repeatEvent = mWindow->consume(); |
| 4194 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 4195 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 4196 | IdGenerator::getSource(repeatEvent->getId())); |
| 4197 | } |
| 4198 | } |
| 4199 | |
| 4200 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4201 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4202 | |
| 4203 | std::unordered_set<int32_t> idSet; |
| 4204 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4205 | InputEvent* repeatEvent = mWindow->consume(); |
| 4206 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 4207 | int32_t id = repeatEvent->getId(); |
| 4208 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 4209 | idSet.insert(id); |
| 4210 | } |
| 4211 | } |
| 4212 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4213 | /* Test InputDispatcher for MultiDisplay */ |
| 4214 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 4215 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4216 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4217 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4218 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4219 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4220 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4221 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4222 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4223 | // Set focus window for primary display, but focused display would be second one. |
| 4224 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4225 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4226 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4227 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4228 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4229 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4230 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4231 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4232 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4233 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4234 | // Set focus display to second one. |
| 4235 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 4236 | // Set focus window for second display. |
| 4237 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4238 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4239 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4240 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4241 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4242 | } |
| 4243 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4244 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4245 | InputDispatcherTest::TearDown(); |
| 4246 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4247 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4248 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4249 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4250 | windowInSecondary.clear(); |
| 4251 | } |
| 4252 | |
| 4253 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4254 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4255 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4256 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4257 | sp<FakeWindowHandle> windowInSecondary; |
| 4258 | }; |
| 4259 | |
| 4260 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 4261 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4262 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4263 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4264 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4265 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4266 | windowInSecondary->assertNoEvents(); |
| 4267 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4268 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4269 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4270 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4271 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4272 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4273 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4274 | } |
| 4275 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4276 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4277 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4278 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4279 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4280 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4281 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4282 | windowInSecondary->assertNoEvents(); |
| 4283 | |
| 4284 | // Test inject a key down without display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4285 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4286 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4287 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4288 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4289 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4290 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4291 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4292 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4293 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4294 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
| 4295 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4296 | |
| 4297 | // Test inject a key down, should timeout because of no target window. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4298 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4299 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4300 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4301 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4302 | windowInSecondary->assertNoEvents(); |
| 4303 | } |
| 4304 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4305 | // Test per-display input monitors for motion event. |
| 4306 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4307 | FakeMonitorReceiver monitorInPrimary = |
| 4308 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4309 | FakeMonitorReceiver monitorInSecondary = |
| 4310 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4311 | |
| 4312 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4313 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4314 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4315 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4316 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4317 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4318 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4319 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4320 | |
| 4321 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4322 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4323 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4324 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4325 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4326 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4327 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4328 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4329 | |
| 4330 | // Test inject a non-pointer motion event. |
| 4331 | // If specific a display, it will dispatch to the focused window of particular display, |
| 4332 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4333 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4334 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 4335 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4336 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4337 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4338 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4339 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4340 | } |
| 4341 | |
| 4342 | // Test per-display input monitors for key event. |
| 4343 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4344 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4345 | FakeMonitorReceiver monitorInPrimary = |
| 4346 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4347 | FakeMonitorReceiver monitorInSecondary = |
| 4348 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4349 | |
| 4350 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4351 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4352 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4353 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4354 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4355 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4356 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4357 | } |
| 4358 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4359 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 4360 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4361 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4362 | secondWindowInPrimary->setFocusable(true); |
| 4363 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 4364 | setFocusedWindow(secondWindowInPrimary); |
| 4365 | windowInPrimary->consumeFocusEvent(false); |
| 4366 | secondWindowInPrimary->consumeFocusEvent(true); |
| 4367 | |
| 4368 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4369 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 4370 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4371 | windowInPrimary->assertNoEvents(); |
| 4372 | windowInSecondary->assertNoEvents(); |
| 4373 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4374 | } |
| 4375 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 4376 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 4377 | FakeMonitorReceiver monitorInPrimary = |
| 4378 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4379 | FakeMonitorReceiver monitorInSecondary = |
| 4380 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 4381 | |
| 4382 | // Test touch down on primary display. |
| 4383 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4384 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4385 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4386 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4387 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4388 | |
| 4389 | // Test touch down on second display. |
| 4390 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4391 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4392 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4393 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4394 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 4395 | |
| 4396 | // Trigger cancel touch. |
| 4397 | mDispatcher->cancelCurrentTouch(); |
| 4398 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4399 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4400 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4401 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4402 | |
| 4403 | // Test inject a move motion event, no window/monitor should receive the event. |
| 4404 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4405 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4406 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 4407 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 4408 | windowInPrimary->assertNoEvents(); |
| 4409 | monitorInPrimary.assertNoEvents(); |
| 4410 | |
| 4411 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4412 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4413 | SECOND_DISPLAY_ID, {110, 200})) |
| 4414 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 4415 | windowInSecondary->assertNoEvents(); |
| 4416 | monitorInSecondary.assertNoEvents(); |
| 4417 | } |
| 4418 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4419 | class InputFilterTest : public InputDispatcherTest { |
| 4420 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4421 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 4422 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4423 | NotifyMotionArgs motionArgs; |
| 4424 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4425 | motionArgs = |
| 4426 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4427 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4428 | motionArgs = |
| 4429 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4430 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4431 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4432 | if (expectToBeFiltered) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4433 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); |
| 4434 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4435 | } else { |
| 4436 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 4437 | } |
| 4438 | } |
| 4439 | |
| 4440 | void testNotifyKey(bool expectToBeFiltered) { |
| 4441 | NotifyKeyArgs keyArgs; |
| 4442 | |
| 4443 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 4444 | mDispatcher->notifyKey(&keyArgs); |
| 4445 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 4446 | mDispatcher->notifyKey(&keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4447 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4448 | |
| 4449 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 4450 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4451 | } else { |
| 4452 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 4453 | } |
| 4454 | } |
| 4455 | }; |
| 4456 | |
| 4457 | // Test InputFilter for MotionEvent |
| 4458 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 4459 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 4460 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 4461 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 4462 | |
| 4463 | // Enable InputFilter |
| 4464 | mDispatcher->setInputFilterEnabled(true); |
| 4465 | // Test touch on both primary and second display, and check if both events are filtered. |
| 4466 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 4467 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 4468 | |
| 4469 | // Disable InputFilter |
| 4470 | mDispatcher->setInputFilterEnabled(false); |
| 4471 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 4472 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 4473 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 4474 | } |
| 4475 | |
| 4476 | // Test InputFilter for KeyEvent |
| 4477 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 4478 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 4479 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 4480 | |
| 4481 | // Enable InputFilter |
| 4482 | mDispatcher->setInputFilterEnabled(true); |
| 4483 | // Send a key event, and check if it is filtered. |
| 4484 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 4485 | |
| 4486 | // Disable InputFilter |
| 4487 | mDispatcher->setInputFilterEnabled(false); |
| 4488 | // Send a key event, and check if it isn't filtered. |
| 4489 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 4490 | } |
| 4491 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4492 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 4493 | // logical display coordinate space. |
| 4494 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 4495 | ui::Transform firstDisplayTransform; |
| 4496 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 4497 | ui::Transform secondDisplayTransform; |
| 4498 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 4499 | |
| 4500 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 4501 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 4502 | displayInfos[0].transform = firstDisplayTransform; |
| 4503 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 4504 | displayInfos[1].transform = secondDisplayTransform; |
| 4505 | |
| 4506 | mDispatcher->onWindowInfosChanged({}, displayInfos); |
| 4507 | |
| 4508 | // Enable InputFilter |
| 4509 | mDispatcher->setInputFilterEnabled(true); |
| 4510 | |
| 4511 | // Ensure the correct transforms are used for the displays. |
| 4512 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); |
| 4513 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); |
| 4514 | } |
| 4515 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4516 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 4517 | protected: |
| 4518 | virtual void SetUp() override { |
| 4519 | InputDispatcherTest::SetUp(); |
| 4520 | |
| 4521 | /** |
| 4522 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 4523 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 4524 | * on. |
| 4525 | */ |
| 4526 | mDispatcher->setInputFilterEnabled(true); |
| 4527 | |
| 4528 | std::shared_ptr<InputApplicationHandle> application = |
| 4529 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4530 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 4531 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4532 | |
| 4533 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4534 | mWindow->setFocusable(true); |
| 4535 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4536 | setFocusedWindow(mWindow); |
| 4537 | mWindow->consumeFocusEvent(true); |
| 4538 | } |
| 4539 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4540 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 4541 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4542 | KeyEvent event; |
| 4543 | |
| 4544 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4545 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 4546 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
| 4547 | KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime); |
| 4548 | const int32_t additionalPolicyFlags = |
| 4549 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 4550 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4551 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4552 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 4553 | policyFlags | additionalPolicyFlags)); |
| 4554 | |
| 4555 | InputEvent* received = mWindow->consume(); |
| 4556 | ASSERT_NE(nullptr, received); |
| 4557 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4558 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY); |
| 4559 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); |
| 4560 | ASSERT_EQ(flags, keyEvent.getFlags()); |
| 4561 | } |
| 4562 | |
| 4563 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 4564 | int32_t flags) { |
| 4565 | MotionEvent event; |
| 4566 | PointerProperties pointerProperties[1]; |
| 4567 | PointerCoords pointerCoords[1]; |
| 4568 | pointerProperties[0].clear(); |
| 4569 | pointerProperties[0].id = 0; |
| 4570 | pointerCoords[0].clear(); |
| 4571 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 4572 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 4573 | |
| 4574 | ui::Transform identityTransform; |
| 4575 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4576 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 4577 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 4578 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 4579 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4580 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 4581 | eventTime, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4582 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 4583 | |
| 4584 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 4585 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4586 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4587 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 4588 | policyFlags | additionalPolicyFlags)); |
| 4589 | |
| 4590 | InputEvent* received = mWindow->consume(); |
| 4591 | ASSERT_NE(nullptr, received); |
| 4592 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
| 4593 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 4594 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); |
| 4595 | ASSERT_EQ(flags, motionEvent.getFlags()); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
| 4598 | private: |
| 4599 | sp<FakeWindowHandle> mWindow; |
| 4600 | }; |
| 4601 | |
| 4602 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4603 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 4604 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 4605 | // injected device id will be overwritten. |
| 4606 | testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 4607 | 0 /*flags*/); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4608 | } |
| 4609 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4610 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4611 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4612 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 4613 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 4614 | } |
| 4615 | |
| 4616 | TEST_F(InputFilterInjectionPolicyTest, |
| 4617 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 4618 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
| 4619 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 4620 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
| 4623 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
| 4624 | testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4625 | VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4626 | } |
| 4627 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4628 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4629 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4630 | InputDispatcherTest::SetUp(); |
| 4631 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4632 | std::shared_ptr<FakeApplicationHandle> application = |
| 4633 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4634 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4635 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4636 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4637 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4638 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4639 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4640 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4641 | |
| 4642 | // Set focused application. |
| 4643 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4644 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4645 | |
| 4646 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4647 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4648 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4649 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4650 | } |
| 4651 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4652 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4653 | InputDispatcherTest::TearDown(); |
| 4654 | |
| 4655 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4656 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4657 | } |
| 4658 | |
| 4659 | protected: |
| 4660 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4661 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4662 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4663 | }; |
| 4664 | |
| 4665 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 4666 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 4667 | // the onPointerDownOutsideFocus callback. |
| 4668 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4669 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4670 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4671 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4672 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4673 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4674 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4675 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4676 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 4677 | } |
| 4678 | |
| 4679 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 4680 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 4681 | // onPointerDownOutsideFocus callback. |
| 4682 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4683 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4684 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4685 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4686 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4687 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4688 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4689 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4690 | } |
| 4691 | |
| 4692 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 4693 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 4694 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4695 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4696 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4697 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4698 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4699 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4700 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4701 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4702 | } |
| 4703 | |
| 4704 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 4705 | // DOWN on the window that already has focus. Ensure no window received the |
| 4706 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4707 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4708 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4709 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4710 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4711 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4712 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4713 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4714 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4715 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4716 | } |
| 4717 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 4718 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 4719 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 4720 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 4721 | const MotionEvent event = |
| 4722 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4723 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 4724 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20)) |
| 4725 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 4726 | .build(); |
| 4727 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) |
| 4728 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4729 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 4730 | |
| 4731 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4732 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 4733 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 4734 | mUnfocusedWindow->assertNoEvents(); |
| 4735 | } |
| 4736 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4737 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 4738 | // windows that point to the same client token. |
| 4739 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 4740 | virtual void SetUp() override { |
| 4741 | InputDispatcherTest::SetUp(); |
| 4742 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4743 | std::shared_ptr<FakeApplicationHandle> application = |
| 4744 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4745 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 4746 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4747 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 4748 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4749 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", |
| 4750 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4751 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 4752 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4753 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4754 | } |
| 4755 | |
| 4756 | protected: |
| 4757 | sp<FakeWindowHandle> mWindow1; |
| 4758 | sp<FakeWindowHandle> mWindow2; |
| 4759 | |
| 4760 | // 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] | 4761 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4762 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 4763 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4764 | } |
| 4765 | |
| 4766 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 4767 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4768 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4769 | InputEvent* event = window->consume(); |
| 4770 | |
| 4771 | ASSERT_NE(nullptr, event) << name.c_str() |
| 4772 | << ": consumer should have returned non-NULL event."; |
| 4773 | |
| 4774 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 4775 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 4776 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 4777 | |
| 4778 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4779 | assertMotionAction(expectedAction, motionEvent.getAction()); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 4780 | ASSERT_EQ(points.size(), motionEvent.getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4781 | |
| 4782 | for (size_t i = 0; i < points.size(); i++) { |
| 4783 | float expectedX = points[i].x; |
| 4784 | float expectedY = points[i].y; |
| 4785 | |
| 4786 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 4787 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 4788 | << ", got " << motionEvent.getX(i); |
| 4789 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 4790 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 4791 | << ", got " << motionEvent.getY(i); |
| 4792 | } |
| 4793 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4794 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 4795 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4796 | std::vector<PointF> expectedPoints) { |
| 4797 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 4798 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 4799 | mDispatcher->notifyMotion(&motionArgs); |
| 4800 | |
| 4801 | // Always consume from window1 since it's the window that has the InputReceiver |
| 4802 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 4803 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4804 | }; |
| 4805 | |
| 4806 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 4807 | // Touch Window 1 |
| 4808 | PointF touchedPoint = {10, 10}; |
| 4809 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4810 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4811 | |
| 4812 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4813 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4814 | |
| 4815 | // Touch Window 2 |
| 4816 | touchedPoint = {150, 150}; |
| 4817 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4818 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4819 | } |
| 4820 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4821 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 4822 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4823 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4824 | |
| 4825 | // Touch Window 1 |
| 4826 | PointF touchedPoint = {10, 10}; |
| 4827 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4828 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4829 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4830 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4831 | |
| 4832 | // Touch Window 2 |
| 4833 | touchedPoint = {150, 150}; |
| 4834 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4835 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 4836 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4837 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4838 | // Update the transform so rotation is set |
| 4839 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4840 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 4841 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4842 | } |
| 4843 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4844 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4845 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4846 | |
| 4847 | // Touch Window 1 |
| 4848 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4849 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4850 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4851 | |
| 4852 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4853 | touchedPoints.push_back(PointF{150, 150}); |
| 4854 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4855 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4856 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4857 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4858 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4859 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4860 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4861 | // Update the transform so rotation is set for Window 2 |
| 4862 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4863 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4864 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4865 | } |
| 4866 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4867 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4868 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4869 | |
| 4870 | // Touch Window 1 |
| 4871 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4872 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4873 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4874 | |
| 4875 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4876 | touchedPoints.push_back(PointF{150, 150}); |
| 4877 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4878 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4879 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4880 | |
| 4881 | // Move both windows |
| 4882 | touchedPoints = {{20, 20}, {175, 175}}; |
| 4883 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 4884 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 4885 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4886 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4887 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4888 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4889 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4890 | expectedPoints.pop_back(); |
| 4891 | |
| 4892 | // Touch Window 2 |
| 4893 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4894 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4895 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4896 | |
| 4897 | // Move both windows |
| 4898 | touchedPoints = {{20, 20}, {175, 175}}; |
| 4899 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 4900 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 4901 | |
| 4902 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4903 | } |
| 4904 | |
| 4905 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 4906 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 4907 | |
| 4908 | // Touch Window 1 |
| 4909 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4910 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4911 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4912 | |
| 4913 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4914 | touchedPoints.push_back(PointF{150, 150}); |
| 4915 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4916 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4917 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4918 | |
| 4919 | // Move both windows |
| 4920 | touchedPoints = {{20, 20}, {175, 175}}; |
| 4921 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 4922 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 4923 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4924 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4925 | } |
| 4926 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4927 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 4928 | virtual void SetUp() override { |
| 4929 | InputDispatcherTest::SetUp(); |
| 4930 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4931 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4932 | mApplication->setDispatchingTimeout(20ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4933 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 4934 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4935 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 4936 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4937 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4938 | |
| 4939 | // Set focused application. |
| 4940 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 4941 | |
| 4942 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4943 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4944 | mWindow->consumeFocusEvent(true); |
| 4945 | } |
| 4946 | |
| 4947 | virtual void TearDown() override { |
| 4948 | InputDispatcherTest::TearDown(); |
| 4949 | mWindow.clear(); |
| 4950 | } |
| 4951 | |
| 4952 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4953 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4954 | sp<FakeWindowHandle> mWindow; |
| 4955 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 4956 | |
| 4957 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4958 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4959 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4960 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4961 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4962 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4963 | WINDOW_LOCATION)); |
| 4964 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4965 | |
| 4966 | sp<FakeWindowHandle> addSpyWindow() { |
| 4967 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4968 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4969 | spy->setTrustedOverlay(true); |
| 4970 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4971 | spy->setSpy(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4972 | spy->setDispatchingTimeout(30ms); |
| 4973 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); |
| 4974 | return spy; |
| 4975 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4976 | }; |
| 4977 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4978 | // Send a tap and respond, which should not cause an ANR. |
| 4979 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 4980 | tapOnWindow(); |
| 4981 | mWindow->consumeMotionDown(); |
| 4982 | mWindow->consumeMotionUp(); |
| 4983 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4984 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4985 | } |
| 4986 | |
| 4987 | // Send a regular key and respond, which should not cause an ANR. |
| 4988 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4989 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4990 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 4991 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4992 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 4993 | } |
| 4994 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4995 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 4996 | mWindow->setFocusable(false); |
| 4997 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4998 | mWindow->consumeFocusEvent(false); |
| 4999 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5000 | InputEventInjectionResult result = |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5001 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5002 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, |
| 5003 | false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5004 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5005 | // Key will not go to window because we have no focused window. |
| 5006 | // The 'no focused window' ANR timer should start instead. |
| 5007 | |
| 5008 | // Now, the focused application goes away. |
| 5009 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 5010 | // The key should get dropped and there should be no ANR. |
| 5011 | |
| 5012 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5013 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5014 | } |
| 5015 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5016 | // 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] | 5017 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 5018 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5019 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5020 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5021 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5022 | WINDOW_LOCATION)); |
| 5023 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5024 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 5025 | ASSERT_TRUE(sequenceNum); |
| 5026 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5027 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5028 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5029 | mWindow->finishEvent(*sequenceNum); |
| 5030 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 5031 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5032 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5033 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5034 | } |
| 5035 | |
| 5036 | // Send a key to the app and have the app not respond right away. |
| 5037 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 5038 | // Inject a key, and don't respond - expect that ANR is called. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5039 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5040 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 5041 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5042 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5043 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5044 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5045 | } |
| 5046 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5047 | // We have a focused application, but no focused window |
| 5048 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
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 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5054 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5055 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5056 | WINDOW_LOCATION)); |
| 5057 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 5058 | mDispatcher->waitForIdle(); |
| 5059 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5060 | |
| 5061 | // Once a focused event arrives, we get an ANR for this application |
| 5062 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 5063 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5064 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5065 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5066 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5067 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5068 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5069 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5070 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5071 | } |
| 5072 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 5073 | /** |
| 5074 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 5075 | * there will not be an ANR. |
| 5076 | */ |
| 5077 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 5078 | mWindow->setFocusable(false); |
| 5079 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5080 | mWindow->consumeFocusEvent(false); |
| 5081 | |
| 5082 | KeyEvent event; |
| 5083 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 5084 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 5085 | |
| 5086 | // Define a valid key down event that is stale (too old). |
| 5087 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 5088 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, |
| 5089 | AMETA_NONE, 1 /*repeatCount*/, eventTime, eventTime); |
| 5090 | |
| 5091 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 5092 | |
| 5093 | InputEventInjectionResult result = |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 5094 | mDispatcher->injectInputEvent(&event, {} /* targetUid */, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 5095 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 5096 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 5097 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 5098 | << "Injection should fail because the event is stale"; |
| 5099 | |
| 5100 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5101 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5102 | mWindow->assertNoEvents(); |
| 5103 | } |
| 5104 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5105 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5106 | // Make sure that we don't notify policy twice about the same ANR. |
| 5107 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5108 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5109 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5110 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5111 | |
| 5112 | // Once a focused event arrives, we get an ANR for this application |
| 5113 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 5114 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5115 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5116 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5117 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5118 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5119 | const std::chrono::duration appTimeout = |
| 5120 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 5121 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5122 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5123 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5124 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 5125 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5126 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5127 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 5128 | // '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] | 5129 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5130 | } |
| 5131 | |
| 5132 | // We have a focused application, but no focused window |
| 5133 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5134 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5135 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5136 | mWindow->consumeFocusEvent(false); |
| 5137 | |
| 5138 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5139 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5140 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5141 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 5142 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5143 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5144 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 5145 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5146 | |
| 5147 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5148 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5149 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5150 | mWindow->assertNoEvents(); |
| 5151 | } |
| 5152 | |
| 5153 | /** |
| 5154 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 5155 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 5156 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 5157 | * the ANR mechanism should still work. |
| 5158 | * |
| 5159 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 5160 | * DOWN event, while not responding on the second one. |
| 5161 | */ |
| 5162 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 5163 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5164 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5165 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 5166 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 5167 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5168 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5169 | |
| 5170 | // Now send ACTION_UP, with identical timestamp |
| 5171 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5172 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 5173 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 5174 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5175 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5176 | |
| 5177 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 5178 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5179 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5180 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5181 | } |
| 5182 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5183 | // A spy window can receive an ANR |
| 5184 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 5185 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 5186 | |
| 5187 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5188 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5189 | WINDOW_LOCATION)); |
| 5190 | mWindow->consumeMotionDown(); |
| 5191 | |
| 5192 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 5193 | ASSERT_TRUE(sequenceNum); |
| 5194 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5195 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5196 | |
| 5197 | spy->finishEvent(*sequenceNum); |
| 5198 | spy->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, |
| 5199 | 0 /*flags*/); |
| 5200 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5201 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5202 | } |
| 5203 | |
| 5204 | // 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] | 5205 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5206 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 5207 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5208 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5209 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5210 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5211 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5212 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5213 | |
| 5214 | // Stuck on the ACTION_UP |
| 5215 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5216 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5217 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5218 | // 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] | 5219 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5220 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5221 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5222 | |
| 5223 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 5224 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5225 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5226 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5227 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5228 | } |
| 5229 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5230 | // 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] | 5231 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5232 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 5233 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5234 | |
| 5235 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5236 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5237 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5238 | |
| 5239 | mWindow->consumeMotionDown(); |
| 5240 | // Stuck on the ACTION_UP |
| 5241 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5242 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5243 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5244 | // 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] | 5245 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5246 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5247 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5248 | |
| 5249 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 5250 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5251 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5252 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5253 | spy->assertNoEvents(); |
| 5254 | } |
| 5255 | |
| 5256 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
| 5257 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); |
| 5258 | |
| 5259 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5260 | |
| 5261 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5262 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5263 | WINDOW_LOCATION)); |
| 5264 | |
| 5265 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5266 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 5267 | ASSERT_TRUE(consumeSeq); |
| 5268 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5269 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5270 | |
| 5271 | monitor.finishEvent(*consumeSeq); |
| 5272 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5273 | |
| 5274 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5275 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5276 | } |
| 5277 | |
| 5278 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 5279 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 5280 | // get an ANR again. |
| 5281 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 5282 | // 2. consume all pending events (= queue becomes healthy again) |
| 5283 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 5284 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 5285 | tapOnWindow(); |
| 5286 | |
| 5287 | mWindow->consumeMotionDown(); |
| 5288 | // Block on ACTION_UP |
| 5289 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5290 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5291 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 5292 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5293 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5294 | mWindow->assertNoEvents(); |
| 5295 | |
| 5296 | tapOnWindow(); |
| 5297 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5298 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5299 | mWindow->consumeMotionUp(); |
| 5300 | |
| 5301 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5302 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5303 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5304 | mWindow->assertNoEvents(); |
| 5305 | } |
| 5306 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5307 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 5308 | // it. |
| 5309 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5310 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5311 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5312 | WINDOW_LOCATION)); |
| 5313 | |
| 5314 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5315 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5316 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5317 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 5318 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5319 | // 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] | 5320 | mWindow->consumeMotionDown(); |
| 5321 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 5322 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 5323 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5324 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5325 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5326 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5327 | } |
| 5328 | |
| 5329 | /** |
| 5330 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 5331 | * not to to the focused window until the motion is processed. |
| 5332 | * |
| 5333 | * Warning!!! |
| 5334 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 5335 | * and the injection timeout that we specify when injecting the key. |
| 5336 | * We must have the injection timeout (10ms) be smaller than |
| 5337 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 5338 | * |
| 5339 | * If that value changes, this test should also change. |
| 5340 | */ |
| 5341 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 5342 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 5343 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5344 | |
| 5345 | tapOnWindow(); |
| 5346 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 5347 | ASSERT_TRUE(downSequenceNum); |
| 5348 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 5349 | ASSERT_TRUE(upSequenceNum); |
| 5350 | // Don't finish the events yet, and send a key |
| 5351 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 5352 | // window even if motions are still being processed. But because the injection timeout is short, |
| 5353 | // we will receive INJECTION_TIMED_OUT as the result. |
| 5354 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5355 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5356 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5357 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 5358 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5359 | // Key will not be sent to the window, yet, because the window is still processing events |
| 5360 | // and the key remains pending, waiting for the touch events to be processed |
| 5361 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 5362 | ASSERT_FALSE(keySequenceNum); |
| 5363 | |
| 5364 | std::this_thread::sleep_for(500ms); |
| 5365 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 5366 | // to the focused window, even though we have not yet finished the motion event |
| 5367 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5368 | mWindow->finishEvent(*downSequenceNum); |
| 5369 | mWindow->finishEvent(*upSequenceNum); |
| 5370 | } |
| 5371 | |
| 5372 | /** |
| 5373 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 5374 | * not go to the focused window until the motion is processed. |
| 5375 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 5376 | * focused window right away. |
| 5377 | */ |
| 5378 | TEST_F(InputDispatcherSingleWindowAnr, |
| 5379 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 5380 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 5381 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5382 | |
| 5383 | tapOnWindow(); |
| 5384 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 5385 | ASSERT_TRUE(downSequenceNum); |
| 5386 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 5387 | ASSERT_TRUE(upSequenceNum); |
| 5388 | // Don't finish the events yet, and send a key |
| 5389 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5390 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5391 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5392 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5393 | // At this point, key is still pending, and should not be sent to the application yet. |
| 5394 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 5395 | ASSERT_FALSE(keySequenceNum); |
| 5396 | |
| 5397 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 5398 | tapOnWindow(); |
| 5399 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 5400 | // the other events yet. We can finish events in any order. |
| 5401 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 5402 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 5403 | mWindow->consumeMotionDown(); |
| 5404 | mWindow->consumeMotionUp(); |
| 5405 | mWindow->assertNoEvents(); |
| 5406 | } |
| 5407 | |
| 5408 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 5409 | virtual void SetUp() override { |
| 5410 | InputDispatcherTest::SetUp(); |
| 5411 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5412 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5413 | mApplication->setDispatchingTimeout(10ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5414 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 5415 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5416 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5417 | // 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] | 5418 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5419 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5420 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 5421 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 5422 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5423 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5424 | |
| 5425 | // Set focused application. |
| 5426 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5427 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5428 | |
| 5429 | // Expect one focus window exist in display. |
| 5430 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5431 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5432 | mFocusedWindow->consumeFocusEvent(true); |
| 5433 | } |
| 5434 | |
| 5435 | virtual void TearDown() override { |
| 5436 | InputDispatcherTest::TearDown(); |
| 5437 | |
| 5438 | mUnfocusedWindow.clear(); |
| 5439 | mFocusedWindow.clear(); |
| 5440 | } |
| 5441 | |
| 5442 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5443 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5444 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 5445 | sp<FakeWindowHandle> mFocusedWindow; |
| 5446 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 5447 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 5448 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 5449 | |
| 5450 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 5451 | |
| 5452 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 5453 | |
| 5454 | private: |
| 5455 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5456 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5457 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5458 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5459 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5460 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5461 | location)); |
| 5462 | } |
| 5463 | }; |
| 5464 | |
| 5465 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 5466 | // should be ANR'd first. |
| 5467 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5468 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5469 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5470 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5471 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5472 | mFocusedWindow->consumeMotionDown(); |
| 5473 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 5474 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 5475 | // We consumed all events, so no ANR |
| 5476 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5477 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5478 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5479 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5480 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5481 | FOCUSED_WINDOW_LOCATION)); |
| 5482 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 5483 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5484 | |
| 5485 | const std::chrono::duration timeout = |
| 5486 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5487 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5488 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 5489 | // sequence to make it consistent |
| 5490 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5491 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5492 | mFocusedWindow->consumeMotionDown(); |
| 5493 | // This cancel is generated because the connection was unresponsive |
| 5494 | mFocusedWindow->consumeMotionCancel(); |
| 5495 | mFocusedWindow->assertNoEvents(); |
| 5496 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5497 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5498 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 5499 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5500 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5501 | } |
| 5502 | |
| 5503 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 5504 | // it doesn't matter which order they should have ANR. |
| 5505 | // But we should receive ANR for both. |
| 5506 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 5507 | // Set the timeout for unfocused window to match the focused window |
| 5508 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 5509 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 5510 | |
| 5511 | tapOnFocusedWindow(); |
| 5512 | // 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] | 5513 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; |
| 5514 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); |
| 5515 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5516 | |
| 5517 | // 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] | 5518 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 5519 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 5520 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 5521 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5522 | |
| 5523 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5524 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5525 | |
| 5526 | mFocusedWindow->consumeMotionDown(); |
| 5527 | mFocusedWindow->consumeMotionUp(); |
| 5528 | mUnfocusedWindow->consumeMotionOutside(); |
| 5529 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5530 | sp<IBinder> responsiveToken1, responsiveToken2; |
| 5531 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); |
| 5532 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5533 | |
| 5534 | // Both applications should be marked as responsive, in any order |
| 5535 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 5536 | mFocusedWindow->getToken() == responsiveToken2); |
| 5537 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 5538 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 5539 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5540 | } |
| 5541 | |
| 5542 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 5543 | // We should also log an error to account for the dropped event (not tested here). |
| 5544 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 5545 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 5546 | tapOnFocusedWindow(); |
| 5547 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 5548 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 5549 | // Receive the events, but don't respond |
| 5550 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 5551 | ASSERT_TRUE(downEventSequenceNum); |
| 5552 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 5553 | ASSERT_TRUE(upEventSequenceNum); |
| 5554 | const std::chrono::duration timeout = |
| 5555 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5556 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5557 | |
| 5558 | // Tap once again |
| 5559 | // 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] | 5560 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5561 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5562 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5563 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5564 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5565 | FOCUSED_WINDOW_LOCATION)); |
| 5566 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 5567 | // valid touch target |
| 5568 | mUnfocusedWindow->assertNoEvents(); |
| 5569 | |
| 5570 | // Consume the first tap |
| 5571 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 5572 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 5573 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5574 | // The second tap did not go to the focused window |
| 5575 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5576 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5577 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 5578 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5579 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5580 | } |
| 5581 | |
| 5582 | // If you tap outside of all windows, there will not be ANR |
| 5583 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5584 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5585 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5586 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 5587 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5588 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5589 | } |
| 5590 | |
| 5591 | // Since the focused window is paused, tapping on it should not produce any events |
| 5592 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 5593 | mFocusedWindow->setPaused(true); |
| 5594 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 5595 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5596 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5597 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5598 | FOCUSED_WINDOW_LOCATION)); |
| 5599 | |
| 5600 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 5601 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5602 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 5603 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5604 | |
| 5605 | mFocusedWindow->assertNoEvents(); |
| 5606 | mUnfocusedWindow->assertNoEvents(); |
| 5607 | } |
| 5608 | |
| 5609 | /** |
| 5610 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 5611 | * not to to the focused window until the motion is processed. |
| 5612 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 5613 | * |
| 5614 | * Warning!!! |
| 5615 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 5616 | * and the injection timeout that we specify when injecting the key. |
| 5617 | * We must have the injection timeout (10ms) be smaller than |
| 5618 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 5619 | * |
| 5620 | * If that value changes, this test should also change. |
| 5621 | */ |
| 5622 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 5623 | // Set a long ANR timeout to prevent it from triggering |
| 5624 | mFocusedWindow->setDispatchingTimeout(2s); |
| 5625 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 5626 | |
| 5627 | tapOnUnfocusedWindow(); |
| 5628 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 5629 | ASSERT_TRUE(downSequenceNum); |
| 5630 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 5631 | ASSERT_TRUE(upSequenceNum); |
| 5632 | // Don't finish the events yet, and send a key |
| 5633 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 5634 | // window even if motions are still being processed. |
| 5635 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5636 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5637 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5638 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 5639 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5640 | // Key will not be sent to the window, yet, because the window is still processing events |
| 5641 | // and the key remains pending, waiting for the touch events to be processed |
| 5642 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 5643 | ASSERT_FALSE(keySequenceNum); |
| 5644 | |
| 5645 | // 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] | 5646 | mFocusedWindow->setFocusable(false); |
| 5647 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5648 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5649 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5650 | |
| 5651 | // Focus events should precede the key events |
| 5652 | mUnfocusedWindow->consumeFocusEvent(true); |
| 5653 | mFocusedWindow->consumeFocusEvent(false); |
| 5654 | |
| 5655 | // Finish the tap events, which should unblock dispatcher |
| 5656 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 5657 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 5658 | |
| 5659 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 5660 | // can finally go to the newly focused "mUnfocusedWindow". |
| 5661 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5662 | mFocusedWindow->assertNoEvents(); |
| 5663 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5664 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5665 | } |
| 5666 | |
| 5667 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 5668 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 5669 | // The other window should not be affected by that. |
| 5670 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 5671 | // Touch Window 1 |
| 5672 | NotifyMotionArgs motionArgs = |
| 5673 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5674 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); |
| 5675 | mDispatcher->notifyMotion(&motionArgs); |
| 5676 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 5677 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 5678 | |
| 5679 | // Touch Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5680 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5681 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5682 | mDispatcher->notifyMotion(&motionArgs); |
| 5683 | |
| 5684 | const std::chrono::duration timeout = |
| 5685 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5686 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5687 | |
| 5688 | mUnfocusedWindow->consumeMotionDown(); |
| 5689 | mFocusedWindow->consumeMotionDown(); |
| 5690 | // Focused window may or may not receive ACTION_MOVE |
| 5691 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 5692 | InputEvent* event; |
| 5693 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 5694 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 5695 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 5696 | ASSERT_NE(nullptr, event); |
| 5697 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 5698 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 5699 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 5700 | mFocusedWindow->consumeMotionCancel(); |
| 5701 | } else { |
| 5702 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 5703 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5704 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5705 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 5706 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5707 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5708 | mUnfocusedWindow->assertNoEvents(); |
| 5709 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5710 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5711 | } |
| 5712 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5713 | /** |
| 5714 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 5715 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 5716 | * |
| 5717 | * If the user touches another application during this time, the key should be dropped. |
| 5718 | * Next, if a new focused window comes in, without toggling the focused application, |
| 5719 | * then no ANR should occur. |
| 5720 | * |
| 5721 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 5722 | * but in some cases the policy may not update the focused application. |
| 5723 | */ |
| 5724 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 5725 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 5726 | std::make_shared<FakeApplicationHandle>(); |
| 5727 | focusedApplication->setDispatchingTimeout(60ms); |
| 5728 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 5729 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 5730 | mFocusedWindow->setFocusable(false); |
| 5731 | |
| 5732 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 5733 | mFocusedWindow->consumeFocusEvent(false); |
| 5734 | |
| 5735 | // Send a key. The ANR timer should start because there is no focused window. |
| 5736 | // 'focusedApplication' will get blamed if this timer completes. |
| 5737 | // 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] | 5738 | InputEventInjectionResult result = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5739 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5740 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, |
| 5741 | false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5742 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5743 | |
| 5744 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 5745 | // then the injected touches won't cause the focused event to get dropped. |
| 5746 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 5747 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 5748 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 5749 | // For this test, it means that the key would get delivered to the window once it becomes |
| 5750 | // focused. |
| 5751 | std::this_thread::sleep_for(10ms); |
| 5752 | |
| 5753 | // Touch unfocused window. This should force the pending key to get dropped. |
| 5754 | NotifyMotionArgs motionArgs = |
| 5755 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5756 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); |
| 5757 | mDispatcher->notifyMotion(&motionArgs); |
| 5758 | |
| 5759 | // We do not consume the motion right away, because that would require dispatcher to first |
| 5760 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 5761 | // Set the focused window first. |
| 5762 | mFocusedWindow->setFocusable(true); |
| 5763 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 5764 | setFocusedWindow(mFocusedWindow); |
| 5765 | mFocusedWindow->consumeFocusEvent(true); |
| 5766 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 5767 | // to another application. This could be a bug / behaviour in the policy. |
| 5768 | |
| 5769 | mUnfocusedWindow->consumeMotionDown(); |
| 5770 | |
| 5771 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5772 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 5773 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 5774 | } |
| 5775 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5776 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 5777 | // that has feature NO_INPUT_CHANNEL. |
| 5778 | // Layout: |
| 5779 | // Top (closest to user) |
| 5780 | // mNoInputWindow (above all windows) |
| 5781 | // mBottomWindow |
| 5782 | // Bottom (furthest from user) |
| 5783 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 5784 | virtual void SetUp() override { |
| 5785 | InputDispatcherTest::SetUp(); |
| 5786 | |
| 5787 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5788 | mNoInputWindow = |
| 5789 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 5790 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
| 5791 | std::make_optional<sp<IBinder>>(nullptr) /*token*/); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5792 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5793 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5794 | // It's perfectly valid for this window to not have an associated input channel |
| 5795 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5796 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 5797 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5798 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5799 | |
| 5800 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 5801 | } |
| 5802 | |
| 5803 | protected: |
| 5804 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 5805 | sp<FakeWindowHandle> mNoInputWindow; |
| 5806 | sp<FakeWindowHandle> mBottomWindow; |
| 5807 | }; |
| 5808 | |
| 5809 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 5810 | PointF touchedPoint = {10, 10}; |
| 5811 | |
| 5812 | NotifyMotionArgs motionArgs = |
| 5813 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5814 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 5815 | mDispatcher->notifyMotion(&motionArgs); |
| 5816 | |
| 5817 | mNoInputWindow->assertNoEvents(); |
| 5818 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 5819 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 5820 | // and therefore should prevent mBottomWindow from receiving touches |
| 5821 | mBottomWindow->assertNoEvents(); |
| 5822 | } |
| 5823 | |
| 5824 | /** |
| 5825 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 5826 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 5827 | */ |
| 5828 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 5829 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5830 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 5831 | "Window with input channel and NO_INPUT_CHANNEL", |
| 5832 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5833 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5834 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5835 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5836 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 5837 | |
| 5838 | PointF touchedPoint = {10, 10}; |
| 5839 | |
| 5840 | NotifyMotionArgs motionArgs = |
| 5841 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5842 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 5843 | mDispatcher->notifyMotion(&motionArgs); |
| 5844 | |
| 5845 | mNoInputWindow->assertNoEvents(); |
| 5846 | mBottomWindow->assertNoEvents(); |
| 5847 | } |
| 5848 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5849 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 5850 | protected: |
| 5851 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 5852 | sp<FakeWindowHandle> mWindow; |
| 5853 | sp<FakeWindowHandle> mMirror; |
| 5854 | |
| 5855 | virtual void SetUp() override { |
| 5856 | InputDispatcherTest::SetUp(); |
| 5857 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5858 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 5859 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 5860 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5861 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 5862 | mWindow->setFocusable(true); |
| 5863 | mMirror->setFocusable(true); |
| 5864 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5865 | } |
| 5866 | }; |
| 5867 | |
| 5868 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 5869 | // Request focus on a mirrored window |
| 5870 | setFocusedWindow(mMirror); |
| 5871 | |
| 5872 | // window gets focused |
| 5873 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5874 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5875 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5876 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5877 | } |
| 5878 | |
| 5879 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 5880 | // focusable. |
| 5881 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 5882 | setFocusedWindow(mMirror); |
| 5883 | |
| 5884 | // window gets focused |
| 5885 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5886 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5887 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5888 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5889 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5890 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5891 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5892 | |
| 5893 | mMirror->setFocusable(false); |
| 5894 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5895 | |
| 5896 | // window loses focus since one of the windows associated with the token in not focusable |
| 5897 | mWindow->consumeFocusEvent(false); |
| 5898 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5899 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5900 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5901 | mWindow->assertNoEvents(); |
| 5902 | } |
| 5903 | |
| 5904 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 5905 | // invisible. |
| 5906 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 5907 | setFocusedWindow(mMirror); |
| 5908 | |
| 5909 | // window gets focused |
| 5910 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5911 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5912 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5913 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5914 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5915 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5916 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5917 | |
| 5918 | mMirror->setVisible(false); |
| 5919 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5920 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5921 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5922 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5923 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5924 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5925 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5926 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5927 | |
| 5928 | mWindow->setVisible(false); |
| 5929 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5930 | |
| 5931 | // window loses focus only after all windows associated with the token become invisible. |
| 5932 | mWindow->consumeFocusEvent(false); |
| 5933 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5934 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5935 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5936 | mWindow->assertNoEvents(); |
| 5937 | } |
| 5938 | |
| 5939 | // A focused & mirrored window remains focused until both windows are removed. |
| 5940 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 5941 | setFocusedWindow(mMirror); |
| 5942 | |
| 5943 | // window gets focused |
| 5944 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5945 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5946 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5947 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5948 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5949 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5950 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5951 | |
| 5952 | // single window is removed but the window token remains focused |
| 5953 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 5954 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5955 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5956 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5957 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5958 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5959 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5960 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5961 | |
| 5962 | // Both windows are removed |
| 5963 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 5964 | mWindow->consumeFocusEvent(false); |
| 5965 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5966 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5967 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5968 | mWindow->assertNoEvents(); |
| 5969 | } |
| 5970 | |
| 5971 | // Focus request can be pending until one window becomes visible. |
| 5972 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 5973 | // Request focus on an invisible mirror. |
| 5974 | mWindow->setVisible(false); |
| 5975 | mMirror->setVisible(false); |
| 5976 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5977 | setFocusedWindow(mMirror); |
| 5978 | |
| 5979 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5980 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5981 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5982 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5983 | |
| 5984 | mMirror->setVisible(true); |
| 5985 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5986 | |
| 5987 | // window gets focused |
| 5988 | mWindow->consumeFocusEvent(true); |
| 5989 | // window gets the pending key event |
| 5990 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5991 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5992 | |
| 5993 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 5994 | protected: |
| 5995 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 5996 | sp<FakeWindowHandle> mWindow; |
| 5997 | sp<FakeWindowHandle> mSecondWindow; |
| 5998 | |
| 5999 | void SetUp() override { |
| 6000 | InputDispatcherTest::SetUp(); |
| 6001 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6002 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6003 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6004 | mSecondWindow = |
| 6005 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6006 | mSecondWindow->setFocusable(true); |
| 6007 | |
| 6008 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 6009 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 6010 | |
| 6011 | setFocusedWindow(mWindow); |
| 6012 | mWindow->consumeFocusEvent(true); |
| 6013 | } |
| 6014 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6015 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
| 6016 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6017 | mDispatcher->notifyPointerCaptureChanged(&args); |
| 6018 | } |
| 6019 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6020 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 6021 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6022 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6023 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 6024 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6025 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6026 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6027 | } |
| 6028 | }; |
| 6029 | |
| 6030 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 6031 | // Ensure that capture cannot be obtained for unfocused windows. |
| 6032 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 6033 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 6034 | mSecondWindow->assertNoEvents(); |
| 6035 | |
| 6036 | // Ensure that capture can be enabled from the focus window. |
| 6037 | requestAndVerifyPointerCapture(mWindow, true); |
| 6038 | |
| 6039 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 6040 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 6041 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 6042 | |
| 6043 | // Ensure that capture can be disabled from the window with capture. |
| 6044 | requestAndVerifyPointerCapture(mWindow, false); |
| 6045 | } |
| 6046 | |
| 6047 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6048 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6049 | |
| 6050 | setFocusedWindow(mSecondWindow); |
| 6051 | |
| 6052 | // Ensure that the capture disabled event was sent first. |
| 6053 | mWindow->consumeCaptureEvent(false); |
| 6054 | mWindow->consumeFocusEvent(false); |
| 6055 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6056 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6057 | |
| 6058 | // 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] | 6059 | notifyPointerCaptureChanged({}); |
| 6060 | notifyPointerCaptureChanged(request); |
| 6061 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6062 | mWindow->assertNoEvents(); |
| 6063 | mSecondWindow->assertNoEvents(); |
| 6064 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 6065 | } |
| 6066 | |
| 6067 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6068 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6069 | |
| 6070 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6071 | notifyPointerCaptureChanged({}); |
| 6072 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6073 | |
| 6074 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6075 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6076 | mWindow->consumeCaptureEvent(false); |
| 6077 | mWindow->assertNoEvents(); |
| 6078 | } |
| 6079 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6080 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 6081 | requestAndVerifyPointerCapture(mWindow, true); |
| 6082 | |
| 6083 | // The first window loses focus. |
| 6084 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6085 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6086 | mWindow->consumeCaptureEvent(false); |
| 6087 | |
| 6088 | // Request Pointer Capture from the second window before the notification from InputReader |
| 6089 | // arrives. |
| 6090 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6091 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6092 | |
| 6093 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6094 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6095 | |
| 6096 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6097 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6098 | |
| 6099 | mSecondWindow->consumeFocusEvent(true); |
| 6100 | mSecondWindow->consumeCaptureEvent(true); |
| 6101 | } |
| 6102 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6103 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 6104 | // App repeatedly enables and disables capture. |
| 6105 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6106 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6107 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 6108 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 6109 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6110 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6111 | |
| 6112 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 6113 | // first request is now stale, this should do nothing. |
| 6114 | notifyPointerCaptureChanged(firstRequest); |
| 6115 | mWindow->assertNoEvents(); |
| 6116 | |
| 6117 | // InputReader notifies that the second request was enabled. |
| 6118 | notifyPointerCaptureChanged(secondRequest); |
| 6119 | mWindow->consumeCaptureEvent(true); |
| 6120 | } |
| 6121 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 6122 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 6123 | requestAndVerifyPointerCapture(mWindow, true); |
| 6124 | |
| 6125 | // App toggles pointer capture off and on. |
| 6126 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 6127 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 6128 | |
| 6129 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6130 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6131 | |
| 6132 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 6133 | // preceding "disable" request. |
| 6134 | notifyPointerCaptureChanged(enableRequest); |
| 6135 | |
| 6136 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 6137 | // any notifications. |
| 6138 | mWindow->assertNoEvents(); |
| 6139 | } |
| 6140 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6141 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 6142 | protected: |
| 6143 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6144 | |
| 6145 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 6146 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 6147 | |
| 6148 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 6149 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 6150 | |
| 6151 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 6152 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 6153 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 6154 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 6155 | MAXIMUM_OBSCURING_OPACITY); |
| 6156 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6157 | static const int32_t TOUCHED_APP_UID = 10001; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6158 | static const int32_t APP_B_UID = 10002; |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6159 | static const int32_t APP_C_UID = 10003; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6160 | |
| 6161 | sp<FakeWindowHandle> mTouchWindow; |
| 6162 | |
| 6163 | virtual void SetUp() override { |
| 6164 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6165 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6166 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 6167 | } |
| 6168 | |
| 6169 | virtual void TearDown() override { |
| 6170 | InputDispatcherTest::TearDown(); |
| 6171 | mTouchWindow.clear(); |
| 6172 | } |
| 6173 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6174 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, |
| 6175 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6176 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6177 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6178 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6179 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6180 | return window; |
| 6181 | } |
| 6182 | |
| 6183 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { |
| 6184 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 6185 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6186 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6187 | // Generate an arbitrary PID based on the UID |
| 6188 | window->setOwnerInfo(1777 + (uid % 10000), uid); |
| 6189 | return window; |
| 6190 | } |
| 6191 | |
| 6192 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
| 6193 | NotifyMotionArgs args = |
| 6194 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6195 | ADISPLAY_ID_DEFAULT, points); |
| 6196 | mDispatcher->notifyMotion(&args); |
| 6197 | } |
| 6198 | }; |
| 6199 | |
| 6200 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6201 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6202 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6203 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6204 | |
| 6205 | touch(); |
| 6206 | |
| 6207 | mTouchWindow->assertNoEvents(); |
| 6208 | } |
| 6209 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6210 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6211 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 6212 | const sp<FakeWindowHandle>& w = |
| 6213 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 6214 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6215 | |
| 6216 | touch(); |
| 6217 | |
| 6218 | mTouchWindow->assertNoEvents(); |
| 6219 | } |
| 6220 | |
| 6221 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6222 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 6223 | const sp<FakeWindowHandle>& w = |
| 6224 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6225 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6226 | |
| 6227 | touch(); |
| 6228 | |
| 6229 | w->assertNoEvents(); |
| 6230 | } |
| 6231 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6232 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6233 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 6234 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6235 | |
| 6236 | touch(); |
| 6237 | |
| 6238 | mTouchWindow->consumeAnyMotionDown(); |
| 6239 | } |
| 6240 | |
| 6241 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6242 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6243 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6244 | w->setFrame(Rect(0, 0, 50, 50)); |
| 6245 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6246 | |
| 6247 | touch({PointF{100, 100}}); |
| 6248 | |
| 6249 | mTouchWindow->consumeAnyMotionDown(); |
| 6250 | } |
| 6251 | |
| 6252 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6253 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6254 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6255 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6256 | |
| 6257 | touch(); |
| 6258 | |
| 6259 | mTouchWindow->consumeAnyMotionDown(); |
| 6260 | } |
| 6261 | |
| 6262 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 6263 | const sp<FakeWindowHandle>& w = |
| 6264 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 6265 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6266 | |
| 6267 | touch(); |
| 6268 | |
| 6269 | mTouchWindow->consumeAnyMotionDown(); |
| 6270 | } |
| 6271 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6272 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 6273 | const sp<FakeWindowHandle>& w = |
| 6274 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 6275 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6276 | |
| 6277 | touch(); |
| 6278 | |
| 6279 | w->assertNoEvents(); |
| 6280 | } |
| 6281 | |
| 6282 | /** |
| 6283 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 6284 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 6285 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 6286 | */ |
| 6287 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6288 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 6289 | const sp<FakeWindowHandle>& w = |
| 6290 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6291 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6292 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6293 | |
| 6294 | touch(); |
| 6295 | |
| 6296 | w->consumeMotionOutside(); |
| 6297 | } |
| 6298 | |
| 6299 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 6300 | const sp<FakeWindowHandle>& w = |
| 6301 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6302 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6303 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6304 | |
| 6305 | touch(); |
| 6306 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6307 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6308 | } |
| 6309 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6310 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6311 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6312 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6313 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6314 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6315 | |
| 6316 | touch(); |
| 6317 | |
| 6318 | mTouchWindow->consumeAnyMotionDown(); |
| 6319 | } |
| 6320 | |
| 6321 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 6322 | const sp<FakeWindowHandle>& w = |
| 6323 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6324 | MAXIMUM_OBSCURING_OPACITY); |
| 6325 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6326 | |
| 6327 | touch(); |
| 6328 | |
| 6329 | mTouchWindow->consumeAnyMotionDown(); |
| 6330 | } |
| 6331 | |
| 6332 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6333 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6334 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6335 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6336 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6337 | |
| 6338 | touch(); |
| 6339 | |
| 6340 | mTouchWindow->assertNoEvents(); |
| 6341 | } |
| 6342 | |
| 6343 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 6344 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 6345 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6346 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 6347 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6348 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6349 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 6350 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6351 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6352 | |
| 6353 | touch(); |
| 6354 | |
| 6355 | mTouchWindow->assertNoEvents(); |
| 6356 | } |
| 6357 | |
| 6358 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 6359 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 6360 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6361 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 6362 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6363 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6364 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 6365 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6366 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6367 | |
| 6368 | touch(); |
| 6369 | |
| 6370 | mTouchWindow->consumeAnyMotionDown(); |
| 6371 | } |
| 6372 | |
| 6373 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6374 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 6375 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6376 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6377 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6378 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6379 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6380 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6381 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 6382 | |
| 6383 | touch(); |
| 6384 | |
| 6385 | mTouchWindow->consumeAnyMotionDown(); |
| 6386 | } |
| 6387 | |
| 6388 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 6389 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6390 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6391 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6392 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6393 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6394 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6395 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6396 | |
| 6397 | touch(); |
| 6398 | |
| 6399 | mTouchWindow->assertNoEvents(); |
| 6400 | } |
| 6401 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6402 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6403 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 6404 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6405 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6406 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6407 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6408 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6409 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6410 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 6411 | |
| 6412 | touch(); |
| 6413 | |
| 6414 | mTouchWindow->assertNoEvents(); |
| 6415 | } |
| 6416 | |
| 6417 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6418 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 6419 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6420 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6421 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6422 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6423 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6424 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6425 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 6426 | |
| 6427 | touch(); |
| 6428 | |
| 6429 | mTouchWindow->consumeAnyMotionDown(); |
| 6430 | } |
| 6431 | |
| 6432 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 6433 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6434 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6435 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6436 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6437 | |
| 6438 | touch(); |
| 6439 | |
| 6440 | mTouchWindow->consumeAnyMotionDown(); |
| 6441 | } |
| 6442 | |
| 6443 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 6444 | const sp<FakeWindowHandle>& w = |
| 6445 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6446 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6447 | |
| 6448 | touch(); |
| 6449 | |
| 6450 | mTouchWindow->consumeAnyMotionDown(); |
| 6451 | } |
| 6452 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 6453 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6454 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 6455 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 6456 | const sp<FakeWindowHandle>& w = |
| 6457 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 6458 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6459 | |
| 6460 | touch(); |
| 6461 | |
| 6462 | mTouchWindow->assertNoEvents(); |
| 6463 | } |
| 6464 | |
| 6465 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 6466 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 6467 | const sp<FakeWindowHandle>& w = |
| 6468 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 6469 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6470 | |
| 6471 | touch(); |
| 6472 | |
| 6473 | mTouchWindow->consumeAnyMotionDown(); |
| 6474 | } |
| 6475 | |
| 6476 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6477 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 6478 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 6479 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6480 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6481 | OPACITY_ABOVE_THRESHOLD); |
| 6482 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6483 | |
| 6484 | touch(); |
| 6485 | |
| 6486 | mTouchWindow->consumeAnyMotionDown(); |
| 6487 | } |
| 6488 | |
| 6489 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6490 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 6491 | const sp<FakeWindowHandle>& w1 = |
| 6492 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 6493 | OPACITY_BELOW_THRESHOLD); |
| 6494 | const sp<FakeWindowHandle>& w2 = |
| 6495 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6496 | OPACITY_BELOW_THRESHOLD); |
| 6497 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6498 | |
| 6499 | touch(); |
| 6500 | |
| 6501 | mTouchWindow->assertNoEvents(); |
| 6502 | } |
| 6503 | |
| 6504 | /** |
| 6505 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 6506 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 6507 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 6508 | */ |
| 6509 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6510 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 6511 | const sp<FakeWindowHandle>& wB = |
| 6512 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 6513 | OPACITY_BELOW_THRESHOLD); |
| 6514 | const sp<FakeWindowHandle>& wC = |
| 6515 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6516 | OPACITY_BELOW_THRESHOLD); |
| 6517 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 6518 | |
| 6519 | touch(); |
| 6520 | |
| 6521 | mTouchWindow->assertNoEvents(); |
| 6522 | } |
| 6523 | |
| 6524 | /** |
| 6525 | * This test is testing that a window from a different UID but with same application token doesn't |
| 6526 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 6527 | */ |
| 6528 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6529 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 6530 | const sp<FakeWindowHandle>& w = |
| 6531 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6532 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 6533 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6534 | |
| 6535 | touch(); |
| 6536 | |
| 6537 | mTouchWindow->consumeAnyMotionDown(); |
| 6538 | } |
| 6539 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6540 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 6541 | protected: |
| 6542 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6543 | sp<FakeWindowHandle> mWindow; |
| 6544 | sp<FakeWindowHandle> mSecondWindow; |
| 6545 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6546 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6547 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 6548 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6549 | |
| 6550 | void SetUp() override { |
| 6551 | InputDispatcherTest::SetUp(); |
| 6552 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6553 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6554 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6555 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6556 | mSecondWindow = |
| 6557 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6558 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6559 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6560 | mSpyWindow = |
| 6561 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6562 | mSpyWindow->setSpy(true); |
| 6563 | mSpyWindow->setTrustedOverlay(true); |
| 6564 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 6565 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6566 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6567 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6568 | } |
| 6569 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6570 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 6571 | switch (fromSource) { |
| 6572 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 6573 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6574 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6575 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6576 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6577 | break; |
| 6578 | case AINPUT_SOURCE_STYLUS: |
| 6579 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6580 | injectMotionEvent( |
| 6581 | mDispatcher, |
| 6582 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 6583 | AINPUT_SOURCE_STYLUS) |
| 6584 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 6585 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6586 | .x(50) |
| 6587 | .y(50)) |
| 6588 | .build())); |
| 6589 | break; |
| 6590 | case AINPUT_SOURCE_MOUSE: |
| 6591 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6592 | injectMotionEvent( |
| 6593 | mDispatcher, |
| 6594 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 6595 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 6596 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 6597 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 6598 | .x(50) |
| 6599 | .y(50)) |
| 6600 | .build())); |
| 6601 | break; |
| 6602 | default: |
| 6603 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 6604 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6605 | |
| 6606 | // Window should receive motion event. |
| 6607 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6608 | // Spy window should also receive motion event |
| 6609 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6610 | } |
| 6611 | |
| 6612 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 6613 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 6614 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6615 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6616 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6617 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6618 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6619 | |
| 6620 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6621 | mDragWindow = |
| 6622 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6623 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6624 | mDispatcher->setInputWindows( |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6625 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6626 | |
| 6627 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6628 | bool transferred = |
| 6629 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
| 6630 | true /* isDragDrop */); |
| 6631 | if (transferred) { |
| 6632 | mWindow->consumeMotionCancel(); |
| 6633 | mDragWindow->consumeMotionDown(); |
| 6634 | } |
| 6635 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6636 | } |
| 6637 | }; |
| 6638 | |
| 6639 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6640 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6641 | |
| 6642 | // Move on window. |
| 6643 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6644 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6645 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6646 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6647 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6648 | mWindow->consumeDragEvent(false, 50, 50); |
| 6649 | mSecondWindow->assertNoEvents(); |
| 6650 | |
| 6651 | // Move to another window. |
| 6652 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6653 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6654 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 6655 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6656 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6657 | mWindow->consumeDragEvent(true, 150, 50); |
| 6658 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 6659 | |
| 6660 | // Move back to original window. |
| 6661 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6662 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6663 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6664 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6665 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6666 | mWindow->consumeDragEvent(false, 50, 50); |
| 6667 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 6668 | |
| 6669 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6670 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6671 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6672 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6673 | mWindow->assertNoEvents(); |
| 6674 | mSecondWindow->assertNoEvents(); |
| 6675 | } |
| 6676 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6677 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6678 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6679 | |
| 6680 | // No cancel event after drag start |
| 6681 | mSpyWindow->assertNoEvents(); |
| 6682 | |
| 6683 | const MotionEvent secondFingerDownEvent = |
| 6684 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6685 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6686 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6687 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(60).y(60)) |
| 6688 | .build(); |
| 6689 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6690 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6691 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6692 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6693 | |
| 6694 | // Receives cancel for first pointer after next pointer down |
| 6695 | mSpyWindow->consumeMotionCancel(); |
| 6696 | mSpyWindow->consumeMotionDown(); |
| 6697 | |
| 6698 | mSpyWindow->assertNoEvents(); |
| 6699 | } |
| 6700 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 6701 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6702 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 6703 | |
| 6704 | // Move on window. |
| 6705 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6706 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6707 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6708 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6709 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6710 | mWindow->consumeDragEvent(false, 50, 50); |
| 6711 | mSecondWindow->assertNoEvents(); |
| 6712 | |
| 6713 | // Move to another window. |
| 6714 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6715 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6716 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 6717 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6718 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6719 | mWindow->consumeDragEvent(true, 150, 50); |
| 6720 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 6721 | |
| 6722 | // drop to another window. |
| 6723 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6724 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6725 | {150, 50})) |
| 6726 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6727 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6728 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 6729 | mWindow->assertNoEvents(); |
| 6730 | mSecondWindow->assertNoEvents(); |
| 6731 | } |
| 6732 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 6733 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6734 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 6735 | |
| 6736 | // Move on window and keep button pressed. |
| 6737 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6738 | injectMotionEvent(mDispatcher, |
| 6739 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 6740 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 6741 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6742 | .x(50) |
| 6743 | .y(50)) |
| 6744 | .build())) |
| 6745 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6746 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6747 | mWindow->consumeDragEvent(false, 50, 50); |
| 6748 | mSecondWindow->assertNoEvents(); |
| 6749 | |
| 6750 | // Move to another window and release button, expect to drop item. |
| 6751 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6752 | injectMotionEvent(mDispatcher, |
| 6753 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 6754 | .buttonState(0) |
| 6755 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6756 | .x(150) |
| 6757 | .y(50)) |
| 6758 | .build())) |
| 6759 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6760 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6761 | mWindow->assertNoEvents(); |
| 6762 | mSecondWindow->assertNoEvents(); |
| 6763 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 6764 | |
| 6765 | // nothing to the window. |
| 6766 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6767 | injectMotionEvent(mDispatcher, |
| 6768 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 6769 | .buttonState(0) |
| 6770 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6771 | .x(150) |
| 6772 | .y(50)) |
| 6773 | .build())) |
| 6774 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6775 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6776 | mWindow->assertNoEvents(); |
| 6777 | mSecondWindow->assertNoEvents(); |
| 6778 | } |
| 6779 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6780 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6781 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 6782 | |
| 6783 | // Set second window invisible. |
| 6784 | mSecondWindow->setVisible(false); |
| 6785 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 6786 | |
| 6787 | // Move on window. |
| 6788 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6789 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6790 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6791 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6792 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6793 | mWindow->consumeDragEvent(false, 50, 50); |
| 6794 | mSecondWindow->assertNoEvents(); |
| 6795 | |
| 6796 | // Move to another window. |
| 6797 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6798 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6799 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 6800 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6801 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6802 | mWindow->consumeDragEvent(true, 150, 50); |
| 6803 | mSecondWindow->assertNoEvents(); |
| 6804 | |
| 6805 | // drop to another window. |
| 6806 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6807 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6808 | {150, 50})) |
| 6809 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6810 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6811 | mFakePolicy->assertDropTargetEquals(nullptr); |
| 6812 | mWindow->assertNoEvents(); |
| 6813 | mSecondWindow->assertNoEvents(); |
| 6814 | } |
| 6815 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6816 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6817 | // Ensure window could track pointerIds if it didn't support split touch. |
| 6818 | mWindow->setPreventSplitting(true); |
| 6819 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6820 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6821 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6822 | {50, 50})) |
| 6823 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6824 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6825 | |
| 6826 | const MotionEvent secondFingerDownEvent = |
| 6827 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6828 | .displayId(ADISPLAY_ID_DEFAULT) |
| 6829 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6830 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6831 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(75).y(50)) |
| 6832 | .build(); |
| 6833 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6834 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6835 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6836 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6837 | mWindow->consumeMotionPointerDown(1 /* pointerIndex */); |
| 6838 | |
| 6839 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6840 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6841 | } |
| 6842 | |
| 6843 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 6844 | // First down on second window. |
| 6845 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6846 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6847 | {150, 50})) |
| 6848 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6849 | |
| 6850 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6851 | |
| 6852 | // Second down on first window. |
| 6853 | const MotionEvent secondFingerDownEvent = |
| 6854 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6855 | .displayId(ADISPLAY_ID_DEFAULT) |
| 6856 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6857 | .pointer( |
| 6858 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 6859 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6860 | .build(); |
| 6861 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6862 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6863 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6864 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6865 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6866 | |
| 6867 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6868 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6869 | |
| 6870 | // Move on window. |
| 6871 | const MotionEvent secondFingerMoveEvent = |
| 6872 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6873 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6874 | .pointer( |
| 6875 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 6876 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6877 | .build(); |
| 6878 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6879 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 6880 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 6881 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6882 | mWindow->consumeDragEvent(false, 50, 50); |
| 6883 | mSecondWindow->consumeMotionMove(); |
| 6884 | |
| 6885 | // Release the drag pointer should perform drop. |
| 6886 | const MotionEvent secondFingerUpEvent = |
| 6887 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6888 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6889 | .pointer( |
| 6890 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 6891 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6892 | .build(); |
| 6893 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6894 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 6895 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 6896 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6897 | mFakePolicy->assertDropTargetEquals(mWindow->getToken()); |
| 6898 | mWindow->assertNoEvents(); |
| 6899 | mSecondWindow->consumeMotionMove(); |
| 6900 | } |
| 6901 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 6902 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6903 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 6904 | |
| 6905 | // Update window of second display. |
| 6906 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6907 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 6908 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 6909 | |
| 6910 | // Let second display has a touch state. |
| 6911 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6912 | injectMotionEvent(mDispatcher, |
| 6913 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 6914 | AINPUT_SOURCE_TOUCHSCREEN) |
| 6915 | .displayId(SECOND_DISPLAY_ID) |
| 6916 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 6917 | .x(100) |
| 6918 | .y(100)) |
| 6919 | .build())); |
| 6920 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 6921 | SECOND_DISPLAY_ID, 0 /* expectedFlag */); |
| 6922 | // Update window again. |
| 6923 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 6924 | |
| 6925 | // Move on window. |
| 6926 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6927 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6928 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6929 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6930 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6931 | mWindow->consumeDragEvent(false, 50, 50); |
| 6932 | mSecondWindow->assertNoEvents(); |
| 6933 | |
| 6934 | // Move to another window. |
| 6935 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6936 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6937 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 6938 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6939 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6940 | mWindow->consumeDragEvent(true, 150, 50); |
| 6941 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 6942 | |
| 6943 | // drop to another window. |
| 6944 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6945 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6946 | {150, 50})) |
| 6947 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6948 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6949 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 6950 | mWindow->assertNoEvents(); |
| 6951 | mSecondWindow->assertNoEvents(); |
| 6952 | } |
| 6953 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6954 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 6955 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 6956 | // Move on window. |
| 6957 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6958 | injectMotionEvent(mDispatcher, |
| 6959 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 6960 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 6961 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 6962 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 6963 | .x(50) |
| 6964 | .y(50)) |
| 6965 | .build())) |
| 6966 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6967 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6968 | mWindow->consumeDragEvent(false, 50, 50); |
| 6969 | mSecondWindow->assertNoEvents(); |
| 6970 | |
| 6971 | // Move to another window. |
| 6972 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6973 | injectMotionEvent(mDispatcher, |
| 6974 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 6975 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 6976 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 6977 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 6978 | .x(150) |
| 6979 | .y(50)) |
| 6980 | .build())) |
| 6981 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6982 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6983 | mWindow->consumeDragEvent(true, 150, 50); |
| 6984 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 6985 | |
| 6986 | // drop to another window. |
| 6987 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6988 | injectMotionEvent(mDispatcher, |
| 6989 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 6990 | .buttonState(0) |
| 6991 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 6992 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 6993 | .x(150) |
| 6994 | .y(50)) |
| 6995 | .build())) |
| 6996 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6997 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6998 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 6999 | mWindow->assertNoEvents(); |
| 7000 | mSecondWindow->assertNoEvents(); |
| 7001 | } |
| 7002 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7003 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 7004 | |
| 7005 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 7006 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7007 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7008 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7009 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7010 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7011 | window->setFocusable(true); |
| 7012 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 7013 | setFocusedWindow(window); |
| 7014 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 7015 | |
| 7016 | // With the flag set, window should not get any input |
| 7017 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 7018 | mDispatcher->notifyKey(&keyArgs); |
| 7019 | window->assertNoEvents(); |
| 7020 | |
| 7021 | NotifyMotionArgs motionArgs = |
| 7022 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7023 | ADISPLAY_ID_DEFAULT); |
| 7024 | mDispatcher->notifyMotion(&motionArgs); |
| 7025 | window->assertNoEvents(); |
| 7026 | |
| 7027 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7028 | window->setDropInput(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7029 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 7030 | |
| 7031 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 7032 | mDispatcher->notifyKey(&keyArgs); |
| 7033 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 7034 | |
| 7035 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7036 | ADISPLAY_ID_DEFAULT); |
| 7037 | mDispatcher->notifyMotion(&motionArgs); |
| 7038 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7039 | window->assertNoEvents(); |
| 7040 | } |
| 7041 | |
| 7042 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 7043 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 7044 | std::make_shared<FakeApplicationHandle>(); |
| 7045 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7046 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 7047 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7048 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 7049 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7050 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7051 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7052 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7053 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7054 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7055 | window->setOwnerInfo(222, 222); |
| 7056 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7057 | window->setFocusable(true); |
| 7058 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 7059 | setFocusedWindow(window); |
| 7060 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 7061 | |
| 7062 | // With the flag set, window should not get any input |
| 7063 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 7064 | mDispatcher->notifyKey(&keyArgs); |
| 7065 | window->assertNoEvents(); |
| 7066 | |
| 7067 | NotifyMotionArgs motionArgs = |
| 7068 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7069 | ADISPLAY_ID_DEFAULT); |
| 7070 | mDispatcher->notifyMotion(&motionArgs); |
| 7071 | window->assertNoEvents(); |
| 7072 | |
| 7073 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7074 | window->setDropInputIfObscured(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7075 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 7076 | |
| 7077 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 7078 | mDispatcher->notifyKey(&keyArgs); |
| 7079 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 7080 | |
| 7081 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7082 | ADISPLAY_ID_DEFAULT); |
| 7083 | mDispatcher->notifyMotion(&motionArgs); |
| 7084 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 7085 | window->assertNoEvents(); |
| 7086 | } |
| 7087 | |
| 7088 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 7089 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 7090 | std::make_shared<FakeApplicationHandle>(); |
| 7091 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7092 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 7093 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7094 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 7095 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7096 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7097 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7098 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7099 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7100 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7101 | window->setOwnerInfo(222, 222); |
| 7102 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7103 | window->setFocusable(true); |
| 7104 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 7105 | setFocusedWindow(window); |
| 7106 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 7107 | |
| 7108 | // With the flag set, window should not get any input |
| 7109 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 7110 | mDispatcher->notifyKey(&keyArgs); |
| 7111 | window->assertNoEvents(); |
| 7112 | |
| 7113 | NotifyMotionArgs motionArgs = |
| 7114 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7115 | ADISPLAY_ID_DEFAULT); |
| 7116 | mDispatcher->notifyMotion(&motionArgs); |
| 7117 | window->assertNoEvents(); |
| 7118 | |
| 7119 | // When the window is no longer obscured because it went on top, it should get input |
| 7120 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 7121 | |
| 7122 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 7123 | mDispatcher->notifyKey(&keyArgs); |
| 7124 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 7125 | |
| 7126 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7127 | ADISPLAY_ID_DEFAULT); |
| 7128 | mDispatcher->notifyMotion(&motionArgs); |
| 7129 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7130 | window->assertNoEvents(); |
| 7131 | } |
| 7132 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7133 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 7134 | protected: |
| 7135 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7136 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7137 | sp<FakeWindowHandle> mWindow; |
| 7138 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7139 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7140 | |
| 7141 | void SetUp() override { |
| 7142 | InputDispatcherTest::SetUp(); |
| 7143 | |
| 7144 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7145 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7146 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7147 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7148 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7149 | mSecondWindow = |
| 7150 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7151 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7152 | mThirdWindow = |
| 7153 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 7154 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 7155 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7156 | |
| 7157 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7158 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}, |
| 7159 | {SECOND_DISPLAY_ID, {mThirdWindow}}}); |
| 7160 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7161 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7162 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7163 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 7164 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7165 | WINDOW_UID, true /* hasPermission */, |
| 7166 | ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7167 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 7168 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7169 | mThirdWindow->assertNoEvents(); |
| 7170 | } |
| 7171 | |
| 7172 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 7173 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
| 7174 | SECONDARY_WINDOW_UID, true /* hasPermission */, |
| 7175 | SECOND_DISPLAY_ID)) { |
| 7176 | mWindow->assertNoEvents(); |
| 7177 | mSecondWindow->assertNoEvents(); |
| 7178 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7179 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7180 | } |
| 7181 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7182 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid, |
| 7183 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7184 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 7185 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7186 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 7187 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7188 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7189 | } |
| 7190 | }; |
| 7191 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7192 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7193 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7194 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 7195 | windowInfo.ownerPid, windowInfo.ownerUid, |
| 7196 | false /* hasPermission */); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7197 | } |
| 7198 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7199 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 7200 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7201 | int32_t ownerPid = windowInfo.ownerPid; |
| 7202 | int32_t ownerUid = windowInfo.ownerUid; |
| 7203 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
| 7204 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7205 | ownerUid, false /*hasPermission*/, |
| 7206 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7207 | mWindow->assertNoEvents(); |
| 7208 | mSecondWindow->assertNoEvents(); |
| 7209 | } |
| 7210 | |
| 7211 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 7212 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7213 | int32_t ownerPid = windowInfo.ownerPid; |
| 7214 | int32_t ownerUid = windowInfo.ownerUid; |
| 7215 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7216 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
| 7217 | ownerUid, true /*hasPermission*/); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7218 | } |
| 7219 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7220 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7221 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7222 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 7223 | windowInfo.ownerPid, windowInfo.ownerUid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7224 | true /*hasPermission*/, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7225 | mWindow->assertNoEvents(); |
| 7226 | mSecondWindow->assertNoEvents(); |
| 7227 | } |
| 7228 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7229 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 7230 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 7231 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 7232 | windowInfo.ownerPid, windowInfo.ownerUid, |
| 7233 | true /*hasPermission*/, SECOND_DISPLAY_ID)); |
| 7234 | mWindow->assertNoEvents(); |
| 7235 | mSecondWindow->assertNoEvents(); |
| 7236 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 7237 | } |
| 7238 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7239 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 7240 | // Interact with the window first. |
| 7241 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 7242 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 7243 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7244 | |
| 7245 | // Then remove focus. |
| 7246 | mWindow->setFocusable(false); |
| 7247 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 7248 | |
| 7249 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 7250 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7251 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 7252 | windowInfo.ownerPid, windowInfo.ownerUid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7253 | false /*hasPermission*/, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7254 | } |
| 7255 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7256 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 7257 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7258 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7259 | std::shared_ptr<FakeApplicationHandle> application = |
| 7260 | std::make_shared<FakeApplicationHandle>(); |
| 7261 | std::string name = "Fake Spy "; |
| 7262 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7263 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7264 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7265 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7266 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7267 | return spy; |
| 7268 | } |
| 7269 | |
| 7270 | sp<FakeWindowHandle> createForeground() { |
| 7271 | std::shared_ptr<FakeApplicationHandle> application = |
| 7272 | std::make_shared<FakeApplicationHandle>(); |
| 7273 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7274 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7275 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7276 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7277 | return window; |
| 7278 | } |
| 7279 | |
| 7280 | private: |
| 7281 | int mSpyCount{0}; |
| 7282 | }; |
| 7283 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7284 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7285 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7286 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 7287 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7288 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
| 7289 | ScopedSilentDeath _silentDeath; |
| 7290 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7291 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7292 | spy->setTrustedOverlay(false); |
| 7293 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), |
| 7294 | ".* not a trusted overlay"); |
| 7295 | } |
| 7296 | |
| 7297 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7298 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 7299 | */ |
| 7300 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7301 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7302 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); |
| 7303 | |
| 7304 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7305 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7306 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7307 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7308 | } |
| 7309 | |
| 7310 | /** |
| 7311 | * Verify the order in which different input windows receive events. The touched foreground window |
| 7312 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 7313 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 7314 | * receive events before ones belows it. |
| 7315 | * |
| 7316 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 7317 | * spy1, spy2, window, spy3. |
| 7318 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 7319 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 7320 | * window. |
| 7321 | */ |
| 7322 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 7323 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7324 | auto spy1 = createSpy(); |
| 7325 | auto spy2 = createSpy(); |
| 7326 | auto spy3 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7327 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); |
| 7328 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 7329 | const size_t numChannels = channels.size(); |
| 7330 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 7331 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7332 | if (!epollFd.ok()) { |
| 7333 | FAIL() << "Failed to create epoll fd"; |
| 7334 | } |
| 7335 | |
| 7336 | for (size_t i = 0; i < numChannels; i++) { |
| 7337 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 7338 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 7339 | FAIL() << "Failed to add fd to epoll"; |
| 7340 | } |
| 7341 | } |
| 7342 | |
| 7343 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7344 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7345 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7346 | |
| 7347 | std::vector<size_t> eventOrder; |
| 7348 | std::vector<struct epoll_event> events(numChannels); |
| 7349 | for (;;) { |
| 7350 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 7351 | (100ms).count()); |
| 7352 | if (nFds < 0) { |
| 7353 | FAIL() << "Failed to call epoll_wait"; |
| 7354 | } |
| 7355 | if (nFds == 0) { |
| 7356 | break; // epoll_wait timed out |
| 7357 | } |
| 7358 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 7359 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 7360 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7361 | channels[i]->consumeMotionDown(); |
| 7362 | } |
| 7363 | } |
| 7364 | |
| 7365 | // Verify the order in which the events were received. |
| 7366 | EXPECT_EQ(3u, eventOrder.size()); |
| 7367 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 7368 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 7369 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 7370 | } |
| 7371 | |
| 7372 | /** |
| 7373 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 7374 | */ |
| 7375 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 7376 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7377 | auto spy = createSpy(); |
| 7378 | spy->setTouchable(false); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7379 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7380 | |
| 7381 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7382 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7383 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7384 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7385 | spy->assertNoEvents(); |
| 7386 | } |
| 7387 | |
| 7388 | /** |
| 7389 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 7390 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 7391 | * to the window. |
| 7392 | */ |
| 7393 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 7394 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7395 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7396 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
| 7397 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7398 | |
| 7399 | // Inject an event outside the spy window's touchable region. |
| 7400 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7401 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7402 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7403 | window->consumeMotionDown(); |
| 7404 | spy->assertNoEvents(); |
| 7405 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7406 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7407 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7408 | window->consumeMotionUp(); |
| 7409 | spy->assertNoEvents(); |
| 7410 | |
| 7411 | // Inject an event inside the spy window's touchable region. |
| 7412 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7413 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7414 | {5, 10})) |
| 7415 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7416 | window->consumeMotionDown(); |
| 7417 | spy->consumeMotionDown(); |
| 7418 | } |
| 7419 | |
| 7420 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7421 | * 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] | 7422 | * 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] | 7423 | */ |
| 7424 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 7425 | auto window = createForeground(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7426 | window->setOwnerInfo(12, 34); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7427 | auto spy = createSpy(); |
| 7428 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7429 | spy->setOwnerInfo(56, 78); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7430 | spy->setFrame(Rect{0, 0, 20, 20}); |
| 7431 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7432 | |
| 7433 | // Inject an event outside the spy window's frame and touchable region. |
| 7434 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7435 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7436 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7437 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7438 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7439 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7440 | } |
| 7441 | |
| 7442 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7443 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 7444 | * pointers that are down within its bounds. |
| 7445 | */ |
| 7446 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 7447 | auto windowLeft = createForeground(); |
| 7448 | windowLeft->setFrame({0, 0, 100, 200}); |
| 7449 | auto windowRight = createForeground(); |
| 7450 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7451 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7452 | spy->setFrame({0, 0, 200, 200}); |
| 7453 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); |
| 7454 | |
| 7455 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7456 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7457 | {50, 50})) |
| 7458 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7459 | windowLeft->consumeMotionDown(); |
| 7460 | spy->consumeMotionDown(); |
| 7461 | |
| 7462 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7463 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7464 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7465 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7466 | .pointer( |
| 7467 | PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7468 | .build(); |
| 7469 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7470 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7471 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7472 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7473 | windowRight->consumeMotionDown(); |
| 7474 | spy->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 7475 | } |
| 7476 | |
| 7477 | /** |
| 7478 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 7479 | * the spy should receive the second pointer with ACTION_DOWN. |
| 7480 | */ |
| 7481 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 7482 | auto window = createForeground(); |
| 7483 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7484 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7485 | spyRight->setFrame({100, 0, 200, 200}); |
| 7486 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); |
| 7487 | |
| 7488 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7489 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7490 | {50, 50})) |
| 7491 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7492 | window->consumeMotionDown(); |
| 7493 | spyRight->assertNoEvents(); |
| 7494 | |
| 7495 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7496 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7497 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7498 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7499 | .pointer( |
| 7500 | PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7501 | .build(); |
| 7502 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7503 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7504 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7505 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7506 | window->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 7507 | spyRight->consumeMotionDown(); |
| 7508 | } |
| 7509 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7510 | /** |
| 7511 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 7512 | * windows should be allowed to control split touch. |
| 7513 | */ |
| 7514 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7515 | // 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] | 7516 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7517 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7518 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7519 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7520 | auto window = createForeground(); |
| 7521 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7522 | |
| 7523 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7524 | |
| 7525 | // First finger down, no window touched. |
| 7526 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7527 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7528 | {100, 200})) |
| 7529 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7530 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7531 | window->assertNoEvents(); |
| 7532 | |
| 7533 | // Second finger down on window, the window should receive touch down. |
| 7534 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7535 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7536 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7537 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7538 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7539 | .x(100) |
| 7540 | .y(200)) |
| 7541 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7542 | .build(); |
| 7543 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7544 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7545 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7546 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7547 | |
| 7548 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7549 | spy->consumeMotionPointerDown(1 /* pointerIndex */); |
| 7550 | } |
| 7551 | |
| 7552 | /** |
| 7553 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 7554 | * do not receive key events. |
| 7555 | */ |
| 7556 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7557 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7558 | spy->setFocusable(false); |
| 7559 | |
| 7560 | auto window = createForeground(); |
| 7561 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7562 | setFocusedWindow(window); |
| 7563 | window->consumeFocusEvent(true); |
| 7564 | |
| 7565 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7566 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 7567 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7568 | |
| 7569 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7570 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 7571 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7572 | |
| 7573 | spy->assertNoEvents(); |
| 7574 | } |
| 7575 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 7576 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 7577 | |
| 7578 | /** |
| 7579 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 7580 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 7581 | */ |
| 7582 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 7583 | auto window = createForeground(); |
| 7584 | auto spy1 = createSpy(); |
| 7585 | auto spy2 = createSpy(); |
| 7586 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); |
| 7587 | |
| 7588 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7589 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7590 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7591 | window->consumeMotionDown(); |
| 7592 | spy1->consumeMotionDown(); |
| 7593 | spy2->consumeMotionDown(); |
| 7594 | |
| 7595 | // Pilfer pointers from the second spy window. |
| 7596 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 7597 | spy2->assertNoEvents(); |
| 7598 | spy1->consumeMotionCancel(); |
| 7599 | window->consumeMotionCancel(); |
| 7600 | |
| 7601 | // The rest of the gesture should only be sent to the second spy window. |
| 7602 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7603 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7604 | ADISPLAY_ID_DEFAULT)) |
| 7605 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7606 | spy2->consumeMotionMove(); |
| 7607 | spy1->assertNoEvents(); |
| 7608 | window->assertNoEvents(); |
| 7609 | } |
| 7610 | |
| 7611 | /** |
| 7612 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 7613 | * in the middle of the gesture. |
| 7614 | */ |
| 7615 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 7616 | auto window = createForeground(); |
| 7617 | auto spy = createSpy(); |
| 7618 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7619 | |
| 7620 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7621 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7622 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7623 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7624 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7625 | |
| 7626 | window->releaseChannel(); |
| 7627 | |
| 7628 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 7629 | |
| 7630 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7631 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7632 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7633 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7634 | } |
| 7635 | |
| 7636 | /** |
| 7637 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 7638 | * the spy, but not to any other windows. |
| 7639 | */ |
| 7640 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 7641 | auto spy = createSpy(); |
| 7642 | auto window = createForeground(); |
| 7643 | |
| 7644 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7645 | |
| 7646 | // First finger down on the window and the spy. |
| 7647 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7648 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7649 | {100, 200})) |
| 7650 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7651 | spy->consumeMotionDown(); |
| 7652 | window->consumeMotionDown(); |
| 7653 | |
| 7654 | // Spy window pilfers the pointers. |
| 7655 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 7656 | window->consumeMotionCancel(); |
| 7657 | |
| 7658 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 7659 | const MotionEvent secondFingerDownEvent = |
| 7660 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7661 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7662 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7663 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7664 | .x(100) |
| 7665 | .y(200)) |
| 7666 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7667 | .build(); |
| 7668 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7669 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7670 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7671 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7672 | |
| 7673 | spy->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 7674 | |
| 7675 | // Third finger goes down outside all windows, so injection should fail. |
| 7676 | const MotionEvent thirdFingerDownEvent = |
| 7677 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7678 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7679 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7680 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7681 | .x(100) |
| 7682 | .y(200)) |
| 7683 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7684 | .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-5).y(-5)) |
| 7685 | .build(); |
| 7686 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 7687 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7688 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7689 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7690 | |
| 7691 | spy->assertNoEvents(); |
| 7692 | window->assertNoEvents(); |
| 7693 | } |
| 7694 | |
| 7695 | /** |
| 7696 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 7697 | */ |
| 7698 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 7699 | auto spy = createSpy(); |
| 7700 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 7701 | auto window = createForeground(); |
| 7702 | window->setFrame(Rect(0, 0, 200, 200)); |
| 7703 | |
| 7704 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7705 | |
| 7706 | // First finger down on the window only |
| 7707 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7708 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7709 | {150, 150})) |
| 7710 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7711 | window->consumeMotionDown(); |
| 7712 | |
| 7713 | // Second finger down on the spy and window |
| 7714 | const MotionEvent secondFingerDownEvent = |
| 7715 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7716 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7717 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7718 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7719 | .x(150) |
| 7720 | .y(150)) |
| 7721 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 7722 | .build(); |
| 7723 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7724 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7725 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7726 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7727 | spy->consumeMotionDown(); |
| 7728 | window->consumeMotionPointerDown(1); |
| 7729 | |
| 7730 | // Third finger down on the spy and window |
| 7731 | const MotionEvent thirdFingerDownEvent = |
| 7732 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7733 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7734 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7735 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7736 | .x(150) |
| 7737 | .y(150)) |
| 7738 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 7739 | .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7740 | .build(); |
| 7741 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7742 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7743 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7744 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7745 | spy->consumeMotionPointerDown(1); |
| 7746 | window->consumeMotionPointerDown(2); |
| 7747 | |
| 7748 | // Spy window pilfers the pointers. |
| 7749 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 7750 | window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 7751 | window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 7752 | |
| 7753 | spy->assertNoEvents(); |
| 7754 | window->assertNoEvents(); |
| 7755 | } |
| 7756 | |
| 7757 | /** |
| 7758 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 7759 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 7760 | * window, then that window should receive ACTION_CANCEL. |
| 7761 | */ |
| 7762 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 7763 | auto spy = createSpy(); |
| 7764 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 7765 | auto window = createForeground(); |
| 7766 | window->setFrame(Rect(0, 0, 200, 200)); |
| 7767 | |
| 7768 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7769 | |
| 7770 | // First finger down on both spy and window |
| 7771 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7772 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7773 | {10, 10})) |
| 7774 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7775 | window->consumeMotionDown(); |
| 7776 | spy->consumeMotionDown(); |
| 7777 | |
| 7778 | // Second finger down on the spy and window |
| 7779 | const MotionEvent secondFingerDownEvent = |
| 7780 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7781 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7782 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7783 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 7784 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7785 | .build(); |
| 7786 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7787 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7788 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7789 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7790 | spy->consumeMotionPointerDown(1); |
| 7791 | window->consumeMotionPointerDown(1); |
| 7792 | |
| 7793 | // Spy window pilfers the pointers. |
| 7794 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 7795 | window->consumeMotionCancel(); |
| 7796 | |
| 7797 | spy->assertNoEvents(); |
| 7798 | window->assertNoEvents(); |
| 7799 | } |
| 7800 | |
| 7801 | /** |
| 7802 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 7803 | * be sent to other windows |
| 7804 | */ |
| 7805 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 7806 | auto spy = createSpy(); |
| 7807 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 7808 | auto window = createForeground(); |
| 7809 | window->setFrame(Rect(0, 0, 200, 200)); |
| 7810 | |
| 7811 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7812 | |
| 7813 | // First finger down on both window and spy |
| 7814 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7815 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7816 | {10, 10})) |
| 7817 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7818 | window->consumeMotionDown(); |
| 7819 | spy->consumeMotionDown(); |
| 7820 | |
| 7821 | // Spy window pilfers the pointers. |
| 7822 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 7823 | window->consumeMotionCancel(); |
| 7824 | |
| 7825 | // Second finger down on the window only |
| 7826 | const MotionEvent secondFingerDownEvent = |
| 7827 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7828 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7829 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7830 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 7831 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7832 | .x(150) |
| 7833 | .y(150)) |
| 7834 | .build(); |
| 7835 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7836 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7837 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7838 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7839 | window->consumeMotionDown(); |
| 7840 | window->assertNoEvents(); |
| 7841 | |
| 7842 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 7843 | spy->consumeMotionMove(); |
| 7844 | spy->assertNoEvents(); |
| 7845 | } |
| 7846 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7847 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 7848 | public: |
| 7849 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 7850 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 7851 | std::make_shared<FakeApplicationHandle>(); |
| 7852 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7853 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 7854 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7855 | overlay->setFocusable(false); |
| 7856 | overlay->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7857 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7858 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7859 | overlay->setTrustedOverlay(true); |
| 7860 | |
| 7861 | std::shared_ptr<FakeApplicationHandle> application = |
| 7862 | std::make_shared<FakeApplicationHandle>(); |
| 7863 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7864 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 7865 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7866 | window->setFocusable(true); |
| 7867 | window->setOwnerInfo(222, 222); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7868 | |
| 7869 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7870 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 7871 | setFocusedWindow(window); |
| 7872 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 7873 | return {std::move(overlay), std::move(window)}; |
| 7874 | } |
| 7875 | |
| 7876 | void sendFingerEvent(int32_t action) { |
| 7877 | NotifyMotionArgs motionArgs = |
| 7878 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 7879 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}}); |
| 7880 | mDispatcher->notifyMotion(&motionArgs); |
| 7881 | } |
| 7882 | |
| 7883 | void sendStylusEvent(int32_t action) { |
| 7884 | NotifyMotionArgs motionArgs = |
| 7885 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 7886 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
| 7887 | motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 7888 | mDispatcher->notifyMotion(&motionArgs); |
| 7889 | } |
| 7890 | }; |
| 7891 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7892 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 7893 | |
| 7894 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
| 7895 | ScopedSilentDeath _silentDeath; |
| 7896 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7897 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 7898 | overlay->setTrustedOverlay(false); |
| 7899 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
| 7900 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), |
| 7901 | ".* not a trusted overlay"); |
| 7902 | } |
| 7903 | |
| 7904 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 7905 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 7906 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 7907 | |
| 7908 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 7909 | overlay->consumeMotionDown(); |
| 7910 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 7911 | overlay->consumeMotionUp(); |
| 7912 | |
| 7913 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 7914 | window->consumeMotionDown(); |
| 7915 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 7916 | window->consumeMotionUp(); |
| 7917 | |
| 7918 | overlay->assertNoEvents(); |
| 7919 | window->assertNoEvents(); |
| 7920 | } |
| 7921 | |
| 7922 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 7923 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7924 | overlay->setSpy(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7925 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 7926 | |
| 7927 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 7928 | overlay->consumeMotionDown(); |
| 7929 | window->consumeMotionDown(); |
| 7930 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 7931 | overlay->consumeMotionUp(); |
| 7932 | window->consumeMotionUp(); |
| 7933 | |
| 7934 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 7935 | window->consumeMotionDown(); |
| 7936 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 7937 | window->consumeMotionUp(); |
| 7938 | |
| 7939 | overlay->assertNoEvents(); |
| 7940 | window->assertNoEvents(); |
| 7941 | } |
| 7942 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 7943 | /** |
| 7944 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 7945 | * The scenario is as follows: |
| 7946 | * - The stylus interceptor overlay is configured as a spy window. |
| 7947 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 7948 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 7949 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 7950 | */ |
| 7951 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 7952 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 7953 | overlay->setSpy(true); |
| 7954 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 7955 | |
| 7956 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 7957 | overlay->consumeMotionDown(); |
| 7958 | window->consumeMotionDown(); |
| 7959 | |
| 7960 | // The interceptor pilfers the pointers. |
| 7961 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 7962 | window->consumeMotionCancel(); |
| 7963 | |
| 7964 | // The interceptor configures itself so that it is no longer a spy. |
| 7965 | overlay->setSpy(false); |
| 7966 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 7967 | |
| 7968 | // It continues to receive the rest of the stylus gesture. |
| 7969 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 7970 | overlay->consumeMotionMove(); |
| 7971 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 7972 | overlay->consumeMotionUp(); |
| 7973 | |
| 7974 | window->assertNoEvents(); |
| 7975 | } |
| 7976 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 7977 | struct User { |
| 7978 | int32_t mPid; |
| 7979 | int32_t mUid; |
| 7980 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 7981 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 7982 | |
| 7983 | User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid) |
| 7984 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 7985 | |
| 7986 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
| 7987 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
| 7988 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 7989 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 7990 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 7991 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 7992 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 7993 | } |
| 7994 | |
| 7995 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
| 7996 | return inputdispatcher::injectKey(mDispatcher, action, 0 /* repeatCount*/, ADISPLAY_ID_NONE, |
| 7997 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 7998 | INJECT_EVENT_TIMEOUT, false /*allowKeyRepeat*/, {mUid}, |
| 7999 | mPolicyFlags); |
| 8000 | } |
| 8001 | |
| 8002 | sp<FakeWindowHandle> createWindow() const { |
| 8003 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 8004 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8005 | sp<FakeWindowHandle> window = |
| 8006 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window", |
| 8007 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8008 | window->setOwnerInfo(mPid, mUid); |
| 8009 | return window; |
| 8010 | } |
| 8011 | }; |
| 8012 | |
| 8013 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 8014 | |
| 8015 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
| 8016 | auto owner = User(mDispatcher, 10, 11); |
| 8017 | auto window = owner.createWindow(); |
| 8018 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8019 | |
| 8020 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8021 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8022 | window->consumeMotionDown(); |
| 8023 | |
| 8024 | setFocusedWindow(window); |
| 8025 | window->consumeFocusEvent(true); |
| 8026 | |
| 8027 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8028 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 8029 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8030 | } |
| 8031 | |
| 8032 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
| 8033 | auto owner = User(mDispatcher, 10, 11); |
| 8034 | auto window = owner.createWindow(); |
| 8035 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8036 | |
| 8037 | auto rando = User(mDispatcher, 20, 21); |
| 8038 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 8039 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8040 | |
| 8041 | setFocusedWindow(window); |
| 8042 | window->consumeFocusEvent(true); |
| 8043 | |
| 8044 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 8045 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 8046 | window->assertNoEvents(); |
| 8047 | } |
| 8048 | |
| 8049 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
| 8050 | auto owner = User(mDispatcher, 10, 11); |
| 8051 | auto window = owner.createWindow(); |
| 8052 | auto spy = owner.createWindow(); |
| 8053 | spy->setSpy(true); |
| 8054 | spy->setTrustedOverlay(true); |
| 8055 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8056 | |
| 8057 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8058 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8059 | spy->consumeMotionDown(); |
| 8060 | window->consumeMotionDown(); |
| 8061 | } |
| 8062 | |
| 8063 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
| 8064 | auto owner = User(mDispatcher, 10, 11); |
| 8065 | auto window = owner.createWindow(); |
| 8066 | |
| 8067 | auto rando = User(mDispatcher, 20, 21); |
| 8068 | auto randosSpy = rando.createWindow(); |
| 8069 | randosSpy->setSpy(true); |
| 8070 | randosSpy->setTrustedOverlay(true); |
| 8071 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 8072 | |
| 8073 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 8074 | // not receive the event. |
| 8075 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8076 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8077 | randosSpy->assertNoEvents(); |
| 8078 | window->consumeMotionDown(); |
| 8079 | } |
| 8080 | |
| 8081 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
| 8082 | auto owner = User(mDispatcher, 10, 11); |
| 8083 | auto window = owner.createWindow(); |
| 8084 | |
| 8085 | auto rando = User(mDispatcher, 20, 21); |
| 8086 | auto randosSpy = rando.createWindow(); |
| 8087 | randosSpy->setSpy(true); |
| 8088 | randosSpy->setTrustedOverlay(true); |
| 8089 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 8090 | |
| 8091 | // A user that has injection permission can inject into any window. |
| 8092 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8093 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8094 | ADISPLAY_ID_DEFAULT)); |
| 8095 | randosSpy->consumeMotionDown(); |
| 8096 | window->consumeMotionDown(); |
| 8097 | |
| 8098 | setFocusedWindow(randosSpy); |
| 8099 | randosSpy->consumeFocusEvent(true); |
| 8100 | |
| 8101 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
| 8102 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8103 | window->assertNoEvents(); |
| 8104 | } |
| 8105 | |
| 8106 | TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) { |
| 8107 | auto owner = User(mDispatcher, 10, 11); |
| 8108 | auto window = owner.createWindow(); |
| 8109 | |
| 8110 | auto rando = User(mDispatcher, 20, 21); |
| 8111 | auto randosWindow = rando.createWindow(); |
| 8112 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 8113 | randosWindow->setWatchOutsideTouch(true); |
| 8114 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); |
| 8115 | |
| 8116 | // We allow generation of ACTION_OUTSIDE events into windows owned by different uids. |
| 8117 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8118 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8119 | window->consumeMotionDown(); |
| 8120 | randosWindow->consumeMotionOutside(); |
| 8121 | } |
| 8122 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 8123 | } // namespace android::inputdispatcher |