| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2010 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 17 | #include "../dispatcher/InputDispatcher.h" | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 18 |  | 
| Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 19 | #include <android-base/properties.h> | 
| Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 20 | #include <android-base/silent_death_test.h> | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 21 | #include <android-base/stringprintf.h> | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 22 | #include <android-base/thread_annotations.h> | 
| Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 23 | #include <binder/Binder.h> | 
| Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 24 | #include <fcntl.h> | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 25 | #include <gmock/gmock.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 26 | #include <gtest/gtest.h> | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 27 | #include <input/Input.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 28 | #include <linux/input.h> | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 29 | #include <sys/epoll.h> | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 30 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 31 | #include <cinttypes> | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 32 | #include <thread> | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 33 | #include <unordered_set> | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 34 | #include <vector> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 35 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 36 | using android::base::StringPrintf; | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 37 | using android::gui::FocusRequest; | 
|  | 38 | using android::gui::TouchOcclusionMode; | 
|  | 39 | using android::gui::WindowInfo; | 
|  | 40 | using android::gui::WindowInfoHandle; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 41 | using android::os::InputEventInjectionResult; | 
|  | 42 | using android::os::InputEventInjectionSync; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 43 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 44 | namespace android::inputdispatcher { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 45 |  | 
| Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 46 | using namespace ftl::flag_operators; | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 47 | using testing::AllOf; | 
| Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 48 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 49 | // An arbitrary time value. | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 50 | static constexpr nsecs_t ARBITRARY_TIME = 1234; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 51 |  | 
|  | 52 | // An arbitrary device id. | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 53 | static constexpr int32_t DEVICE_ID = 1; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 54 |  | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 55 | // An arbitrary display id. | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 56 | static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; | 
|  | 57 | static constexpr int32_t SECOND_DISPLAY_ID = 1; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 58 |  | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 59 | static constexpr int32_t POINTER_1_DOWN = | 
|  | 60 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 61 | static constexpr int32_t POINTER_2_DOWN = | 
|  | 62 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 63 | static constexpr int32_t POINTER_3_DOWN = | 
|  | 64 | AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 65 | static constexpr int32_t POINTER_1_UP = | 
|  | 66 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
|  | 67 |  | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 68 | // The default pid and uid for windows created on the primary display by the test. | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 69 | static constexpr int32_t WINDOW_PID = 999; | 
|  | 70 | static constexpr int32_t WINDOW_UID = 1001; | 
|  | 71 |  | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 72 | // The default pid and uid for the windows created on the secondary display by the test. | 
|  | 73 | static constexpr int32_t SECONDARY_WINDOW_PID = 1010; | 
|  | 74 | static constexpr int32_t SECONDARY_WINDOW_UID = 1012; | 
|  | 75 |  | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 76 | // The default policy flags to use for event injection by tests. | 
|  | 77 | static constexpr uint32_t DEFAULT_POLICY_FLAGS = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 78 |  | 
| Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 79 | // An arbitrary pid of the gesture monitor window | 
|  | 80 | static constexpr int32_t MONITOR_PID = 2001; | 
|  | 81 |  | 
| Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 82 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; | 
|  | 83 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 84 | struct PointF { | 
|  | 85 | float x; | 
|  | 86 | float y; | 
|  | 87 | }; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 88 |  | 
| Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 89 | /** | 
|  | 90 | * Return a DOWN key event with KEYCODE_A. | 
|  | 91 | */ | 
|  | 92 | static KeyEvent getTestKeyEvent() { | 
|  | 93 | KeyEvent event; | 
|  | 94 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 95 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 96 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, | 
|  | 97 | ARBITRARY_TIME, ARBITRARY_TIME); | 
| Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 98 | return event; | 
|  | 99 | } | 
|  | 100 |  | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 101 | static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) { | 
|  | 102 | ASSERT_EQ(expectedAction, receivedAction) | 
|  | 103 | << "expected " << MotionEvent::actionToString(expectedAction) << ", got " | 
|  | 104 | << MotionEvent::actionToString(receivedAction); | 
|  | 105 | } | 
|  | 106 |  | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 107 | MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") { | 
|  | 108 | bool matches = action == arg.getAction(); | 
|  | 109 | if (!matches) { | 
|  | 110 | *result_listener << "expected action " << MotionEvent::actionToString(action) | 
|  | 111 | << ", but got " << MotionEvent::actionToString(arg.getAction()); | 
|  | 112 | } | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 113 | if (action == AMOTION_EVENT_ACTION_DOWN) { | 
|  | 114 | if (!matches) { | 
|  | 115 | *result_listener << "; "; | 
|  | 116 | } | 
|  | 117 | *result_listener << "downTime should match eventTime for ACTION_DOWN events"; | 
|  | 118 | matches &= arg.getDownTime() == arg.getEventTime(); | 
|  | 119 | } | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 120 | if (action == AMOTION_EVENT_ACTION_CANCEL) { | 
|  | 121 | if (!matches) { | 
|  | 122 | *result_listener << "; "; | 
|  | 123 | } | 
|  | 124 | *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set"; | 
|  | 125 | matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0; | 
|  | 126 | } | 
|  | 127 | return matches; | 
|  | 128 | } | 
|  | 129 |  | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 130 | MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") { | 
|  | 131 | return arg.getDownTime() == downTime; | 
|  | 132 | } | 
|  | 133 |  | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 134 | MATCHER_P(WithSource, source, "InputEvent with specified source") { | 
|  | 135 | *result_listener << "expected source " << inputEventSourceToString(source) << ", but got " | 
|  | 136 | << inputEventSourceToString(arg.getSource()); | 
|  | 137 | return arg.getSource() == source; | 
|  | 138 | } | 
|  | 139 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 140 | // --- FakeInputDispatcherPolicy --- | 
|  | 141 |  | 
|  | 142 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { | 
|  | 143 | InputDispatcherConfiguration mConfig; | 
|  | 144 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 145 | using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>; | 
|  | 146 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 147 | protected: | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 148 | virtual ~FakeInputDispatcherPolicy() {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 149 |  | 
|  | 150 | public: | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 151 | FakeInputDispatcherPolicy() {} | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 152 |  | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 153 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 154 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { | 
|  | 155 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_KEY); | 
|  | 156 | EXPECT_EQ(event.getDisplayId(), args.displayId); | 
|  | 157 |  | 
|  | 158 | const auto& keyEvent = static_cast<const KeyEvent&>(event); | 
|  | 159 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); | 
|  | 160 | EXPECT_EQ(keyEvent.getAction(), args.action); | 
|  | 161 | }); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 162 | } | 
|  | 163 |  | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 164 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { | 
|  | 165 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { | 
|  | 166 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_MOTION); | 
|  | 167 | EXPECT_EQ(event.getDisplayId(), args.displayId); | 
|  | 168 |  | 
|  | 169 | const auto& motionEvent = static_cast<const MotionEvent&>(event); | 
|  | 170 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); | 
|  | 171 | EXPECT_EQ(motionEvent.getAction(), args.action); | 
|  | 172 | EXPECT_EQ(motionEvent.getX(0), point.x); | 
|  | 173 | EXPECT_EQ(motionEvent.getY(0), point.y); | 
|  | 174 | EXPECT_EQ(motionEvent.getRawX(0), point.x); | 
|  | 175 | EXPECT_EQ(motionEvent.getRawY(0), point.y); | 
|  | 176 | }); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 177 | } | 
|  | 178 |  | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 179 | void assertFilterInputEventWasNotCalled() { | 
|  | 180 | std::scoped_lock lock(mLock); | 
|  | 181 | ASSERT_EQ(nullptr, mFilteredEvent); | 
|  | 182 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 183 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 184 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 185 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 186 | ASSERT_TRUE(mConfigurationChangedTime) | 
|  | 187 | << "Timed out waiting for configuration changed call"; | 
|  | 188 | ASSERT_EQ(*mConfigurationChangedTime, when); | 
|  | 189 | mConfigurationChangedTime = std::nullopt; | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 193 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 194 | ASSERT_TRUE(mLastNotifySwitch); | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 195 | // 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] | 196 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); | 
|  | 197 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); | 
|  | 198 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); | 
|  | 199 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); | 
|  | 200 | mLastNotifySwitch = std::nullopt; | 
|  | 201 | } | 
|  | 202 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 203 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 204 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 205 | ASSERT_EQ(touchedToken, mOnPointerDownToken); | 
|  | 206 | mOnPointerDownToken.clear(); | 
|  | 207 | } | 
|  | 208 |  | 
|  | 209 | void assertOnPointerDownWasNotCalled() { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 210 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 211 | ASSERT_TRUE(mOnPointerDownToken == nullptr) | 
|  | 212 | << "Expected onPointerDownOutsideFocus to not have been called"; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 213 | } | 
|  | 214 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 215 | // This function must be called soon after the expected ANR timer starts, | 
|  | 216 | // because we are also checking how much time has passed. | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 217 | void assertNotifyNoFocusedWindowAnrWasCalled( | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 218 | std::chrono::nanoseconds timeout, | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 219 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 220 | std::unique_lock lock(mLock); | 
|  | 221 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 222 | std::shared_ptr<InputApplicationHandle> application; | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 223 | ASSERT_NO_FATAL_FAILURE( | 
|  | 224 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 225 | ASSERT_EQ(expectedApplication, application); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 226 | } | 
|  | 227 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 228 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 229 | const sp<WindowInfoHandle>& window) { | 
|  | 230 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); | 
|  | 231 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), | 
|  | 232 | window->getInfo()->ownerPid); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 233 | } | 
|  | 234 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 235 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, | 
|  | 236 | const sp<IBinder>& expectedToken, | 
|  | 237 | int32_t expectedPid) { | 
|  | 238 | std::unique_lock lock(mLock); | 
|  | 239 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 240 | AnrResult result; | 
|  | 241 | ASSERT_NO_FATAL_FAILURE(result = | 
|  | 242 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); | 
|  | 243 | const auto& [token, pid] = result; | 
|  | 244 | ASSERT_EQ(expectedToken, token); | 
|  | 245 | ASSERT_EQ(expectedPid, pid); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 246 | } | 
|  | 247 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 248 | /** 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] | 249 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 250 | std::unique_lock lock(mLock); | 
|  | 251 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 252 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); | 
|  | 253 | const auto& [token, _] = result; | 
|  | 254 | return token; | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 255 | } | 
|  | 256 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 257 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, | 
|  | 258 | int32_t expectedPid) { | 
|  | 259 | std::unique_lock lock(mLock); | 
|  | 260 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 261 | AnrResult result; | 
|  | 262 | ASSERT_NO_FATAL_FAILURE( | 
|  | 263 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); | 
|  | 264 | const auto& [token, pid] = result; | 
|  | 265 | ASSERT_EQ(expectedToken, token); | 
|  | 266 | ASSERT_EQ(expectedPid, pid); | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | /** 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] | 270 | sp<IBinder> getResponsiveWindowToken() { | 
|  | 271 | std::unique_lock lock(mLock); | 
|  | 272 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 273 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); | 
|  | 274 | const auto& [token, _] = result; | 
|  | 275 | return token; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 276 | } | 
|  | 277 |  | 
|  | 278 | void assertNotifyAnrWasNotCalled() { | 
|  | 279 | std::scoped_lock lock(mLock); | 
|  | 280 | ASSERT_TRUE(mAnrApplications.empty()); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 281 | ASSERT_TRUE(mAnrWindows.empty()); | 
|  | 282 | ASSERT_TRUE(mResponsiveWindows.empty()) | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 283 | << "ANR was not called, but please also consume the 'connection is responsive' " | 
|  | 284 | "signal"; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 285 | } | 
|  | 286 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 287 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { | 
|  | 288 | mConfig.keyRepeatTimeout = timeout; | 
|  | 289 | mConfig.keyRepeatDelay = delay; | 
|  | 290 | } | 
|  | 291 |  | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 292 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 293 | std::unique_lock lock(mLock); | 
|  | 294 | base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 295 |  | 
|  | 296 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, | 
|  | 297 | [this, enabled]() REQUIRES(mLock) { | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 298 | return mPointerCaptureRequest->enable == | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 299 | enabled; | 
|  | 300 | })) { | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 301 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled | 
|  | 302 | << ") to be called."; | 
|  | 303 | return {}; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 304 | } | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 305 | auto request = *mPointerCaptureRequest; | 
|  | 306 | mPointerCaptureRequest.reset(); | 
|  | 307 | return request; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 308 | } | 
|  | 309 |  | 
|  | 310 | void assertSetPointerCaptureNotCalled() { | 
|  | 311 | std::unique_lock lock(mLock); | 
|  | 312 | base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 313 |  | 
|  | 314 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 315 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 316 | "enabled = " | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 317 | << std::to_string(mPointerCaptureRequest->enable); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 318 | } | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 319 | mPointerCaptureRequest.reset(); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 320 | } | 
|  | 321 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 322 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { | 
|  | 323 | std::scoped_lock lock(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 324 | ASSERT_TRUE(mNotifyDropWindowWasCalled); | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 325 | ASSERT_EQ(targetToken, mDropTargetWindowToken); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 326 | mNotifyDropWindowWasCalled = false; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 327 | } | 
|  | 328 |  | 
| Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 329 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { | 
|  | 330 | std::unique_lock lock(mLock); | 
|  | 331 | base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 332 | std::optional<sp<IBinder>> receivedToken = | 
|  | 333 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, | 
|  | 334 | mNotifyInputChannelBroken); | 
|  | 335 | ASSERT_TRUE(receivedToken.has_value()); | 
|  | 336 | ASSERT_EQ(token, *receivedToken); | 
|  | 337 | } | 
|  | 338 |  | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 339 | /** | 
|  | 340 | * Set policy timeout. A value of zero means next key will not be intercepted. | 
|  | 341 | */ | 
|  | 342 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { | 
|  | 343 | mInterceptKeyTimeout = timeout; | 
|  | 344 | } | 
|  | 345 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 346 | private: | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 347 | std::mutex mLock; | 
|  | 348 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); | 
|  | 349 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); | 
|  | 350 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); | 
|  | 351 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 352 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 353 | std::condition_variable mPointerCaptureChangedCondition; | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 354 |  | 
|  | 355 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 356 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 357 | // ANR handling | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 358 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 359 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); | 
|  | 360 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 361 | std::condition_variable mNotifyAnr; | 
| Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 362 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); | 
|  | 363 | std::condition_variable mNotifyInputChannelBroken; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 364 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 365 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 366 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 367 |  | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 368 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; | 
|  | 369 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 370 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait | 
|  | 371 | // for a specific container to become non-empty. When the container is non-empty, return the | 
|  | 372 | // first entry from the container and erase it. | 
|  | 373 | template <class T> | 
|  | 374 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, | 
|  | 375 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { | 
|  | 376 | // If there is an ANR, Dispatcher won't be idle because there are still events | 
|  | 377 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle | 
|  | 378 | // before checking if ANR was called. | 
|  | 379 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need | 
|  | 380 | // to provide it some time to act. 100ms seems reasonable. | 
|  | 381 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack | 
|  | 382 | const std::chrono::time_point start = std::chrono::steady_clock::now(); | 
|  | 383 | std::optional<T> token = | 
|  | 384 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); | 
|  | 385 | if (!token.has_value()) { | 
|  | 386 | ADD_FAILURE() << "Did not receive the ANR callback"; | 
|  | 387 | return {}; | 
|  | 388 | } | 
|  | 389 |  | 
|  | 390 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; | 
|  | 391 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because | 
|  | 392 | // the dispatcher started counting before this function was called | 
|  | 393 | if (std::chrono::abs(timeout - waited) > 100ms) { | 
|  | 394 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " | 
|  | 395 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() | 
|  | 396 | << "ms, but waited " | 
|  | 397 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() | 
|  | 398 | << "ms instead"; | 
|  | 399 | } | 
|  | 400 | return *token; | 
|  | 401 | } | 
|  | 402 |  | 
|  | 403 | template <class T> | 
|  | 404 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, | 
|  | 405 | std::queue<T>& storage, | 
|  | 406 | std::unique_lock<std::mutex>& lock, | 
|  | 407 | std::condition_variable& condition) | 
|  | 408 | REQUIRES(mLock) { | 
|  | 409 | condition.wait_for(lock, timeout, | 
|  | 410 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); | 
|  | 411 | if (storage.empty()) { | 
|  | 412 | ADD_FAILURE() << "Did not receive the expected callback"; | 
|  | 413 | return std::nullopt; | 
|  | 414 | } | 
|  | 415 | T item = storage.front(); | 
|  | 416 | storage.pop(); | 
|  | 417 | return std::make_optional(item); | 
|  | 418 | } | 
|  | 419 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 420 | void notifyConfigurationChanged(nsecs_t when) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 421 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 422 | mConfigurationChangedTime = when; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 423 | } | 
|  | 424 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 425 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid, | 
|  | 426 | const std::string&) override { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 427 | std::scoped_lock lock(mLock); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 428 | ASSERT_TRUE(pid.has_value()); | 
|  | 429 | mAnrWindows.push({connectionToken, *pid}); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 430 | mNotifyAnr.notify_all(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 431 | } | 
|  | 432 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 433 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, | 
|  | 434 | std::optional<int32_t> pid) override { | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 435 | std::scoped_lock lock(mLock); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 436 | ASSERT_TRUE(pid.has_value()); | 
|  | 437 | mResponsiveWindows.push({connectionToken, *pid}); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 438 | mNotifyAnr.notify_all(); | 
|  | 439 | } | 
|  | 440 |  | 
|  | 441 | void notifyNoFocusedWindowAnr( | 
|  | 442 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { | 
|  | 443 | std::scoped_lock lock(mLock); | 
|  | 444 | mAnrApplications.push(applicationHandle); | 
|  | 445 | mNotifyAnr.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 446 | } | 
|  | 447 |  | 
| Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 448 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { | 
|  | 449 | std::scoped_lock lock(mLock); | 
|  | 450 | mBrokenInputChannels.push(connectionToken); | 
|  | 451 | mNotifyInputChannelBroken.notify_all(); | 
|  | 452 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 453 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 454 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} | 
| Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 455 |  | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 456 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, | 
|  | 457 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, | 
|  | 458 | const std::vector<float>& values) override {} | 
|  | 459 |  | 
|  | 460 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, | 
|  | 461 | InputDeviceSensorAccuracy accuracy) override {} | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 462 |  | 
| Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 463 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} | 
|  | 464 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 465 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 466 | *outConfig = mConfig; | 
|  | 467 | } | 
|  | 468 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 469 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 470 | std::scoped_lock lock(mLock); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 471 | switch (inputEvent->getType()) { | 
|  | 472 | case AINPUT_EVENT_TYPE_KEY: { | 
|  | 473 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 474 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 475 | break; | 
|  | 476 | } | 
|  | 477 |  | 
|  | 478 | case AINPUT_EVENT_TYPE_MOTION: { | 
|  | 479 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 480 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 481 | break; | 
|  | 482 | } | 
|  | 483 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 484 | return true; | 
|  | 485 | } | 
|  | 486 |  | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 487 | void interceptKeyBeforeQueueing(const KeyEvent* inputEvent, uint32_t&) override { | 
|  | 488 | if (inputEvent->getAction() == AKEY_EVENT_ACTION_UP) { | 
|  | 489 | // Clear intercept state when we handled the event. | 
|  | 490 | mInterceptKeyTimeout = 0ms; | 
|  | 491 | } | 
|  | 492 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 493 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 494 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 495 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 496 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 497 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); | 
|  | 498 | // Clear intercept state so we could dispatch the event in next wake. | 
|  | 499 | mInterceptKeyTimeout = 0ms; | 
|  | 500 | return delay; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 501 | } | 
|  | 502 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 503 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 504 | return false; | 
|  | 505 | } | 
|  | 506 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 507 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, | 
|  | 508 | uint32_t policyFlags) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 509 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 510 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is | 
|  | 511 | * essentially a passthrough for notifySwitch. | 
|  | 512 | */ | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 513 | mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 514 | } | 
|  | 515 |  | 
| Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 516 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 517 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 518 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 519 | std::scoped_lock lock(mLock); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 520 | mOnPointerDownToken = newToken; | 
|  | 521 | } | 
|  | 522 |  | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 523 | void setPointerCapture(const PointerCaptureRequest& request) override { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 524 | std::scoped_lock lock(mLock); | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 525 | mPointerCaptureRequest = {request}; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 526 | mPointerCaptureChangedCondition.notify_all(); | 
|  | 527 | } | 
|  | 528 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 529 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { | 
|  | 530 | std::scoped_lock lock(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 531 | mNotifyDropWindowWasCalled = true; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 532 | mDropTargetWindowToken = token; | 
|  | 533 | } | 
|  | 534 |  | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 535 | void assertFilterInputEventWasCalledInternal( | 
|  | 536 | const std::function<void(const InputEvent&)>& verify) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 537 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 538 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 539 | verify(*mFilteredEvent); | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 540 | mFilteredEvent = nullptr; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 541 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 542 | }; | 
|  | 543 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 544 | // --- InputDispatcherTest --- | 
|  | 545 |  | 
|  | 546 | class InputDispatcherTest : public testing::Test { | 
|  | 547 | protected: | 
|  | 548 | sp<FakeInputDispatcherPolicy> mFakePolicy; | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 549 | std::unique_ptr<InputDispatcher> mDispatcher; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 550 |  | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 551 | void SetUp() override { | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 552 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); | 
| Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 553 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy, STALE_EVENT_TIMEOUT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 554 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 555 | // Start InputDispatcher thread | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 556 | ASSERT_EQ(OK, mDispatcher->start()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 557 | } | 
|  | 558 |  | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 559 | void TearDown() override { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 560 | ASSERT_EQ(OK, mDispatcher->stop()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 561 | mFakePolicy.clear(); | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 562 | mDispatcher.reset(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 563 | } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 564 |  | 
|  | 565 | /** | 
|  | 566 | * Used for debugging when writing the test | 
|  | 567 | */ | 
|  | 568 | void dumpDispatcherState() { | 
|  | 569 | std::string dump; | 
|  | 570 | mDispatcher->dump(dump); | 
|  | 571 | std::stringstream ss(dump); | 
|  | 572 | std::string to; | 
|  | 573 |  | 
|  | 574 | while (std::getline(ss, to, '\n')) { | 
|  | 575 | ALOGE("%s", to.c_str()); | 
|  | 576 | } | 
|  | 577 | } | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 578 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 579 | void setFocusedWindow(const sp<WindowInfoHandle>& window, | 
|  | 580 | const sp<WindowInfoHandle>& focusedWindow = nullptr) { | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 581 | FocusRequest request; | 
|  | 582 | request.token = window->getToken(); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 583 | request.windowName = window->getName(); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 584 | if (focusedWindow) { | 
|  | 585 | request.focusedToken = focusedWindow->getToken(); | 
|  | 586 | } | 
|  | 587 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 588 | request.displayId = window->getInfo()->displayId; | 
|  | 589 | mDispatcher->setFocusedWindow(request); | 
|  | 590 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 591 | }; | 
|  | 592 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 593 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { | 
|  | 594 | KeyEvent event; | 
|  | 595 |  | 
|  | 596 | // Rejects undefined key actions. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 597 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 598 | INVALID_HMAC, | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 599 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, | 
|  | 600 | ARBITRARY_TIME); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 601 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 602 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, | 
|  | 603 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 604 | << "Should reject key events with undefined action."; | 
|  | 605 |  | 
|  | 606 | // 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] | 607 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 608 | 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] | 609 | ARBITRARY_TIME, ARBITRARY_TIME); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 610 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 611 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, | 
|  | 612 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 613 | << "Should reject key events with ACTION_MULTIPLE."; | 
|  | 614 | } | 
|  | 615 |  | 
|  | 616 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { | 
|  | 617 | MotionEvent event; | 
|  | 618 | PointerProperties pointerProperties[MAX_POINTERS + 1]; | 
|  | 619 | PointerCoords pointerCoords[MAX_POINTERS + 1]; | 
| Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 620 | for (size_t i = 0; i <= MAX_POINTERS; i++) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 621 | pointerProperties[i].clear(); | 
|  | 622 | pointerProperties[i].id = i; | 
|  | 623 | pointerCoords[i].clear(); | 
|  | 624 | } | 
|  | 625 |  | 
| Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 626 | // Some constants commonly used below | 
|  | 627 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; | 
|  | 628 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; | 
|  | 629 | constexpr int32_t metaState = AMETA_NONE; | 
|  | 630 | constexpr MotionClassification classification = MotionClassification::NONE; | 
|  | 631 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 632 | ui::Transform identityTransform; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 633 | // Rejects undefined motion actions. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 634 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 635 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, | 
|  | 636 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 637 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, | 
|  | 638 | ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 639 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 640 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 641 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, | 
|  | 642 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 643 | << "Should reject motion events with undefined action."; | 
|  | 644 |  | 
|  | 645 | // Rejects pointer down with invalid index. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 646 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 647 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
|  | 648 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 649 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, | 
|  | 650 | ARBITRARY_TIME, | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 651 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 652 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 653 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, | 
|  | 654 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 655 | << "Should reject motion events with pointer down index too large."; | 
|  | 656 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 657 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 658 | AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 659 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 660 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 661 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 662 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 663 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 664 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 665 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, | 
|  | 666 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 667 | << "Should reject motion events with pointer down index too small."; | 
|  | 668 |  | 
|  | 669 | // Rejects pointer up with invalid index. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 670 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 671 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, | 
|  | 672 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 673 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, | 
|  | 674 | ARBITRARY_TIME, | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 675 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 676 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 677 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, | 
|  | 678 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 679 | << "Should reject motion events with pointer up index too large."; | 
|  | 680 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 681 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 682 | AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 683 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 684 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 685 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 686 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 687 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 688 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 689 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, | 
|  | 690 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 691 | << "Should reject motion events with pointer up index too small."; | 
|  | 692 |  | 
|  | 693 | // Rejects motion events with invalid number of pointers. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 694 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 695 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 696 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 697 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, | 
|  | 698 | ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 699 | /*pointerCount*/ 0, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 700 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 701 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, | 
|  | 702 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 703 | << "Should reject motion events with 0 pointers."; | 
|  | 704 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 705 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 706 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 707 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 708 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, | 
|  | 709 | ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 710 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 711 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 712 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, | 
|  | 713 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 714 | << "Should reject motion events with more than MAX_POINTERS pointers."; | 
|  | 715 |  | 
|  | 716 | // Rejects motion events with invalid pointer ids. | 
|  | 717 | pointerProperties[0].id = -1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 718 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 719 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 720 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 721 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, | 
|  | 722 | ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 723 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 724 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 725 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, | 
|  | 726 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 727 | << "Should reject motion events with pointer ids less than 0."; | 
|  | 728 |  | 
|  | 729 | pointerProperties[0].id = MAX_POINTER_ID + 1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 730 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 731 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 732 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 733 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, | 
|  | 734 | ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 735 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 736 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 737 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, | 
|  | 738 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 739 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; | 
|  | 740 |  | 
|  | 741 | // Rejects motion events with duplicate pointer ids. | 
|  | 742 | pointerProperties[0].id = 1; | 
|  | 743 | pointerProperties[1].id = 1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 744 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 745 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 746 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 747 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, | 
|  | 748 | ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 749 | /*pointerCount*/ 2, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 750 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 751 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, | 
|  | 752 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 753 | << "Should reject motion events with duplicate pointer ids."; | 
|  | 754 | } | 
|  | 755 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 756 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ | 
|  | 757 |  | 
|  | 758 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { | 
|  | 759 | constexpr nsecs_t eventTime = 20; | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 760 | NotifyConfigurationChangedArgs args(10 /*id*/, eventTime); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 761 | mDispatcher->notifyConfigurationChanged(&args); | 
|  | 762 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 763 |  | 
|  | 764 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); | 
|  | 765 | } | 
|  | 766 |  | 
|  | 767 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 768 | NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/, | 
|  | 769 | 2 /*switchMask*/); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 770 | mDispatcher->notifySwitch(&args); | 
|  | 771 |  | 
|  | 772 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener | 
|  | 773 | args.policyFlags |= POLICY_FLAG_TRUSTED; | 
|  | 774 | mFakePolicy->assertNotifySwitchWasCalled(args); | 
|  | 775 | } | 
|  | 776 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 777 | // --- InputDispatcherTest SetInputWindowTest --- | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 778 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; | 
| Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 779 | // Default input dispatching timeout if there is no focused application or paused window | 
|  | 780 | // from which to determine an appropriate dispatching timeout. | 
|  | 781 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( | 
|  | 782 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * | 
|  | 783 | android::base::HwTimeoutMultiplier()); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 784 |  | 
|  | 785 | class FakeApplicationHandle : public InputApplicationHandle { | 
|  | 786 | public: | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 787 | FakeApplicationHandle() { | 
|  | 788 | mInfo.name = "Fake Application"; | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 789 | mInfo.token = sp<BBinder>::make(); | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 790 | mInfo.dispatchingTimeoutMillis = | 
|  | 791 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 792 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 793 | virtual ~FakeApplicationHandle() {} | 
|  | 794 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 795 | virtual bool updateInfo() override { return true; } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 796 |  | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 797 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { | 
|  | 798 | mInfo.dispatchingTimeoutMillis = timeout.count(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 799 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 800 | }; | 
|  | 801 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 802 | class FakeInputReceiver { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 803 | public: | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 804 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 805 | : mName(name) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 806 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 807 | } | 
|  | 808 |  | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 809 | InputEvent* consume() { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 810 | InputEvent* event; | 
|  | 811 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); | 
|  | 812 | if (!consumeSeq) { | 
|  | 813 | return nullptr; | 
|  | 814 | } | 
|  | 815 | finishEvent(*consumeSeq); | 
|  | 816 | return event; | 
|  | 817 | } | 
|  | 818 |  | 
|  | 819 | /** | 
|  | 820 | * Receive an event without acknowledging it. | 
|  | 821 | * Return the sequence number that could later be used to send finished signal. | 
|  | 822 | */ | 
|  | 823 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 824 | uint32_t consumeSeq; | 
|  | 825 | InputEvent* event; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 826 |  | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 827 | std::chrono::time_point start = std::chrono::steady_clock::now(); | 
|  | 828 | status_t status = WOULD_BLOCK; | 
|  | 829 | while (status == WOULD_BLOCK) { | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 830 | status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 831 | &event); | 
|  | 832 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; | 
|  | 833 | if (elapsed > 100ms) { | 
|  | 834 | break; | 
|  | 835 | } | 
|  | 836 | } | 
|  | 837 |  | 
|  | 838 | if (status == WOULD_BLOCK) { | 
|  | 839 | // Just means there's no event available. | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 840 | return std::nullopt; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 841 | } | 
|  | 842 |  | 
|  | 843 | if (status != OK) { | 
|  | 844 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; | 
| 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 | if (event == nullptr) { | 
|  | 848 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 849 | return std::nullopt; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 850 | } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 851 | if (outEvent != nullptr) { | 
|  | 852 | *outEvent = event; | 
|  | 853 | } | 
|  | 854 | return consumeSeq; | 
|  | 855 | } | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 856 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 857 | /** | 
|  | 858 | * To be used together with "receiveEvent" to complete the consumption of an event. | 
|  | 859 | */ | 
|  | 860 | void finishEvent(uint32_t consumeSeq) { | 
|  | 861 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); | 
|  | 862 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 863 | } | 
|  | 864 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 865 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { | 
|  | 866 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); | 
|  | 867 | ASSERT_EQ(OK, status); | 
|  | 868 | } | 
|  | 869 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 870 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, | 
|  | 871 | std::optional<int32_t> expectedDisplayId, | 
|  | 872 | std::optional<int32_t> expectedFlags) { | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 873 | InputEvent* event = consume(); | 
|  | 874 |  | 
|  | 875 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 876 | << ": consumer should have returned non-NULL event."; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 877 | ASSERT_EQ(expectedEventType, event->getType()) | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 878 | << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType) | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 879 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 880 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 881 | if (expectedDisplayId.has_value()) { | 
|  | 882 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); | 
|  | 883 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 884 |  | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 885 | switch (expectedEventType) { | 
|  | 886 | case AINPUT_EVENT_TYPE_KEY: { | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 887 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); | 
|  | 888 | EXPECT_EQ(expectedAction, keyEvent.getAction()); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 889 | if (expectedFlags.has_value()) { | 
|  | 890 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); | 
|  | 891 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 892 | break; | 
|  | 893 | } | 
|  | 894 | case AINPUT_EVENT_TYPE_MOTION: { | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 895 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 896 | assertMotionAction(expectedAction, motionEvent.getAction()); | 
|  | 897 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 898 | if (expectedFlags.has_value()) { | 
|  | 899 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); | 
|  | 900 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 901 | break; | 
|  | 902 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 903 | case AINPUT_EVENT_TYPE_FOCUS: { | 
|  | 904 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; | 
|  | 905 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 906 | case AINPUT_EVENT_TYPE_CAPTURE: { | 
|  | 907 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; | 
|  | 908 | } | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 909 | case AINPUT_EVENT_TYPE_TOUCH_MODE: { | 
|  | 910 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; | 
|  | 911 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 912 | case AINPUT_EVENT_TYPE_DRAG: { | 
|  | 913 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; | 
|  | 914 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 915 | default: { | 
|  | 916 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; | 
|  | 917 | } | 
|  | 918 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 919 | } | 
|  | 920 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 921 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { | 
|  | 922 | InputEvent* event = consume(); | 
|  | 923 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 924 | << ": consumer should have returned non-NULL event."; | 
|  | 925 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType()) | 
|  | 926 | << "Got " << inputEventTypeToString(event->getType()) | 
|  | 927 | << " event instead of FOCUS event"; | 
|  | 928 |  | 
|  | 929 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 930 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 931 |  | 
|  | 932 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); | 
|  | 933 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 934 | } | 
|  | 935 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 936 | void consumeCaptureEvent(bool hasCapture) { | 
|  | 937 | const InputEvent* event = consume(); | 
|  | 938 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 939 | << ": consumer should have returned non-NULL event."; | 
|  | 940 | ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType()) | 
|  | 941 | << "Got " << inputEventTypeToString(event->getType()) | 
|  | 942 | << " event instead of CAPTURE event"; | 
|  | 943 |  | 
|  | 944 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 945 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 946 |  | 
|  | 947 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); | 
|  | 948 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); | 
|  | 949 | } | 
|  | 950 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 951 | void consumeDragEvent(bool isExiting, float x, float y) { | 
|  | 952 | const InputEvent* event = consume(); | 
|  | 953 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 954 | << ": consumer should have returned non-NULL event."; | 
|  | 955 | ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType()) | 
|  | 956 | << "Got " << inputEventTypeToString(event->getType()) | 
|  | 957 | << " event instead of DRAG event"; | 
|  | 958 |  | 
|  | 959 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 960 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 961 |  | 
|  | 962 | const auto& dragEvent = static_cast<const DragEvent&>(*event); | 
|  | 963 | EXPECT_EQ(isExiting, dragEvent.isExiting()); | 
|  | 964 | EXPECT_EQ(x, dragEvent.getX()); | 
|  | 965 | EXPECT_EQ(y, dragEvent.getY()); | 
|  | 966 | } | 
|  | 967 |  | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 968 | void consumeTouchModeEvent(bool inTouchMode) { | 
|  | 969 | const InputEvent* event = consume(); | 
|  | 970 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 971 | << ": consumer should have returned non-NULL event."; | 
|  | 972 | ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType()) | 
|  | 973 | << "Got " << inputEventTypeToString(event->getType()) | 
|  | 974 | << " event instead of TOUCH_MODE event"; | 
|  | 975 |  | 
|  | 976 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 977 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 978 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); | 
|  | 979 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); | 
|  | 980 | } | 
|  | 981 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 982 | void assertNoEvents() { | 
|  | 983 | InputEvent* event = consume(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 984 | if (event == nullptr) { | 
|  | 985 | return; | 
|  | 986 | } | 
|  | 987 | if (event->getType() == AINPUT_EVENT_TYPE_KEY) { | 
|  | 988 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); | 
|  | 989 | ADD_FAILURE() << "Received key event " | 
|  | 990 | << KeyEvent::actionToString(keyEvent.getAction()); | 
|  | 991 | } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) { | 
|  | 992 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 993 | ADD_FAILURE() << "Received motion event " | 
|  | 994 | << MotionEvent::actionToString(motionEvent.getAction()); | 
|  | 995 | } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) { | 
|  | 996 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); | 
|  | 997 | ADD_FAILURE() << "Received focus event, hasFocus = " | 
|  | 998 | << (focusEvent.getHasFocus() ? "true" : "false"); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 999 | } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) { | 
|  | 1000 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); | 
|  | 1001 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " | 
|  | 1002 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1003 | } else if (event->getType() == AINPUT_EVENT_TYPE_TOUCH_MODE) { | 
|  | 1004 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); | 
|  | 1005 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " | 
|  | 1006 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1007 | } | 
|  | 1008 | FAIL() << mName.c_str() | 
|  | 1009 | << ": should not have received any events, so consume() should return NULL"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1010 | } | 
|  | 1011 |  | 
|  | 1012 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } | 
|  | 1013 |  | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1014 | int getChannelFd() { return mConsumer->getChannel()->getFd().get(); } | 
|  | 1015 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1016 | protected: | 
|  | 1017 | std::unique_ptr<InputConsumer> mConsumer; | 
|  | 1018 | PreallocatedInputEventFactory mEventFactory; | 
|  | 1019 |  | 
|  | 1020 | std::string mName; | 
|  | 1021 | }; | 
|  | 1022 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1023 | class FakeWindowHandle : public WindowInfoHandle { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1024 | public: | 
|  | 1025 | static const int32_t WIDTH = 600; | 
|  | 1026 | static const int32_t HEIGHT = 800; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1027 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1028 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1029 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1030 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1031 | : mName(name) { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1032 | if (token == std::nullopt) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1033 | base::Result<std::unique_ptr<InputChannel>> channel = | 
|  | 1034 | dispatcher->createInputChannel(name); | 
|  | 1035 | token = (*channel)->getConnectionToken(); | 
|  | 1036 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1037 | } | 
|  | 1038 |  | 
|  | 1039 | inputApplicationHandle->updateInfo(); | 
|  | 1040 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); | 
|  | 1041 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1042 | mInfo.token = *token; | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1043 | mInfo.id = sId++; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1044 | mInfo.name = name; | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1045 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1046 | mInfo.alpha = 1.0; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1047 | mInfo.frameLeft = 0; | 
|  | 1048 | mInfo.frameTop = 0; | 
|  | 1049 | mInfo.frameRight = WIDTH; | 
|  | 1050 | mInfo.frameBottom = HEIGHT; | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1051 | mInfo.transform.set(0, 0); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1052 | mInfo.globalScaleFactor = 1.0; | 
|  | 1053 | mInfo.touchableRegion.clear(); | 
|  | 1054 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1055 | mInfo.ownerPid = WINDOW_PID; | 
|  | 1056 | mInfo.ownerUid = WINDOW_UID; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1057 | mInfo.displayId = displayId; | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1058 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1059 | } | 
|  | 1060 |  | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1061 | sp<FakeWindowHandle> clone( | 
|  | 1062 | const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1063 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) { | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1064 | sp<FakeWindowHandle> handle = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1065 | sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher, | 
|  | 1066 | mInfo.name + "(Mirror)", displayId, mInfo.token); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1067 | return handle; | 
|  | 1068 | } | 
|  | 1069 |  | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1070 | void setTouchable(bool touchable) { | 
|  | 1071 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); | 
|  | 1072 | } | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1073 |  | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1074 | void setFocusable(bool focusable) { | 
|  | 1075 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); | 
|  | 1076 | } | 
|  | 1077 |  | 
|  | 1078 | void setVisible(bool visible) { | 
|  | 1079 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); | 
|  | 1080 | } | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1081 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1082 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1083 | mInfo.dispatchingTimeout = timeout; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1084 | } | 
|  | 1085 |  | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1086 | void setPaused(bool paused) { | 
|  | 1087 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); | 
|  | 1088 | } | 
|  | 1089 |  | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1090 | void setPreventSplitting(bool preventSplitting) { | 
|  | 1091 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1092 | } | 
|  | 1093 |  | 
|  | 1094 | void setSlippery(bool slippery) { | 
|  | 1095 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); | 
|  | 1096 | } | 
|  | 1097 |  | 
|  | 1098 | void setWatchOutsideTouch(bool watchOutside) { | 
|  | 1099 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); | 
|  | 1100 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1101 |  | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1102 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } | 
|  | 1103 |  | 
|  | 1104 | void setInterceptsStylus(bool interceptsStylus) { | 
|  | 1105 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); | 
|  | 1106 | } | 
|  | 1107 |  | 
|  | 1108 | void setDropInput(bool dropInput) { | 
|  | 1109 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); | 
|  | 1110 | } | 
|  | 1111 |  | 
|  | 1112 | void setDropInputIfObscured(bool dropInputIfObscured) { | 
|  | 1113 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); | 
|  | 1114 | } | 
|  | 1115 |  | 
|  | 1116 | void setNoInputChannel(bool noInputChannel) { | 
|  | 1117 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); | 
|  | 1118 | } | 
|  | 1119 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1120 | void setAlpha(float alpha) { mInfo.alpha = alpha; } | 
|  | 1121 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1122 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1123 |  | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1124 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } | 
|  | 1125 |  | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1126 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1127 | mInfo.frameLeft = frame.left; | 
|  | 1128 | mInfo.frameTop = frame.top; | 
|  | 1129 | mInfo.frameRight = frame.right; | 
|  | 1130 | mInfo.frameBottom = frame.bottom; | 
|  | 1131 | mInfo.touchableRegion.clear(); | 
|  | 1132 | mInfo.addTouchableRegion(frame); | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1133 |  | 
|  | 1134 | const Rect logicalDisplayFrame = displayTransform.transform(frame); | 
|  | 1135 | ui::Transform translate; | 
|  | 1136 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); | 
|  | 1137 | mInfo.transform = translate * displayTransform; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1138 | } | 
|  | 1139 |  | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1140 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } | 
|  | 1141 |  | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1142 | void setIsWallpaper(bool isWallpaper) { | 
|  | 1143 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); | 
|  | 1144 | } | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1145 |  | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1146 | void setDupTouchToWallpaper(bool hasWallpaper) { | 
|  | 1147 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); | 
|  | 1148 | } | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1149 |  | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1150 | void setTrustedOverlay(bool trustedOverlay) { | 
|  | 1151 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); | 
|  | 1152 | } | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1153 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1154 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { | 
|  | 1155 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); | 
|  | 1156 | } | 
|  | 1157 |  | 
|  | 1158 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1159 |  | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1160 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } | 
|  | 1161 |  | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1162 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 1163 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, | 
|  | 1164 | expectedFlags); | 
|  | 1165 | } | 
|  | 1166 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1167 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 1168 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); | 
|  | 1169 | } | 
|  | 1170 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1171 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1172 | int32_t expectedFlags = 0) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1173 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId, | 
|  | 1174 | expectedFlags); | 
|  | 1175 | } | 
|  | 1176 |  | 
|  | 1177 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1178 | int32_t expectedFlags = 0) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1179 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId, | 
|  | 1180 | expectedFlags); | 
|  | 1181 | } | 
|  | 1182 |  | 
|  | 1183 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1184 | int32_t expectedFlags = 0) { | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1185 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); | 
|  | 1186 | } | 
|  | 1187 |  | 
|  | 1188 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, | 
|  | 1189 | std::optional<int32_t> expectedFlags = std::nullopt) { | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1190 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, | 
|  | 1191 | expectedFlags); | 
|  | 1192 | } | 
|  | 1193 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1194 | void consumeMotionPointerDown(int32_t pointerIdx, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1195 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
|  | 1196 | int32_t expectedFlags = 0) { | 
|  | 1197 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1198 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1199 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); | 
|  | 1200 | } | 
|  | 1201 |  | 
|  | 1202 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1203 | int32_t expectedFlags = 0) { | 
|  | 1204 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1205 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1206 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); | 
|  | 1207 | } | 
|  | 1208 |  | 
|  | 1209 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1210 | int32_t expectedFlags = 0) { | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1211 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, | 
|  | 1212 | expectedFlags); | 
|  | 1213 | } | 
|  | 1214 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1215 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
|  | 1216 | int32_t expectedFlags = 0) { | 
|  | 1217 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, | 
|  | 1218 | expectedFlags); | 
|  | 1219 | } | 
|  | 1220 |  | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1221 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
|  | 1222 | int32_t expectedFlags = 0) { | 
|  | 1223 | InputEvent* event = consume(); | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 1224 | ASSERT_NE(nullptr, event); | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1225 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()); | 
|  | 1226 | const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 1227 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked()); | 
|  | 1228 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX()); | 
|  | 1229 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY()); | 
|  | 1230 | } | 
|  | 1231 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1232 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { | 
|  | 1233 | ASSERT_NE(mInputReceiver, nullptr) | 
|  | 1234 | << "Cannot consume events from a window with no receiver"; | 
|  | 1235 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); | 
|  | 1236 | } | 
|  | 1237 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1238 | void consumeCaptureEvent(bool hasCapture) { | 
|  | 1239 | ASSERT_NE(mInputReceiver, nullptr) | 
|  | 1240 | << "Cannot consume events from a window with no receiver"; | 
|  | 1241 | mInputReceiver->consumeCaptureEvent(hasCapture); | 
|  | 1242 | } | 
|  | 1243 |  | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1244 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { | 
|  | 1245 | MotionEvent* motionEvent = consumeMotion(); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 1246 | 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] | 1247 | ASSERT_THAT(*motionEvent, matcher); | 
|  | 1248 | } | 
|  | 1249 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1250 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, | 
|  | 1251 | std::optional<int32_t> expectedDisplayId, | 
|  | 1252 | std::optional<int32_t> expectedFlags) { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1253 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; | 
|  | 1254 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, | 
|  | 1255 | expectedFlags); | 
|  | 1256 | } | 
|  | 1257 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1258 | void consumeDragEvent(bool isExiting, float x, float y) { | 
|  | 1259 | mInputReceiver->consumeDragEvent(isExiting, x, y); | 
|  | 1260 | } | 
|  | 1261 |  | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1262 | void consumeTouchModeEvent(bool inTouchMode) { | 
|  | 1263 | ASSERT_NE(mInputReceiver, nullptr) | 
|  | 1264 | << "Cannot consume events from a window with no receiver"; | 
|  | 1265 | mInputReceiver->consumeTouchModeEvent(inTouchMode); | 
|  | 1266 | } | 
|  | 1267 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1268 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1269 | if (mInputReceiver == nullptr) { | 
|  | 1270 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; | 
|  | 1271 | return std::nullopt; | 
|  | 1272 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1273 | return mInputReceiver->receiveEvent(outEvent); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1274 | } | 
|  | 1275 |  | 
|  | 1276 | void finishEvent(uint32_t sequenceNum) { | 
|  | 1277 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; | 
|  | 1278 | mInputReceiver->finishEvent(sequenceNum); | 
|  | 1279 | } | 
|  | 1280 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1281 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { | 
|  | 1282 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; | 
|  | 1283 | mInputReceiver->sendTimeline(inputEventId, timeline); | 
|  | 1284 | } | 
|  | 1285 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1286 | InputEvent* consume() { | 
|  | 1287 | if (mInputReceiver == nullptr) { | 
|  | 1288 | return nullptr; | 
|  | 1289 | } | 
|  | 1290 | return mInputReceiver->consume(); | 
|  | 1291 | } | 
|  | 1292 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1293 | MotionEvent* consumeMotion() { | 
|  | 1294 | InputEvent* event = consume(); | 
|  | 1295 | if (event == nullptr) { | 
|  | 1296 | ADD_FAILURE() << "Consume failed : no event"; | 
|  | 1297 | return nullptr; | 
|  | 1298 | } | 
|  | 1299 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { | 
|  | 1300 | ADD_FAILURE() << "Instead of motion event, got " | 
|  | 1301 | << inputEventTypeToString(event->getType()); | 
|  | 1302 | return nullptr; | 
|  | 1303 | } | 
|  | 1304 | return static_cast<MotionEvent*>(event); | 
|  | 1305 | } | 
|  | 1306 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1307 | void assertNoEvents() { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1308 | if (mInputReceiver == nullptr && | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1309 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1310 | return; // Can't receive events if the window does not have input channel | 
|  | 1311 | } | 
|  | 1312 | ASSERT_NE(nullptr, mInputReceiver) | 
|  | 1313 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1314 | mInputReceiver->assertNoEvents(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1315 | } | 
|  | 1316 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1317 | sp<IBinder> getToken() { return mInfo.token; } | 
|  | 1318 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1319 | const std::string& getName() { return mName; } | 
|  | 1320 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1321 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { | 
|  | 1322 | mInfo.ownerPid = ownerPid; | 
|  | 1323 | mInfo.ownerUid = ownerUid; | 
|  | 1324 | } | 
|  | 1325 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1326 | int32_t getPid() const { return mInfo.ownerPid; } | 
|  | 1327 |  | 
| Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1328 | void destroyReceiver() { mInputReceiver = nullptr; } | 
|  | 1329 |  | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1330 | int getChannelFd() { return mInputReceiver->getChannelFd(); } | 
|  | 1331 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1332 | private: | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1333 | const std::string mName; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1334 | std::unique_ptr<FakeInputReceiver> mInputReceiver; | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1335 | 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] | 1336 | }; | 
|  | 1337 |  | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1338 | std::atomic<int32_t> FakeWindowHandle::sId{1}; | 
|  | 1339 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1340 | static InputEventInjectionResult injectKey( | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1341 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1342 | int32_t displayId = ADISPLAY_ID_NONE, | 
|  | 1343 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1344 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1345 | bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {}, | 
|  | 1346 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1347 | KeyEvent event; | 
|  | 1348 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1349 |  | 
|  | 1350 | // Define a valid key down event. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1351 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1352 | INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE, | 
|  | 1353 | repeatCount, currentTime, currentTime); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1354 |  | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1355 | if (!allowKeyRepeat) { | 
|  | 1356 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; | 
|  | 1357 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1358 | // Inject event until dispatch out. | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1359 | return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1360 | } | 
|  | 1361 |  | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1362 | static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1363 | int32_t displayId = ADISPLAY_ID_NONE) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1364 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId); | 
|  | 1365 | } | 
|  | 1366 |  | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1367 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without | 
|  | 1368 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it | 
|  | 1369 | // has to be woken up to process the repeating key. | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1370 | static InputEventInjectionResult injectKeyDownNoRepeat( | 
|  | 1371 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) { | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1372 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId, | 
|  | 1373 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, | 
|  | 1374 | /* allowKeyRepeat */ false); | 
|  | 1375 | } | 
|  | 1376 |  | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1377 | static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1378 | int32_t displayId = ADISPLAY_ID_NONE) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1379 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId); | 
|  | 1380 | } | 
|  | 1381 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1382 | class PointerBuilder { | 
|  | 1383 | public: | 
|  | 1384 | PointerBuilder(int32_t id, int32_t toolType) { | 
|  | 1385 | mProperties.clear(); | 
|  | 1386 | mProperties.id = id; | 
|  | 1387 | mProperties.toolType = toolType; | 
|  | 1388 | mCoords.clear(); | 
|  | 1389 | } | 
|  | 1390 |  | 
|  | 1391 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } | 
|  | 1392 |  | 
|  | 1393 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } | 
|  | 1394 |  | 
|  | 1395 | PointerBuilder& axis(int32_t axis, float value) { | 
|  | 1396 | mCoords.setAxisValue(axis, value); | 
|  | 1397 | return *this; | 
|  | 1398 | } | 
|  | 1399 |  | 
|  | 1400 | PointerProperties buildProperties() const { return mProperties; } | 
|  | 1401 |  | 
|  | 1402 | PointerCoords buildCoords() const { return mCoords; } | 
|  | 1403 |  | 
|  | 1404 | private: | 
|  | 1405 | PointerProperties mProperties; | 
|  | 1406 | PointerCoords mCoords; | 
|  | 1407 | }; | 
|  | 1408 |  | 
|  | 1409 | class MotionEventBuilder { | 
|  | 1410 | public: | 
|  | 1411 | MotionEventBuilder(int32_t action, int32_t source) { | 
|  | 1412 | mAction = action; | 
|  | 1413 | mSource = source; | 
|  | 1414 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1415 | } | 
|  | 1416 |  | 
|  | 1417 | MotionEventBuilder& eventTime(nsecs_t eventTime) { | 
|  | 1418 | mEventTime = eventTime; | 
|  | 1419 | return *this; | 
|  | 1420 | } | 
|  | 1421 |  | 
|  | 1422 | MotionEventBuilder& displayId(int32_t displayId) { | 
|  | 1423 | mDisplayId = displayId; | 
|  | 1424 | return *this; | 
|  | 1425 | } | 
|  | 1426 |  | 
|  | 1427 | MotionEventBuilder& actionButton(int32_t actionButton) { | 
|  | 1428 | mActionButton = actionButton; | 
|  | 1429 | return *this; | 
|  | 1430 | } | 
|  | 1431 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1432 | MotionEventBuilder& buttonState(int32_t buttonState) { | 
|  | 1433 | mButtonState = buttonState; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1434 | return *this; | 
|  | 1435 | } | 
|  | 1436 |  | 
|  | 1437 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { | 
|  | 1438 | mRawXCursorPosition = rawXCursorPosition; | 
|  | 1439 | return *this; | 
|  | 1440 | } | 
|  | 1441 |  | 
|  | 1442 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { | 
|  | 1443 | mRawYCursorPosition = rawYCursorPosition; | 
|  | 1444 | return *this; | 
|  | 1445 | } | 
|  | 1446 |  | 
|  | 1447 | MotionEventBuilder& pointer(PointerBuilder pointer) { | 
|  | 1448 | mPointers.push_back(pointer); | 
|  | 1449 | return *this; | 
|  | 1450 | } | 
|  | 1451 |  | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1452 | MotionEventBuilder& addFlag(uint32_t flags) { | 
|  | 1453 | mFlags |= flags; | 
|  | 1454 | return *this; | 
|  | 1455 | } | 
|  | 1456 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1457 | MotionEvent build() { | 
|  | 1458 | std::vector<PointerProperties> pointerProperties; | 
|  | 1459 | std::vector<PointerCoords> pointerCoords; | 
|  | 1460 | for (const PointerBuilder& pointer : mPointers) { | 
|  | 1461 | pointerProperties.push_back(pointer.buildProperties()); | 
|  | 1462 | pointerCoords.push_back(pointer.buildCoords()); | 
|  | 1463 | } | 
|  | 1464 |  | 
|  | 1465 | // Set mouse cursor position for the most common cases to avoid boilerplate. | 
|  | 1466 | if (mSource == AINPUT_SOURCE_MOUSE && | 
|  | 1467 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && | 
|  | 1468 | mPointers.size() == 1) { | 
|  | 1469 | mRawXCursorPosition = pointerCoords[0].getX(); | 
|  | 1470 | mRawYCursorPosition = pointerCoords[0].getY(); | 
|  | 1471 | } | 
|  | 1472 |  | 
|  | 1473 | MotionEvent event; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1474 | ui::Transform identityTransform; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1475 | event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC, | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1476 | mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1477 | mButtonState, MotionClassification::NONE, identityTransform, | 
|  | 1478 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 1479 | mRawYCursorPosition, identityTransform, mEventTime, mEventTime, | 
|  | 1480 | mPointers.size(), pointerProperties.data(), pointerCoords.data()); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1481 |  | 
|  | 1482 | return event; | 
|  | 1483 | } | 
|  | 1484 |  | 
|  | 1485 | private: | 
|  | 1486 | int32_t mAction; | 
|  | 1487 | int32_t mSource; | 
|  | 1488 | nsecs_t mEventTime; | 
|  | 1489 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; | 
|  | 1490 | int32_t mActionButton{0}; | 
|  | 1491 | int32_t mButtonState{0}; | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1492 | int32_t mFlags{0}; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1493 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; | 
|  | 1494 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; | 
|  | 1495 |  | 
|  | 1496 | std::vector<PointerBuilder> mPointers; | 
|  | 1497 | }; | 
|  | 1498 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1499 | static InputEventInjectionResult injectMotionEvent( | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1500 | const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1501 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1502 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, | 
|  | 1503 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { | 
|  | 1504 | return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, | 
|  | 1505 | policyFlags); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1506 | } | 
|  | 1507 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1508 | static InputEventInjectionResult injectMotionEvent( | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1509 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source, | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1510 | int32_t displayId, const PointF& position = {100, 200}, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1511 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1512 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
|  | 1513 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1514 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1515 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), | 
|  | 1516 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1517 | MotionEvent event = MotionEventBuilder(action, source) | 
|  | 1518 | .displayId(displayId) | 
|  | 1519 | .eventTime(eventTime) | 
|  | 1520 | .rawXCursorPosition(cursorPosition.x) | 
|  | 1521 | .rawYCursorPosition(cursorPosition.y) | 
|  | 1522 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 1523 | .x(position.x) | 
|  | 1524 | .y(position.y)) | 
|  | 1525 | .build(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1526 |  | 
|  | 1527 | // Inject event until dispatch out. | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1528 | return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid, | 
|  | 1529 | policyFlags); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1530 | } | 
|  | 1531 |  | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1532 | static InputEventInjectionResult injectMotionDown( | 
|  | 1533 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId, | 
|  | 1534 | const PointF& location = {100, 200}) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1535 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1536 | } | 
|  | 1537 |  | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1538 | static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1539 | int32_t source, int32_t displayId, | 
|  | 1540 | const PointF& location = {100, 200}) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1541 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1542 | } | 
|  | 1543 |  | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1544 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { | 
|  | 1545 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1546 | // Define a valid key event. | 
| Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1547 | NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, | 
|  | 1548 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, | 
|  | 1549 | KEY_A, AMETA_NONE, currentTime); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1550 |  | 
|  | 1551 | return args; | 
|  | 1552 | } | 
|  | 1553 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1554 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, | 
|  | 1555 | const std::vector<PointF>& points) { | 
|  | 1556 | size_t pointerCount = points.size(); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1557 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { | 
|  | 1558 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; | 
|  | 1559 | } | 
|  | 1560 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1561 | PointerProperties pointerProperties[pointerCount]; | 
|  | 1562 | PointerCoords pointerCoords[pointerCount]; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1563 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1564 | for (size_t i = 0; i < pointerCount; i++) { | 
|  | 1565 | pointerProperties[i].clear(); | 
|  | 1566 | pointerProperties[i].id = i; | 
|  | 1567 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; | 
| 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 | pointerCoords[i].clear(); | 
|  | 1570 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); | 
|  | 1571 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); | 
|  | 1572 | } | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1573 |  | 
|  | 1574 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1575 | // Define a valid motion event. | 
| Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1576 | NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1577 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, | 
|  | 1578 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1579 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, | 
|  | 1580 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1581 | AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 1582 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1583 |  | 
|  | 1584 | return args; | 
|  | 1585 | } | 
|  | 1586 |  | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1587 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { | 
|  | 1588 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); | 
|  | 1589 | } | 
|  | 1590 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1591 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { | 
|  | 1592 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); | 
|  | 1593 | } | 
|  | 1594 |  | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1595 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( | 
|  | 1596 | const PointerCaptureRequest& request) { | 
|  | 1597 | return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), request); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1598 | } | 
|  | 1599 |  | 
| Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1600 | /** | 
|  | 1601 | * When a window unexpectedly disposes of its input channel, policy should be notified about the | 
|  | 1602 | * broken channel. | 
|  | 1603 | */ | 
|  | 1604 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { | 
|  | 1605 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 1606 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1607 | sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 1608 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1609 |  | 
|  | 1610 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 1611 |  | 
|  | 1612 | // Window closes its channel, but the window remains. | 
|  | 1613 | window->destroyReceiver(); | 
|  | 1614 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); | 
|  | 1615 | } | 
|  | 1616 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1617 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1618 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1619 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 1620 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1621 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1622 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1623 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1624 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 1625 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1626 |  | 
|  | 1627 | // Window should receive motion event. | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1628 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1629 | } | 
|  | 1630 |  | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1631 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { | 
|  | 1632 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1633 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 1634 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1635 |  | 
|  | 1636 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 1637 | // Inject a MotionEvent to an unknown display. | 
|  | 1638 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1639 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) | 
|  | 1640 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1641 |  | 
|  | 1642 | // Window should receive motion event. | 
|  | 1643 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1644 | } | 
|  | 1645 |  | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1646 | /** | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1647 | * Calling setInputWindows once should not cause any issues. | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1648 | * This test serves as a sanity check for the next test, where setInputWindows is | 
|  | 1649 | * called twice. | 
|  | 1650 | */ | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1651 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1652 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1653 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 1654 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1655 | window->setFrame(Rect(0, 0, 100, 100)); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1656 |  | 
|  | 1657 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1658 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1659 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1660 | {50, 50})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1661 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1662 |  | 
|  | 1663 | // Window should receive motion event. | 
|  | 1664 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1665 | } | 
|  | 1666 |  | 
|  | 1667 | /** | 
|  | 1668 | * Calling setInputWindows twice, with the same info, should not cause any issues. | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1669 | */ | 
|  | 1670 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1671 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1672 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 1673 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1674 | window->setFrame(Rect(0, 0, 100, 100)); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1675 |  | 
|  | 1676 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 1677 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1678 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1679 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1680 | {50, 50})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1681 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1682 |  | 
|  | 1683 | // Window should receive motion event. | 
|  | 1684 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1685 | } | 
|  | 1686 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1687 | // The foreground window should receive the first touch down event. | 
|  | 1688 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1689 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1690 | sp<FakeWindowHandle> windowTop = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1691 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1692 | sp<FakeWindowHandle> windowSecond = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1693 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1694 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1695 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1696 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1697 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 1698 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1699 |  | 
|  | 1700 | // 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] | 1701 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1702 | windowSecond->assertNoEvents(); | 
|  | 1703 | } | 
|  | 1704 |  | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1705 | /** | 
|  | 1706 | * Two windows: A top window, and a wallpaper behind the window. | 
|  | 1707 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window | 
|  | 1708 | * gets ACTION_CANCEL. | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1709 | * 1. foregroundWindow <-- dup touch to wallpaper | 
|  | 1710 | * 2. wallpaperWindow <-- is wallpaper | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1711 | */ | 
|  | 1712 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { | 
|  | 1713 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 1714 | sp<FakeWindowHandle> foregroundWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1715 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1716 | foregroundWindow->setDupTouchToWallpaper(true); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1717 | sp<FakeWindowHandle> wallpaperWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1718 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1719 | wallpaperWindow->setIsWallpaper(true); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1720 | constexpr int expectedWallpaperFlags = | 
|  | 1721 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 1722 |  | 
|  | 1723 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); | 
|  | 1724 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1725 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1726 | {100, 200})) | 
|  | 1727 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1728 |  | 
|  | 1729 | // Both foreground window and its wallpaper should receive the touch down | 
|  | 1730 | foregroundWindow->consumeMotionDown(); | 
|  | 1731 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1732 |  | 
|  | 1733 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1734 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1735 | ADISPLAY_ID_DEFAULT, {110, 200})) | 
|  | 1736 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1737 |  | 
|  | 1738 | foregroundWindow->consumeMotionMove(); | 
|  | 1739 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1740 |  | 
|  | 1741 | // Now the foreground window goes away, but the wallpaper stays | 
|  | 1742 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); | 
|  | 1743 | foregroundWindow->consumeMotionCancel(); | 
|  | 1744 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. | 
|  | 1745 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1746 | } | 
|  | 1747 |  | 
|  | 1748 | /** | 
| Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1749 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, | 
|  | 1750 | * with the following differences: | 
|  | 1751 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to | 
|  | 1752 | * clean up the connection. | 
|  | 1753 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. | 
|  | 1754 | * Ensure that there's no crash in the dispatcher. | 
|  | 1755 | */ | 
|  | 1756 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { | 
|  | 1757 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 1758 | sp<FakeWindowHandle> foregroundWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1759 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1760 | foregroundWindow->setDupTouchToWallpaper(true); | 
| Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1761 | sp<FakeWindowHandle> wallpaperWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1762 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1763 | wallpaperWindow->setIsWallpaper(true); | 
| Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1764 | constexpr int expectedWallpaperFlags = | 
|  | 1765 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 1766 |  | 
|  | 1767 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); | 
|  | 1768 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1769 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1770 | {100, 200})) | 
|  | 1771 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1772 |  | 
|  | 1773 | // Both foreground window and its wallpaper should receive the touch down | 
|  | 1774 | foregroundWindow->consumeMotionDown(); | 
|  | 1775 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1776 |  | 
|  | 1777 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1778 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1779 | ADISPLAY_ID_DEFAULT, {110, 200})) | 
|  | 1780 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1781 |  | 
|  | 1782 | foregroundWindow->consumeMotionMove(); | 
|  | 1783 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1784 |  | 
|  | 1785 | // Wallpaper closes its channel, but the window remains. | 
|  | 1786 | wallpaperWindow->destroyReceiver(); | 
|  | 1787 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); | 
|  | 1788 |  | 
|  | 1789 | // Now the foreground window goes away, but the wallpaper stays, even though its channel | 
|  | 1790 | // is no longer valid. | 
|  | 1791 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); | 
|  | 1792 | foregroundWindow->consumeMotionCancel(); | 
|  | 1793 | } | 
|  | 1794 |  | 
|  | 1795 | /** | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1796 | * A single window that receives touch (on top), and a wallpaper window underneath it. | 
|  | 1797 | * The top window gets a multitouch gesture. | 
|  | 1798 | * Ensure that wallpaper gets the same gesture. | 
|  | 1799 | */ | 
|  | 1800 | TEST_F(InputDispatcherTest, WallpaperWindow_ReceivesMultiTouch) { | 
|  | 1801 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 1802 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1803 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1804 | window->setDupTouchToWallpaper(true); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1805 |  | 
|  | 1806 | sp<FakeWindowHandle> wallpaperWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1807 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1808 | wallpaperWindow->setIsWallpaper(true); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1809 | constexpr int expectedWallpaperFlags = | 
|  | 1810 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 1811 |  | 
|  | 1812 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, wallpaperWindow}}}); | 
|  | 1813 |  | 
|  | 1814 | // Touch down on top window | 
|  | 1815 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1816 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1817 | {100, 100})) | 
|  | 1818 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1819 |  | 
|  | 1820 | // Both top window and its wallpaper should receive the touch down | 
|  | 1821 | window->consumeMotionDown(); | 
|  | 1822 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1823 |  | 
|  | 1824 | // Second finger down on the top window | 
|  | 1825 | const MotionEvent secondFingerDownEvent = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1826 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1827 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 1828 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 1829 | .x(100) | 
|  | 1830 | .y(100)) | 
|  | 1831 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 1832 | .x(150) | 
|  | 1833 | .y(150)) | 
|  | 1834 | .build(); | 
|  | 1835 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1836 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 1837 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 1838 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1839 |  | 
|  | 1840 | window->consumeMotionPointerDown(1 /* pointerIndex */); | 
|  | 1841 | wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT, | 
|  | 1842 | expectedWallpaperFlags); | 
|  | 1843 | window->assertNoEvents(); | 
|  | 1844 | wallpaperWindow->assertNoEvents(); | 
|  | 1845 | } | 
|  | 1846 |  | 
|  | 1847 | /** | 
|  | 1848 | * Two windows: a window on the left and window on the right. | 
|  | 1849 | * A third window, wallpaper, is behind both windows, and spans both top windows. | 
|  | 1850 | * The first touch down goes to the left window. A second pointer touches down on the right window. | 
|  | 1851 | * The touch is split, so both left and right windows should receive ACTION_DOWN. | 
|  | 1852 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by | 
|  | 1853 | * ACTION_POINTER_DOWN(1). | 
|  | 1854 | */ | 
|  | 1855 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { | 
|  | 1856 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 1857 | sp<FakeWindowHandle> leftWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1858 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1859 | leftWindow->setFrame(Rect(0, 0, 200, 200)); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1860 | leftWindow->setDupTouchToWallpaper(true); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1861 |  | 
|  | 1862 | sp<FakeWindowHandle> rightWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1863 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1864 | rightWindow->setFrame(Rect(200, 0, 400, 200)); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1865 | rightWindow->setDupTouchToWallpaper(true); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1866 |  | 
|  | 1867 | sp<FakeWindowHandle> wallpaperWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1868 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1869 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1870 | wallpaperWindow->setIsWallpaper(true); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1871 | constexpr int expectedWallpaperFlags = | 
|  | 1872 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 1873 |  | 
|  | 1874 | mDispatcher->setInputWindows( | 
|  | 1875 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); | 
|  | 1876 |  | 
|  | 1877 | // Touch down on left window | 
|  | 1878 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1879 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1880 | {100, 100})) | 
|  | 1881 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1882 |  | 
|  | 1883 | // Both foreground window and its wallpaper should receive the touch down | 
|  | 1884 | leftWindow->consumeMotionDown(); | 
|  | 1885 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1886 |  | 
|  | 1887 | // Second finger down on the right window | 
|  | 1888 | const MotionEvent secondFingerDownEvent = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1889 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1890 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 1891 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 1892 | .x(100) | 
|  | 1893 | .y(100)) | 
|  | 1894 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 1895 | .x(300) | 
|  | 1896 | .y(100)) | 
|  | 1897 | .build(); | 
|  | 1898 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1899 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 1900 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 1901 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1902 |  | 
|  | 1903 | leftWindow->consumeMotionMove(); | 
|  | 1904 | // Since the touch is split, right window gets ACTION_DOWN | 
|  | 1905 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1906 | wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT, | 
|  | 1907 | expectedWallpaperFlags); | 
|  | 1908 |  | 
|  | 1909 | // Now, leftWindow, which received the first finger, disappears. | 
|  | 1910 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); | 
|  | 1911 | leftWindow->consumeMotionCancel(); | 
|  | 1912 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. | 
|  | 1913 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1914 |  | 
|  | 1915 | // The pointer that's still down on the right window moves, and goes to the right window only. | 
|  | 1916 | // As far as the dispatcher's concerned though, both pointers are still present. | 
|  | 1917 | const MotionEvent secondFingerMoveEvent = | 
|  | 1918 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 1919 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 1920 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 1921 | .x(100) | 
|  | 1922 | .y(100)) | 
|  | 1923 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 1924 | .x(310) | 
|  | 1925 | .y(110)) | 
|  | 1926 | .build(); | 
|  | 1927 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1928 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, | 
|  | 1929 | InputEventInjectionSync::WAIT_FOR_RESULT)); | 
|  | 1930 | rightWindow->consumeMotionMove(); | 
|  | 1931 |  | 
|  | 1932 | leftWindow->assertNoEvents(); | 
|  | 1933 | rightWindow->assertNoEvents(); | 
|  | 1934 | wallpaperWindow->assertNoEvents(); | 
|  | 1935 | } | 
|  | 1936 |  | 
| Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1937 | TEST_F(InputDispatcherTest, WallpaperWindowReceivesMultiTouch) { | 
|  | 1938 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 1939 | sp<FakeWindowHandle> window = | 
|  | 1940 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
|  | 1941 | window->setDupTouchToWallpaper(true); | 
|  | 1942 |  | 
|  | 1943 | sp<FakeWindowHandle> wallpaperWindow = | 
|  | 1944 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); | 
|  | 1945 | wallpaperWindow->setIsWallpaper(true); | 
|  | 1946 | constexpr int expectedWallpaperFlags = | 
|  | 1947 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 1948 | wallpaperWindow->setPreventSplitting(true); | 
|  | 1949 |  | 
|  | 1950 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, wallpaperWindow}}}); | 
|  | 1951 |  | 
|  | 1952 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1953 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1954 | {50, 50})) | 
|  | 1955 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1956 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1957 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1958 |  | 
|  | 1959 | const MotionEvent secondFingerDownEvent = | 
|  | 1960 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 1961 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 1962 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 1963 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) | 
|  | 1964 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) | 
|  | 1965 | .build(); | 
|  | 1966 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1967 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 1968 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 1969 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1970 |  | 
|  | 1971 | window->consumeMotionPointerDown(1); | 
|  | 1972 | wallpaperWindow->consumeMotionPointerDown(1, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1973 |  | 
|  | 1974 | const MotionEvent secondFingerUpEvent = | 
|  | 1975 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 1976 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 1977 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 1978 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) | 
|  | 1979 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) | 
|  | 1980 | .build(); | 
|  | 1981 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1982 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, | 
|  | 1983 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 1984 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1985 | window->consumeMotionPointerUp(1); | 
|  | 1986 | wallpaperWindow->consumeMotionPointerUp(1, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1987 |  | 
|  | 1988 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1989 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 1990 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1991 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 1992 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1993 | } | 
|  | 1994 |  | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1995 | /** | 
|  | 1996 | * On the display, have a single window, and also an area where there's no window. | 
|  | 1997 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. | 
|  | 1998 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. | 
|  | 1999 | */ | 
|  | 2000 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { | 
|  | 2001 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2002 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2003 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2004 |  | 
|  | 2005 | mDispatcher->setInputWindows({{DISPLAY_ID, {window}}}); | 
|  | 2006 | NotifyMotionArgs args; | 
|  | 2007 |  | 
|  | 2008 | // Touch down on the empty space | 
|  | 2009 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}))); | 
|  | 2010 |  | 
|  | 2011 | mDispatcher->waitForIdle(); | 
|  | 2012 | window->assertNoEvents(); | 
|  | 2013 |  | 
|  | 2014 | // Now touch down on the window with another pointer | 
|  | 2015 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}))); | 
|  | 2016 | mDispatcher->waitForIdle(); | 
|  | 2017 | window->consumeMotionDown(); | 
|  | 2018 | } | 
|  | 2019 |  | 
|  | 2020 | /** | 
|  | 2021 | * Same test as above, but instead of touching the empty space, the first touch goes to | 
|  | 2022 | * non-touchable window. | 
|  | 2023 | */ | 
|  | 2024 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { | 
|  | 2025 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2026 | sp<FakeWindowHandle> window1 = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2027 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2028 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); | 
|  | 2029 | window1->setTouchable(false); | 
|  | 2030 | sp<FakeWindowHandle> window2 = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2031 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2032 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); | 
|  | 2033 |  | 
|  | 2034 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); | 
|  | 2035 |  | 
|  | 2036 | NotifyMotionArgs args; | 
|  | 2037 | // Touch down on the non-touchable window | 
|  | 2038 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); | 
|  | 2039 |  | 
|  | 2040 | mDispatcher->waitForIdle(); | 
|  | 2041 | window1->assertNoEvents(); | 
|  | 2042 | window2->assertNoEvents(); | 
|  | 2043 |  | 
|  | 2044 | // Now touch down on the window with another pointer | 
|  | 2045 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); | 
|  | 2046 | mDispatcher->waitForIdle(); | 
|  | 2047 | window2->consumeMotionDown(); | 
|  | 2048 | } | 
|  | 2049 |  | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2050 | /** | 
|  | 2051 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds | 
|  | 2052 | * to the event time of the first ACTION_DOWN sent to the particular window. | 
|  | 2053 | */ | 
|  | 2054 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { | 
|  | 2055 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2056 | sp<FakeWindowHandle> window1 = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2057 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2058 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); | 
|  | 2059 | sp<FakeWindowHandle> window2 = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2060 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2061 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); | 
|  | 2062 |  | 
|  | 2063 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); | 
|  | 2064 |  | 
|  | 2065 | NotifyMotionArgs args; | 
|  | 2066 | // Touch down on the first window | 
|  | 2067 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); | 
|  | 2068 |  | 
|  | 2069 | mDispatcher->waitForIdle(); | 
|  | 2070 | InputEvent* inputEvent1 = window1->consume(); | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2071 | ASSERT_NE(inputEvent1, nullptr); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2072 | window2->assertNoEvents(); | 
|  | 2073 | MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1); | 
|  | 2074 | nsecs_t downTimeForWindow1 = motionEvent1.getDownTime(); | 
|  | 2075 | ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime()); | 
|  | 2076 |  | 
|  | 2077 | // Now touch down on the window with another pointer | 
|  | 2078 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); | 
|  | 2079 | mDispatcher->waitForIdle(); | 
|  | 2080 | InputEvent* inputEvent2 = window2->consume(); | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2081 | ASSERT_NE(inputEvent2, nullptr); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2082 | MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2); | 
|  | 2083 | nsecs_t downTimeForWindow2 = motionEvent2.getDownTime(); | 
|  | 2084 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); | 
|  | 2085 | ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime()); | 
|  | 2086 |  | 
|  | 2087 | // Now move the pointer on the second window | 
|  | 2088 | mDispatcher->notifyMotion( | 
|  | 2089 | &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}))); | 
|  | 2090 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2091 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2092 |  | 
|  | 2093 | // Now add new touch down on the second window | 
|  | 2094 | mDispatcher->notifyMotion( | 
|  | 2095 | &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}))); | 
|  | 2096 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2097 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2098 |  | 
|  | 2099 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line | 
|  | 2100 | window1->consumeMotionMove(); | 
|  | 2101 | window1->assertNoEvents(); | 
|  | 2102 |  | 
|  | 2103 | // Now move the pointer on the first window | 
|  | 2104 | mDispatcher->notifyMotion( | 
|  | 2105 | &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}}))); | 
|  | 2106 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2107 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2108 |  | 
|  | 2109 | mDispatcher->notifyMotion(&( | 
|  | 2110 | args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}))); | 
|  | 2111 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2112 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2113 | } | 
|  | 2114 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2115 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2116 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2117 | sp<FakeWindowHandle> windowLeft = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2118 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2119 | windowLeft->setFrame(Rect(0, 0, 600, 800)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2120 | sp<FakeWindowHandle> windowRight = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2121 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2122 | windowRight->setFrame(Rect(600, 0, 1200, 800)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2123 |  | 
|  | 2124 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2125 |  | 
|  | 2126 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); | 
|  | 2127 |  | 
|  | 2128 | // 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] | 2129 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2130 | injectMotionEvent(mDispatcher, | 
|  | 2131 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 2132 | AINPUT_SOURCE_MOUSE) | 
|  | 2133 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 2134 | .x(900) | 
|  | 2135 | .y(400)) | 
|  | 2136 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2137 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
| Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2138 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2139 |  | 
|  | 2140 | // Move cursor into 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(300) | 
|  | 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_EXIT)); | 
|  | 2150 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
| Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2151 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2152 |  | 
|  | 2153 | // Inject a series of mouse events for a mouse click | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2154 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2155 | injectMotionEvent(mDispatcher, | 
|  | 2156 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 2157 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 2158 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 2159 | .x(300) | 
|  | 2160 | .y(400)) | 
|  | 2161 | .build())); | 
|  | 2162 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2163 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2164 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2165 | injectMotionEvent(mDispatcher, | 
|  | 2166 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 2167 | AINPUT_SOURCE_MOUSE) | 
|  | 2168 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 2169 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 2170 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 2171 | .x(300) | 
|  | 2172 | .y(400)) | 
|  | 2173 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2174 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 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_RELEASE, | 
|  | 2179 | AINPUT_SOURCE_MOUSE) | 
|  | 2180 | .buttonState(0) | 
|  | 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_RELEASE)); | 
| 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_UP, AINPUT_SOURCE_MOUSE) | 
|  | 2191 | .buttonState(0) | 
|  | 2192 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 2193 | .x(300) | 
|  | 2194 | .y(400)) | 
|  | 2195 | .build())); | 
|  | 2196 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 2197 |  | 
|  | 2198 | // Move mouse cursor back to right window | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2199 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2200 | injectMotionEvent(mDispatcher, | 
|  | 2201 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 2202 | AINPUT_SOURCE_MOUSE) | 
|  | 2203 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 2204 | .x(900) | 
|  | 2205 | .y(400)) | 
|  | 2206 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2207 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); | 
|  | 2208 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
| Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2209 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2210 |  | 
|  | 2211 | // No more events | 
|  | 2212 | windowLeft->assertNoEvents(); | 
|  | 2213 | windowRight->assertNoEvents(); | 
|  | 2214 | } | 
|  | 2215 |  | 
|  | 2216 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { | 
|  | 2217 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2218 |  | 
|  | 2219 | sp<FakeWindowHandle> spyWindow = | 
|  | 2220 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); | 
|  | 2221 | spyWindow->setFrame(Rect(0, 0, 600, 800)); | 
|  | 2222 | spyWindow->setTrustedOverlay(true); | 
|  | 2223 | spyWindow->setSpy(true); | 
|  | 2224 | sp<FakeWindowHandle> window = | 
|  | 2225 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 2226 | window->setFrame(Rect(0, 0, 600, 800)); | 
|  | 2227 |  | 
|  | 2228 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2229 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); | 
|  | 2230 |  | 
|  | 2231 | // Send mouse cursor to the window | 
|  | 2232 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2233 | injectMotionEvent(mDispatcher, | 
|  | 2234 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 2235 | AINPUT_SOURCE_MOUSE) | 
|  | 2236 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 2237 | .x(100) | 
|  | 2238 | .y(100)) | 
|  | 2239 | .build())); | 
|  | 2240 |  | 
|  | 2241 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), | 
|  | 2242 | WithSource(AINPUT_SOURCE_MOUSE))); | 
|  | 2243 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), | 
|  | 2244 | WithSource(AINPUT_SOURCE_MOUSE))); | 
|  | 2245 |  | 
|  | 2246 | window->assertNoEvents(); | 
|  | 2247 | spyWindow->assertNoEvents(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2248 | } | 
|  | 2249 |  | 
|  | 2250 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected | 
|  | 2251 | // directly in this test. | 
|  | 2252 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2253 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2254 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2255 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2256 | window->setFrame(Rect(0, 0, 1200, 800)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2257 |  | 
|  | 2258 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2259 |  | 
|  | 2260 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2261 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2262 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2263 | injectMotionEvent(mDispatcher, | 
|  | 2264 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 2265 | AINPUT_SOURCE_MOUSE) | 
|  | 2266 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 2267 | .x(300) | 
|  | 2268 | .y(400)) | 
|  | 2269 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2270 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
| Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2271 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2272 | // Inject a series of mouse events for a mouse click | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2273 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2274 | injectMotionEvent(mDispatcher, | 
|  | 2275 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 2276 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 2277 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 2278 | .x(300) | 
|  | 2279 | .y(400)) | 
|  | 2280 | .build())); | 
|  | 2281 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2282 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2283 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2284 | injectMotionEvent(mDispatcher, | 
|  | 2285 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 2286 | AINPUT_SOURCE_MOUSE) | 
|  | 2287 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 2288 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 2289 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 2290 | .x(300) | 
|  | 2291 | .y(400)) | 
|  | 2292 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2293 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 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_RELEASE, | 
|  | 2298 | AINPUT_SOURCE_MOUSE) | 
|  | 2299 | .buttonState(0) | 
|  | 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_RELEASE)); | 
| 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_UP, AINPUT_SOURCE_MOUSE) | 
|  | 2310 | .buttonState(0) | 
|  | 2311 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 2312 | .x(300) | 
|  | 2313 | .y(400)) | 
|  | 2314 | .build())); | 
|  | 2315 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 2316 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2317 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2318 | injectMotionEvent(mDispatcher, | 
|  | 2319 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 2320 | AINPUT_SOURCE_MOUSE) | 
|  | 2321 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 2322 | .x(300) | 
|  | 2323 | .y(400)) | 
|  | 2324 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2325 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2326 | } | 
|  | 2327 |  | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2328 | /** | 
|  | 2329 | * Inject a mouse hover event followed by a tap from touchscreen. | 
| Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2330 | * 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] | 2331 | */ | 
|  | 2332 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { | 
|  | 2333 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2334 | sp<FakeWindowHandle> window = | 
|  | 2335 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 2336 | window->setFrame(Rect(0, 0, 100, 100)); | 
|  | 2337 |  | 
|  | 2338 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2339 |  | 
|  | 2340 | // Inject a hover_move from mouse. | 
|  | 2341 | NotifyMotionArgs motionArgs = | 
|  | 2342 | generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE, | 
|  | 2343 | ADISPLAY_ID_DEFAULT, {{50, 50}}); | 
|  | 2344 | motionArgs.xCursorPosition = 50; | 
|  | 2345 | motionArgs.yCursorPosition = 50; | 
|  | 2346 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2347 | ASSERT_NO_FATAL_FAILURE( | 
|  | 2348 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), | 
|  | 2349 | WithSource(AINPUT_SOURCE_MOUSE)))); | 
| Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2350 | ASSERT_NO_FATAL_FAILURE( | 
|  | 2351 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), | 
|  | 2352 | WithSource(AINPUT_SOURCE_MOUSE)))); | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2353 |  | 
|  | 2354 | // Tap on the window | 
|  | 2355 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2356 | ADISPLAY_ID_DEFAULT, {{10, 10}}); | 
|  | 2357 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2358 | ASSERT_NO_FATAL_FAILURE( | 
|  | 2359 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), | 
|  | 2360 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); | 
|  | 2361 |  | 
|  | 2362 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2363 | ADISPLAY_ID_DEFAULT, {{10, 10}}); | 
|  | 2364 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2365 | ASSERT_NO_FATAL_FAILURE( | 
|  | 2366 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), | 
|  | 2367 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); | 
|  | 2368 | } | 
|  | 2369 |  | 
| Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2370 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { | 
|  | 2371 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2372 | sp<FakeWindowHandle> windowDefaultDisplay = | 
|  | 2373 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", | 
|  | 2374 | ADISPLAY_ID_DEFAULT); | 
|  | 2375 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); | 
|  | 2376 | sp<FakeWindowHandle> windowSecondDisplay = | 
|  | 2377 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", | 
|  | 2378 | SECOND_DISPLAY_ID); | 
|  | 2379 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); | 
|  | 2380 |  | 
|  | 2381 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, | 
|  | 2382 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); | 
|  | 2383 |  | 
|  | 2384 | // Set cursor position in window in default display and check that hover enter and move | 
|  | 2385 | // events are generated. | 
|  | 2386 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2387 | injectMotionEvent(mDispatcher, | 
|  | 2388 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 2389 | AINPUT_SOURCE_MOUSE) | 
|  | 2390 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 2391 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 2392 | .x(300) | 
|  | 2393 | .y(600)) | 
|  | 2394 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2395 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
| Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2396 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)); | 
| Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2397 |  | 
|  | 2398 | // Remove all windows in secondary display and check that no event happens on window in | 
|  | 2399 | // primary display. | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2400 | mDispatcher->setInputWindows( | 
|  | 2401 | {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}}); | 
| Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2402 | windowDefaultDisplay->assertNoEvents(); | 
|  | 2403 |  | 
|  | 2404 | // Move cursor position in window in default display and check that only hover move | 
|  | 2405 | // event is generated and not hover enter event. | 
|  | 2406 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, | 
|  | 2407 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); | 
|  | 2408 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2409 | injectMotionEvent(mDispatcher, | 
|  | 2410 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 2411 | AINPUT_SOURCE_MOUSE) | 
|  | 2412 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 2413 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 2414 | .x(400) | 
|  | 2415 | .y(700)) | 
|  | 2416 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2417 | windowDefaultDisplay->consumeMotionEvent( | 
|  | 2418 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), | 
|  | 2419 | WithSource(AINPUT_SOURCE_MOUSE))); | 
| Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2420 | windowDefaultDisplay->assertNoEvents(); | 
|  | 2421 | } | 
|  | 2422 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2423 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2424 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2425 |  | 
|  | 2426 | sp<FakeWindowHandle> windowLeft = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2427 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2428 | windowLeft->setFrame(Rect(0, 0, 600, 800)); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2429 | sp<FakeWindowHandle> windowRight = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2430 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2431 | windowRight->setFrame(Rect(600, 0, 1200, 800)); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2432 |  | 
|  | 2433 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 2434 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2435 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2436 |  | 
|  | 2437 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of | 
|  | 2438 | // left window. This event should be dispatched to the left window. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2439 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2440 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2441 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2442 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2443 | windowRight->assertNoEvents(); | 
|  | 2444 | } | 
|  | 2445 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2446 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2447 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2448 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 2449 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2450 | window->setFocusable(true); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2451 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2452 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2453 | setFocusedWindow(window); | 
|  | 2454 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2455 | window->consumeFocusEvent(true); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2456 |  | 
|  | 2457 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 2458 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2459 |  | 
|  | 2460 | // Window should receive key down event. | 
|  | 2461 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 2462 |  | 
|  | 2463 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED | 
|  | 2464 | // on the app side. | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2465 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2466 | mDispatcher->notifyDeviceReset(&args); | 
|  | 2467 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, | 
|  | 2468 | AKEY_EVENT_FLAG_CANCELED); | 
|  | 2469 | } | 
|  | 2470 |  | 
|  | 2471 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2472 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2473 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 2474 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2475 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2476 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2477 |  | 
|  | 2478 | NotifyMotionArgs motionArgs = | 
|  | 2479 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2480 | ADISPLAY_ID_DEFAULT); | 
|  | 2481 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2482 |  | 
|  | 2483 | // Window should receive motion down event. | 
|  | 2484 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2485 |  | 
|  | 2486 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL | 
|  | 2487 | // on the app side. | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2488 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2489 | mDispatcher->notifyDeviceReset(&args); | 
|  | 2490 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, | 
|  | 2491 | 0 /*expectedFlags*/); | 
|  | 2492 | } | 
|  | 2493 |  | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2494 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { | 
|  | 2495 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2496 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 2497 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2498 | window->setFocusable(true); | 
|  | 2499 |  | 
|  | 2500 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2501 | setFocusedWindow(window); | 
|  | 2502 |  | 
|  | 2503 | window->consumeFocusEvent(true); | 
|  | 2504 |  | 
|  | 2505 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 2506 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; | 
|  | 2507 | const nsecs_t injectTime = keyArgs.eventTime; | 
|  | 2508 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); | 
|  | 2509 | mDispatcher->notifyKey(&keyArgs); | 
|  | 2510 | // The dispatching time should be always greater than or equal to intercept key timeout. | 
|  | 2511 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 2512 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= | 
|  | 2513 | std::chrono::nanoseconds(interceptKeyTimeout).count()); | 
|  | 2514 | } | 
|  | 2515 |  | 
|  | 2516 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { | 
|  | 2517 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2518 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 2519 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2520 | window->setFocusable(true); | 
|  | 2521 |  | 
|  | 2522 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2523 | setFocusedWindow(window); | 
|  | 2524 |  | 
|  | 2525 | window->consumeFocusEvent(true); | 
|  | 2526 |  | 
|  | 2527 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 2528 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); | 
|  | 2529 | mFakePolicy->setInterceptKeyTimeout(150ms); | 
|  | 2530 | mDispatcher->notifyKey(&keyDown); | 
|  | 2531 | mDispatcher->notifyKey(&keyUp); | 
|  | 2532 |  | 
|  | 2533 | // Window should receive key event immediately when same key up. | 
|  | 2534 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 2535 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); | 
|  | 2536 | } | 
|  | 2537 |  | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2538 | /** | 
| Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2539 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when | 
|  | 2540 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one | 
|  | 2541 | * ACTION_OUTSIDE event is sent per gesture. | 
|  | 2542 | */ | 
|  | 2543 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { | 
|  | 2544 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. | 
|  | 2545 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2546 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 2547 | "First Window", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2548 | window->setWatchOutsideTouch(true); | 
|  | 2549 | window->setFrame(Rect{0, 0, 100, 100}); | 
|  | 2550 | sp<FakeWindowHandle> secondWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2551 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", | 
|  | 2552 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2553 | secondWindow->setFrame(Rect{100, 100, 200, 200}); | 
|  | 2554 | sp<FakeWindowHandle> thirdWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2555 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", | 
|  | 2556 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2557 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); | 
|  | 2558 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}}); | 
|  | 2559 |  | 
|  | 2560 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. | 
|  | 2561 | NotifyMotionArgs motionArgs = | 
|  | 2562 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2563 | ADISPLAY_ID_DEFAULT, {PointF{-10, -10}}); | 
|  | 2564 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2565 | window->assertNoEvents(); | 
|  | 2566 | secondWindow->assertNoEvents(); | 
|  | 2567 |  | 
|  | 2568 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. | 
|  | 2569 | // Now, `window` should get ACTION_OUTSIDE. | 
|  | 2570 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 2571 | {PointF{-10, -10}, PointF{105, 105}}); | 
|  | 2572 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2573 | window->consumeMotionOutside(); | 
|  | 2574 | secondWindow->consumeMotionDown(); | 
|  | 2575 | thirdWindow->assertNoEvents(); | 
|  | 2576 |  | 
|  | 2577 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is | 
|  | 2578 | // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture. | 
|  | 2579 | motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 2580 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}); | 
|  | 2581 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 2582 | window->assertNoEvents(); | 
|  | 2583 | secondWindow->consumeMotionMove(); | 
|  | 2584 | thirdWindow->consumeMotionDown(); | 
|  | 2585 | } | 
|  | 2586 |  | 
| Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 2587 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { | 
|  | 2588 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2589 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 2590 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 2591 | window->setFocusable(true); | 
|  | 2592 |  | 
|  | 2593 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); | 
|  | 2594 | setFocusedWindow(window); | 
|  | 2595 |  | 
|  | 2596 | window->consumeFocusEvent(true); | 
|  | 2597 |  | 
|  | 2598 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 2599 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); | 
|  | 2600 | mDispatcher->notifyKey(&keyDown); | 
|  | 2601 | mDispatcher->notifyKey(&keyUp); | 
|  | 2602 |  | 
|  | 2603 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 2604 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); | 
|  | 2605 |  | 
|  | 2606 | // All windows are removed from the display. Ensure that we can no longer dispatch to it. | 
|  | 2607 | mDispatcher->onWindowInfosChanged({}, {}); | 
|  | 2608 |  | 
|  | 2609 | window->consumeFocusEvent(false); | 
|  | 2610 |  | 
|  | 2611 | mDispatcher->notifyKey(&keyDown); | 
|  | 2612 | mDispatcher->notifyKey(&keyUp); | 
|  | 2613 | window->assertNoEvents(); | 
|  | 2614 | } | 
|  | 2615 |  | 
| Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2616 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { | 
|  | 2617 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2618 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 2619 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 2620 | // Ensure window is non-split and have some transform. | 
|  | 2621 | window->setPreventSplitting(true); | 
|  | 2622 | window->setWindowOffset(20, 40); | 
|  | 2623 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); | 
|  | 2624 |  | 
|  | 2625 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2626 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 2627 | {50, 50})) | 
|  | 2628 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 2629 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2630 |  | 
|  | 2631 | const MotionEvent secondFingerDownEvent = | 
|  | 2632 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2633 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 2634 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 2635 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) | 
|  | 2636 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 2637 | .x(-30) | 
|  | 2638 | .y(-50)) | 
|  | 2639 | .build(); | 
|  | 2640 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2641 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 2642 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 2643 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 2644 |  | 
|  | 2645 | const MotionEvent* event = window->consumeMotion(); | 
|  | 2646 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); | 
|  | 2647 | EXPECT_EQ(70, event->getX(0));  // 50 + 20 | 
|  | 2648 | EXPECT_EQ(90, event->getY(0));  // 50 + 40 | 
|  | 2649 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 | 
|  | 2650 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 | 
|  | 2651 | } | 
|  | 2652 |  | 
| Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2653 | /** | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2654 | * Ensure the correct coordinate spaces are used by InputDispatcher. | 
|  | 2655 | * | 
|  | 2656 | * InputDispatcher works in the display space, so its coordinate system is relative to the display | 
|  | 2657 | * panel. Windows get events in the window space, and get raw coordinates in the logical display | 
|  | 2658 | * space. | 
|  | 2659 | */ | 
|  | 2660 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { | 
|  | 2661 | public: | 
|  | 2662 | void SetUp() override { | 
|  | 2663 | InputDispatcherTest::SetUp(); | 
|  | 2664 | mDisplayInfos.clear(); | 
|  | 2665 | mWindowInfos.clear(); | 
|  | 2666 | } | 
|  | 2667 |  | 
|  | 2668 | void addDisplayInfo(int displayId, const ui::Transform& transform) { | 
|  | 2669 | gui::DisplayInfo info; | 
|  | 2670 | info.displayId = displayId; | 
|  | 2671 | info.transform = transform; | 
|  | 2672 | mDisplayInfos.push_back(std::move(info)); | 
|  | 2673 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); | 
|  | 2674 | } | 
|  | 2675 |  | 
|  | 2676 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { | 
|  | 2677 | mWindowInfos.push_back(*windowHandle->getInfo()); | 
|  | 2678 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); | 
|  | 2679 | } | 
|  | 2680 |  | 
|  | 2681 | // Set up a test scenario where the display has a scaled projection and there are two windows | 
|  | 2682 | // on the display. | 
|  | 2683 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { | 
|  | 2684 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions | 
|  | 2685 | // respectively. | 
|  | 2686 | ui::Transform displayTransform; | 
|  | 2687 | displayTransform.set(2, 0, 0, 4); | 
|  | 2688 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); | 
|  | 2689 |  | 
|  | 2690 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 2691 | std::make_shared<FakeApplicationHandle>(); | 
|  | 2692 |  | 
|  | 2693 | // Add two windows to the display. Their frames are represented in the display space. | 
|  | 2694 | sp<FakeWindowHandle> firstWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2695 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", | 
|  | 2696 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2697 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); | 
|  | 2698 | addWindow(firstWindow); | 
|  | 2699 |  | 
|  | 2700 | sp<FakeWindowHandle> secondWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2701 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", | 
|  | 2702 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2703 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); | 
|  | 2704 | addWindow(secondWindow); | 
|  | 2705 | return {std::move(firstWindow), std::move(secondWindow)}; | 
|  | 2706 | } | 
|  | 2707 |  | 
|  | 2708 | private: | 
|  | 2709 | std::vector<gui::DisplayInfo> mDisplayInfos; | 
|  | 2710 | std::vector<gui::WindowInfo> mWindowInfos; | 
|  | 2711 | }; | 
|  | 2712 |  | 
|  | 2713 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) { | 
|  | 2714 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); | 
|  | 2715 | // Send down to the first window. The point is represented in the display space. The point is | 
|  | 2716 | // selected so that if the hit test was done with the transform applied to it, then it would | 
|  | 2717 | // end up in the incorrect window. | 
|  | 2718 | NotifyMotionArgs downMotionArgs = | 
|  | 2719 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2720 | ADISPLAY_ID_DEFAULT, {PointF{75, 55}}); | 
|  | 2721 | mDispatcher->notifyMotion(&downMotionArgs); | 
|  | 2722 |  | 
|  | 2723 | firstWindow->consumeMotionDown(); | 
|  | 2724 | secondWindow->assertNoEvents(); | 
|  | 2725 | } | 
|  | 2726 |  | 
|  | 2727 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, | 
|  | 2728 | // the event should be treated as being in the logical display space. | 
|  | 2729 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { | 
|  | 2730 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); | 
|  | 2731 | // Send down to the first window. The point is represented in the logical display space. The | 
|  | 2732 | // point is selected so that if the hit test was done in logical display space, then it would | 
|  | 2733 | // end up in the incorrect window. | 
|  | 2734 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 2735 | PointF{75 * 2, 55 * 4}); | 
|  | 2736 |  | 
|  | 2737 | firstWindow->consumeMotionDown(); | 
|  | 2738 | secondWindow->assertNoEvents(); | 
|  | 2739 | } | 
|  | 2740 |  | 
| Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 2741 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed | 
|  | 2742 | // event should be treated as being in the logical display space. | 
|  | 2743 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { | 
|  | 2744 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); | 
|  | 2745 |  | 
|  | 2746 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; | 
|  | 2747 | ui::Transform injectedEventTransform; | 
|  | 2748 | injectedEventTransform.set(matrix); | 
|  | 2749 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. | 
|  | 2750 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); | 
|  | 2751 |  | 
|  | 2752 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2753 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 2754 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 2755 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 2756 | .x(untransformedPoint.x) | 
|  | 2757 | .y(untransformedPoint.y)) | 
|  | 2758 | .build(); | 
|  | 2759 | event.transform(matrix); | 
|  | 2760 |  | 
|  | 2761 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, | 
|  | 2762 | InputEventInjectionSync::WAIT_FOR_RESULT); | 
|  | 2763 |  | 
|  | 2764 | firstWindow->consumeMotionDown(); | 
|  | 2765 | secondWindow->assertNoEvents(); | 
|  | 2766 | } | 
|  | 2767 |  | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2768 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { | 
|  | 2769 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); | 
|  | 2770 |  | 
|  | 2771 | // Send down to the second window. | 
|  | 2772 | NotifyMotionArgs downMotionArgs = | 
|  | 2773 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2774 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}}); | 
|  | 2775 | mDispatcher->notifyMotion(&downMotionArgs); | 
|  | 2776 |  | 
|  | 2777 | firstWindow->assertNoEvents(); | 
|  | 2778 | const MotionEvent* event = secondWindow->consumeMotion(); | 
|  | 2779 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); | 
|  | 2780 |  | 
|  | 2781 | // Ensure that the events from the "getRaw" API are in logical display coordinates. | 
|  | 2782 | EXPECT_EQ(300, event->getRawX(0)); | 
|  | 2783 | EXPECT_EQ(880, event->getRawY(0)); | 
|  | 2784 |  | 
|  | 2785 | // Ensure that the x and y values are in the window's coordinate space. | 
|  | 2786 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in | 
|  | 2787 | // the logical display space. This will be the origin of the window space. | 
|  | 2788 | EXPECT_EQ(100, event->getX(0)); | 
|  | 2789 | EXPECT_EQ(80, event->getY(0)); | 
|  | 2790 | } | 
|  | 2791 |  | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2792 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, | 
|  | 2793 | sp<IBinder>, sp<IBinder>)>; | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2794 |  | 
|  | 2795 | class TransferTouchFixture : public InputDispatcherTest, | 
|  | 2796 | public ::testing::WithParamInterface<TransferFunction> {}; | 
|  | 2797 |  | 
|  | 2798 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2799 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2800 |  | 
|  | 2801 | // Create a couple of windows | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2802 | sp<FakeWindowHandle> firstWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2803 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", | 
|  | 2804 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2805 | sp<FakeWindowHandle> secondWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2806 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", | 
|  | 2807 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2808 |  | 
|  | 2809 | // Add the windows to the dispatcher | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2810 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2811 |  | 
|  | 2812 | // Send down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2813 | NotifyMotionArgs downMotionArgs = | 
|  | 2814 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2815 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2816 | mDispatcher->notifyMotion(&downMotionArgs); | 
|  | 2817 | // Only the first window should get the down event | 
|  | 2818 | firstWindow->consumeMotionDown(); | 
|  | 2819 | secondWindow->assertNoEvents(); | 
|  | 2820 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2821 | // Transfer touch to the second window | 
|  | 2822 | TransferFunction f = GetParam(); | 
|  | 2823 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); | 
|  | 2824 | ASSERT_TRUE(success); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2825 | // The first window gets cancel and the second gets down | 
|  | 2826 | firstWindow->consumeMotionCancel(); | 
|  | 2827 | secondWindow->consumeMotionDown(); | 
|  | 2828 |  | 
|  | 2829 | // Send up event to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2830 | NotifyMotionArgs upMotionArgs = | 
|  | 2831 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2832 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2833 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 2834 | // The first  window gets no events and the second gets up | 
|  | 2835 | firstWindow->assertNoEvents(); | 
|  | 2836 | secondWindow->consumeMotionUp(); | 
|  | 2837 | } | 
|  | 2838 |  | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2839 | /** | 
|  | 2840 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch | 
|  | 2841 | * from. When we have spy windows, there are several windows to choose from: either spy, or the | 
|  | 2842 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most | 
|  | 2843 | * natural to the user. | 
|  | 2844 | * In this test, we are sending a pointer to both spy window and first window. We then try to | 
|  | 2845 | * transfer touch to the second window. The dispatcher should identify the first window as the | 
|  | 2846 | * one that should lose the gesture, and therefore the action should be to move the gesture from | 
|  | 2847 | * the first window to the second. | 
|  | 2848 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test | 
|  | 2849 | * the other API, as well. | 
|  | 2850 | */ | 
|  | 2851 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { | 
|  | 2852 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2853 |  | 
|  | 2854 | // Create a couple of windows + a spy window | 
|  | 2855 | sp<FakeWindowHandle> spyWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2856 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2857 | spyWindow->setTrustedOverlay(true); | 
|  | 2858 | spyWindow->setSpy(true); | 
|  | 2859 | sp<FakeWindowHandle> firstWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2860 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2861 | sp<FakeWindowHandle> secondWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2862 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2863 |  | 
|  | 2864 | // Add the windows to the dispatcher | 
|  | 2865 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}}); | 
|  | 2866 |  | 
|  | 2867 | // Send down to the first window | 
|  | 2868 | NotifyMotionArgs downMotionArgs = | 
|  | 2869 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2870 | ADISPLAY_ID_DEFAULT); | 
|  | 2871 | mDispatcher->notifyMotion(&downMotionArgs); | 
|  | 2872 | // Only the first window and spy should get the down event | 
|  | 2873 | spyWindow->consumeMotionDown(); | 
|  | 2874 | firstWindow->consumeMotionDown(); | 
|  | 2875 |  | 
|  | 2876 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window | 
|  | 2877 | // if f === 'transferTouch'. | 
|  | 2878 | TransferFunction f = GetParam(); | 
|  | 2879 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); | 
|  | 2880 | ASSERT_TRUE(success); | 
|  | 2881 | // The first window gets cancel and the second gets down | 
|  | 2882 | firstWindow->consumeMotionCancel(); | 
|  | 2883 | secondWindow->consumeMotionDown(); | 
|  | 2884 |  | 
|  | 2885 | // Send up event to the second window | 
|  | 2886 | NotifyMotionArgs upMotionArgs = | 
|  | 2887 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2888 | ADISPLAY_ID_DEFAULT); | 
|  | 2889 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 2890 | // The first  window gets no events and the second+spy get up | 
|  | 2891 | firstWindow->assertNoEvents(); | 
|  | 2892 | spyWindow->consumeMotionUp(); | 
|  | 2893 | secondWindow->consumeMotionUp(); | 
|  | 2894 | } | 
|  | 2895 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2896 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2897 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2898 |  | 
|  | 2899 | PointF touchPoint = {10, 10}; | 
|  | 2900 |  | 
|  | 2901 | // Create a couple of windows | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2902 | sp<FakeWindowHandle> firstWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2903 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", | 
|  | 2904 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 2905 | firstWindow->setPreventSplitting(true); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2906 | sp<FakeWindowHandle> secondWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2907 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", | 
|  | 2908 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 2909 | secondWindow->setPreventSplitting(true); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2910 |  | 
|  | 2911 | // Add the windows to the dispatcher | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2912 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2913 |  | 
|  | 2914 | // Send down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2915 | NotifyMotionArgs downMotionArgs = | 
|  | 2916 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2917 | ADISPLAY_ID_DEFAULT, {touchPoint}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2918 | mDispatcher->notifyMotion(&downMotionArgs); | 
|  | 2919 | // Only the first window should get the down event | 
|  | 2920 | firstWindow->consumeMotionDown(); | 
|  | 2921 | secondWindow->assertNoEvents(); | 
|  | 2922 |  | 
|  | 2923 | // Send pointer down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2924 | NotifyMotionArgs pointerDownMotionArgs = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2925 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2926 | {touchPoint, touchPoint}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2927 | mDispatcher->notifyMotion(&pointerDownMotionArgs); | 
|  | 2928 | // Only the first window should get the pointer down event | 
|  | 2929 | firstWindow->consumeMotionPointerDown(1); | 
|  | 2930 | secondWindow->assertNoEvents(); | 
|  | 2931 |  | 
|  | 2932 | // Transfer touch focus to the second window | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2933 | TransferFunction f = GetParam(); | 
|  | 2934 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); | 
|  | 2935 | ASSERT_TRUE(success); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2936 | // The first window gets cancel and the second gets down and pointer down | 
|  | 2937 | firstWindow->consumeMotionCancel(); | 
|  | 2938 | secondWindow->consumeMotionDown(); | 
|  | 2939 | secondWindow->consumeMotionPointerDown(1); | 
|  | 2940 |  | 
|  | 2941 | // Send pointer up to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2942 | NotifyMotionArgs pointerUpMotionArgs = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2943 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2944 | {touchPoint, touchPoint}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2945 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 2946 | // The first window gets nothing and the second gets pointer up | 
|  | 2947 | firstWindow->assertNoEvents(); | 
|  | 2948 | secondWindow->consumeMotionPointerUp(1); | 
|  | 2949 |  | 
|  | 2950 | // Send up event to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2951 | NotifyMotionArgs upMotionArgs = | 
|  | 2952 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2953 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2954 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 2955 | // The first window gets nothing and the second gets up | 
|  | 2956 | firstWindow->assertNoEvents(); | 
|  | 2957 | secondWindow->consumeMotionUp(); | 
|  | 2958 | } | 
|  | 2959 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2960 | // For the cases of single pointer touch and two pointers non-split touch, the api's | 
|  | 2961 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ | 
|  | 2962 | // for the case where there are multiple pointers split across several windows. | 
|  | 2963 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, | 
|  | 2964 | ::testing::Values( | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2965 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, | 
|  | 2966 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2967 | return dispatcher->transferTouch(destChannelToken, | 
|  | 2968 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2969 | }, | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2970 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, | 
|  | 2971 | sp<IBinder> from, sp<IBinder> to) { | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2972 | return dispatcher->transferTouchFocus(from, to, | 
|  | 2973 | false /*isDragAndDrop*/); | 
|  | 2974 | })); | 
|  | 2975 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2976 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2977 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2978 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2979 | sp<FakeWindowHandle> firstWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2980 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", | 
|  | 2981 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2982 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2983 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2984 | sp<FakeWindowHandle> secondWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2985 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", | 
|  | 2986 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2987 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2988 |  | 
|  | 2989 | // Add the windows to the dispatcher | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2990 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2991 |  | 
|  | 2992 | PointF pointInFirst = {300, 200}; | 
|  | 2993 | PointF pointInSecond = {300, 600}; | 
|  | 2994 |  | 
|  | 2995 | // Send down to the first window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2996 | NotifyMotionArgs firstDownMotionArgs = | 
|  | 2997 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2998 | ADISPLAY_ID_DEFAULT, {pointInFirst}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2999 | mDispatcher->notifyMotion(&firstDownMotionArgs); | 
|  | 3000 | // Only the first window should get the down event | 
|  | 3001 | firstWindow->consumeMotionDown(); | 
|  | 3002 | secondWindow->assertNoEvents(); | 
|  | 3003 |  | 
|  | 3004 | // Send down to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3005 | NotifyMotionArgs secondDownMotionArgs = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3006 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3007 | {pointInFirst, pointInSecond}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3008 | mDispatcher->notifyMotion(&secondDownMotionArgs); | 
|  | 3009 | // The first window gets a move and the second a down | 
|  | 3010 | firstWindow->consumeMotionMove(); | 
|  | 3011 | secondWindow->consumeMotionDown(); | 
|  | 3012 |  | 
|  | 3013 | // Transfer touch focus to the second window | 
|  | 3014 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); | 
|  | 3015 | // The first window gets cancel and the new gets pointer down (it already saw down) | 
|  | 3016 | firstWindow->consumeMotionCancel(); | 
|  | 3017 | secondWindow->consumeMotionPointerDown(1); | 
|  | 3018 |  | 
|  | 3019 | // Send pointer up to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3020 | NotifyMotionArgs pointerUpMotionArgs = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3021 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3022 | {pointInFirst, pointInSecond}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3023 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 3024 | // The first window gets nothing and the second gets pointer up | 
|  | 3025 | firstWindow->assertNoEvents(); | 
|  | 3026 | secondWindow->consumeMotionPointerUp(1); | 
|  | 3027 |  | 
|  | 3028 | // Send up event to the second window | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3029 | NotifyMotionArgs upMotionArgs = | 
|  | 3030 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3031 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3032 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 3033 | // The first window gets nothing and the second gets up | 
|  | 3034 | firstWindow->assertNoEvents(); | 
|  | 3035 | secondWindow->consumeMotionUp(); | 
|  | 3036 | } | 
|  | 3037 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3038 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. | 
|  | 3039 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving | 
|  | 3040 | // touch is not supported, so the touch should continue on those windows and the transferred-to | 
|  | 3041 | // window should get nothing. | 
|  | 3042 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { | 
|  | 3043 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3044 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3045 | sp<FakeWindowHandle> firstWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3046 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", | 
|  | 3047 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3048 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3049 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3050 | sp<FakeWindowHandle> secondWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3051 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", | 
|  | 3052 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3053 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3054 |  | 
|  | 3055 | // Add the windows to the dispatcher | 
|  | 3056 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
|  | 3057 |  | 
|  | 3058 | PointF pointInFirst = {300, 200}; | 
|  | 3059 | PointF pointInSecond = {300, 600}; | 
|  | 3060 |  | 
|  | 3061 | // Send down to the first window | 
|  | 3062 | NotifyMotionArgs firstDownMotionArgs = | 
|  | 3063 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3064 | ADISPLAY_ID_DEFAULT, {pointInFirst}); | 
|  | 3065 | mDispatcher->notifyMotion(&firstDownMotionArgs); | 
|  | 3066 | // Only the first window should get the down event | 
|  | 3067 | firstWindow->consumeMotionDown(); | 
|  | 3068 | secondWindow->assertNoEvents(); | 
|  | 3069 |  | 
|  | 3070 | // Send down to the second window | 
|  | 3071 | NotifyMotionArgs secondDownMotionArgs = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3072 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3073 | {pointInFirst, pointInSecond}); | 
|  | 3074 | mDispatcher->notifyMotion(&secondDownMotionArgs); | 
|  | 3075 | // The first window gets a move and the second a down | 
|  | 3076 | firstWindow->consumeMotionMove(); | 
|  | 3077 | secondWindow->consumeMotionDown(); | 
|  | 3078 |  | 
|  | 3079 | // Transfer touch focus to the second window | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3080 | const bool transferred = | 
|  | 3081 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3082 | // The 'transferTouch' call should not succeed, because there are 2 touched windows | 
|  | 3083 | ASSERT_FALSE(transferred); | 
|  | 3084 | firstWindow->assertNoEvents(); | 
|  | 3085 | secondWindow->assertNoEvents(); | 
|  | 3086 |  | 
|  | 3087 | // The rest of the dispatch should proceed as normal | 
|  | 3088 | // Send pointer up to the second window | 
|  | 3089 | NotifyMotionArgs pointerUpMotionArgs = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3090 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3091 | {pointInFirst, pointInSecond}); | 
|  | 3092 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 3093 | // The first window gets MOVE and the second gets pointer up | 
|  | 3094 | firstWindow->consumeMotionMove(); | 
|  | 3095 | secondWindow->consumeMotionUp(); | 
|  | 3096 |  | 
|  | 3097 | // Send up event to the first window | 
|  | 3098 | NotifyMotionArgs upMotionArgs = | 
|  | 3099 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3100 | ADISPLAY_ID_DEFAULT); | 
|  | 3101 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 3102 | // The first window gets nothing and the second gets up | 
|  | 3103 | firstWindow->consumeMotionUp(); | 
|  | 3104 | secondWindow->assertNoEvents(); | 
|  | 3105 | } | 
|  | 3106 |  | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3107 | // This case will create two windows and one mirrored window on the default display and mirror | 
|  | 3108 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put | 
|  | 3109 | // the windows info of second display before default display. | 
|  | 3110 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { | 
|  | 3111 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3112 | sp<FakeWindowHandle> firstWindowInPrimary = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3113 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3114 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3115 | sp<FakeWindowHandle> secondWindowInPrimary = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3116 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3117 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3118 |  | 
|  | 3119 | sp<FakeWindowHandle> mirrorWindowInPrimary = | 
|  | 3120 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); | 
|  | 3121 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3122 |  | 
|  | 3123 | sp<FakeWindowHandle> firstWindowInSecondary = | 
|  | 3124 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); | 
|  | 3125 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3126 |  | 
|  | 3127 | sp<FakeWindowHandle> secondWindowInSecondary = | 
|  | 3128 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); | 
|  | 3129 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3130 |  | 
|  | 3131 | // Update window info, let it find window handle of second display first. | 
|  | 3132 | mDispatcher->setInputWindows( | 
|  | 3133 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, | 
|  | 3134 | {ADISPLAY_ID_DEFAULT, | 
|  | 3135 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); | 
|  | 3136 |  | 
|  | 3137 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3138 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3139 | {50, 50})) | 
|  | 3140 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 3141 |  | 
|  | 3142 | // Window should receive motion event. | 
|  | 3143 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3144 |  | 
|  | 3145 | // Transfer touch focus | 
|  | 3146 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), | 
|  | 3147 | secondWindowInPrimary->getToken())); | 
|  | 3148 | // The first window gets cancel. | 
|  | 3149 | firstWindowInPrimary->consumeMotionCancel(); | 
|  | 3150 | secondWindowInPrimary->consumeMotionDown(); | 
|  | 3151 |  | 
|  | 3152 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3153 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3154 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 3155 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 3156 | firstWindowInPrimary->assertNoEvents(); | 
|  | 3157 | secondWindowInPrimary->consumeMotionMove(); | 
|  | 3158 |  | 
|  | 3159 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3160 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3161 | {150, 50})) | 
|  | 3162 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 3163 | firstWindowInPrimary->assertNoEvents(); | 
|  | 3164 | secondWindowInPrimary->consumeMotionUp(); | 
|  | 3165 | } | 
|  | 3166 |  | 
|  | 3167 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use | 
|  | 3168 | // 'transferTouch' api. | 
|  | 3169 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { | 
|  | 3170 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3171 | sp<FakeWindowHandle> firstWindowInPrimary = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3172 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3173 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3174 | sp<FakeWindowHandle> secondWindowInPrimary = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3175 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3176 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3177 |  | 
|  | 3178 | sp<FakeWindowHandle> mirrorWindowInPrimary = | 
|  | 3179 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); | 
|  | 3180 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3181 |  | 
|  | 3182 | sp<FakeWindowHandle> firstWindowInSecondary = | 
|  | 3183 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); | 
|  | 3184 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3185 |  | 
|  | 3186 | sp<FakeWindowHandle> secondWindowInSecondary = | 
|  | 3187 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); | 
|  | 3188 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3189 |  | 
|  | 3190 | // Update window info, let it find window handle of second display first. | 
|  | 3191 | mDispatcher->setInputWindows( | 
|  | 3192 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, | 
|  | 3193 | {ADISPLAY_ID_DEFAULT, | 
|  | 3194 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); | 
|  | 3195 |  | 
|  | 3196 | // Touch on second display. | 
|  | 3197 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3198 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) | 
|  | 3199 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 3200 |  | 
|  | 3201 | // Window should receive motion event. | 
|  | 3202 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); | 
|  | 3203 |  | 
|  | 3204 | // Transfer touch focus | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3205 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3206 |  | 
|  | 3207 | // The first window gets cancel. | 
|  | 3208 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); | 
|  | 3209 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); | 
|  | 3210 |  | 
|  | 3211 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3212 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3213 | SECOND_DISPLAY_ID, {150, 50})) | 
|  | 3214 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 3215 | firstWindowInPrimary->assertNoEvents(); | 
|  | 3216 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); | 
|  | 3217 |  | 
|  | 3218 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3219 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) | 
|  | 3220 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 3221 | firstWindowInPrimary->assertNoEvents(); | 
|  | 3222 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); | 
|  | 3223 | } | 
|  | 3224 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3225 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3226 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3227 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3228 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3229 |  | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3230 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3231 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3232 | setFocusedWindow(window); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3233 |  | 
|  | 3234 | window->consumeFocusEvent(true); | 
|  | 3235 |  | 
|  | 3236 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 3237 | mDispatcher->notifyKey(&keyArgs); | 
|  | 3238 |  | 
|  | 3239 | // Window should receive key down event. | 
|  | 3240 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 3241 | } | 
|  | 3242 |  | 
|  | 3243 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3244 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3245 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3246 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3247 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3248 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3249 |  | 
|  | 3250 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 3251 | mDispatcher->notifyKey(&keyArgs); | 
|  | 3252 | mDispatcher->waitForIdle(); | 
|  | 3253 |  | 
|  | 3254 | window->assertNoEvents(); | 
|  | 3255 | } | 
|  | 3256 |  | 
|  | 3257 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys | 
|  | 3258 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3259 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3260 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3261 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3262 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3263 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3264 |  | 
|  | 3265 | // Send key | 
|  | 3266 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 3267 | mDispatcher->notifyKey(&keyArgs); | 
|  | 3268 | // Send motion | 
|  | 3269 | NotifyMotionArgs motionArgs = | 
|  | 3270 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3271 | ADISPLAY_ID_DEFAULT); | 
|  | 3272 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 3273 |  | 
|  | 3274 | // Window should receive only the motion event | 
|  | 3275 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3276 | window->assertNoEvents(); // Key event or focus event will not be received | 
|  | 3277 | } | 
|  | 3278 |  | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3279 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { | 
|  | 3280 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3281 |  | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3282 | sp<FakeWindowHandle> firstWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3283 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", | 
|  | 3284 | ADISPLAY_ID_DEFAULT); | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3285 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3286 |  | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3287 | sp<FakeWindowHandle> secondWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3288 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", | 
|  | 3289 | ADISPLAY_ID_DEFAULT); | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3290 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3291 |  | 
|  | 3292 | // Add the windows to the dispatcher | 
|  | 3293 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
|  | 3294 |  | 
|  | 3295 | PointF pointInFirst = {300, 200}; | 
|  | 3296 | PointF pointInSecond = {300, 600}; | 
|  | 3297 |  | 
|  | 3298 | // Send down to the first window | 
|  | 3299 | NotifyMotionArgs firstDownMotionArgs = | 
|  | 3300 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3301 | ADISPLAY_ID_DEFAULT, {pointInFirst}); | 
|  | 3302 | mDispatcher->notifyMotion(&firstDownMotionArgs); | 
|  | 3303 | // Only the first window should get the down event | 
|  | 3304 | firstWindow->consumeMotionDown(); | 
|  | 3305 | secondWindow->assertNoEvents(); | 
|  | 3306 |  | 
|  | 3307 | // Send down to the second window | 
|  | 3308 | NotifyMotionArgs secondDownMotionArgs = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3309 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3310 | {pointInFirst, pointInSecond}); | 
|  | 3311 | mDispatcher->notifyMotion(&secondDownMotionArgs); | 
|  | 3312 | // The first window gets a move and the second a down | 
|  | 3313 | firstWindow->consumeMotionMove(); | 
|  | 3314 | secondWindow->consumeMotionDown(); | 
|  | 3315 |  | 
|  | 3316 | // Send pointer cancel to the second window | 
|  | 3317 | NotifyMotionArgs pointerUpMotionArgs = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3318 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3319 | {pointInFirst, pointInSecond}); | 
|  | 3320 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; | 
|  | 3321 | mDispatcher->notifyMotion(&pointerUpMotionArgs); | 
|  | 3322 | // The first window gets move and the second gets cancel. | 
|  | 3323 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); | 
|  | 3324 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); | 
|  | 3325 |  | 
|  | 3326 | // Send up event. | 
|  | 3327 | NotifyMotionArgs upMotionArgs = | 
|  | 3328 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3329 | ADISPLAY_ID_DEFAULT); | 
|  | 3330 | mDispatcher->notifyMotion(&upMotionArgs); | 
|  | 3331 | // The first window gets up and the second gets nothing. | 
|  | 3332 | firstWindow->consumeMotionUp(); | 
|  | 3333 | secondWindow->assertNoEvents(); | 
|  | 3334 | } | 
|  | 3335 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3336 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { | 
|  | 3337 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3338 |  | 
|  | 3339 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3340 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3341 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3342 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; | 
|  | 3343 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; | 
|  | 3344 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; | 
|  | 3345 |  | 
|  | 3346 | window->sendTimeline(1 /*inputEventId*/, graphicsTimeline); | 
|  | 3347 | window->assertNoEvents(); | 
|  | 3348 | mDispatcher->waitForIdle(); | 
|  | 3349 | } | 
|  | 3350 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3351 | class FakeMonitorReceiver { | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3352 | public: | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3353 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3354 | int32_t displayId) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3355 | base::Result<std::unique_ptr<InputChannel>> channel = | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3356 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3357 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3358 | } | 
|  | 3359 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3360 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } | 
|  | 3361 |  | 
|  | 3362 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 3363 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, | 
|  | 3364 | expectedDisplayId, expectedFlags); | 
|  | 3365 | } | 
|  | 3366 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3367 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } | 
|  | 3368 |  | 
|  | 3369 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } | 
|  | 3370 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3371 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 3372 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, | 
|  | 3373 | expectedDisplayId, expectedFlags); | 
|  | 3374 | } | 
|  | 3375 |  | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3376 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 3377 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, | 
|  | 3378 | expectedDisplayId, expectedFlags); | 
|  | 3379 | } | 
|  | 3380 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3381 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 3382 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, | 
|  | 3383 | expectedDisplayId, expectedFlags); | 
|  | 3384 | } | 
|  | 3385 |  | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3386 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
|  | 3387 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, | 
|  | 3388 | expectedDisplayId, expectedFlags); | 
|  | 3389 | } | 
|  | 3390 |  | 
| Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 3391 | void consumeMotionPointerDown(int32_t pointerIdx) { | 
|  | 3392 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 3393 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
|  | 3394 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT, | 
|  | 3395 | 0 /*expectedFlags*/); | 
|  | 3396 | } | 
|  | 3397 |  | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3398 | MotionEvent* consumeMotion() { | 
|  | 3399 | InputEvent* event = mInputReceiver->consume(); | 
|  | 3400 | if (!event) { | 
|  | 3401 | ADD_FAILURE() << "No event was produced"; | 
|  | 3402 | return nullptr; | 
|  | 3403 | } | 
|  | 3404 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { | 
|  | 3405 | ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion"; | 
|  | 3406 | return nullptr; | 
|  | 3407 | } | 
|  | 3408 | return static_cast<MotionEvent*>(event); | 
|  | 3409 | } | 
|  | 3410 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3411 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } | 
|  | 3412 |  | 
|  | 3413 | private: | 
|  | 3414 | std::unique_ptr<FakeInputReceiver> mInputReceiver; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3415 | }; | 
|  | 3416 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3417 | using InputDispatcherMonitorTest = InputDispatcherTest; | 
|  | 3418 |  | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3419 | /** | 
|  | 3420 | * Two entities that receive touch: A window, and a global monitor. | 
|  | 3421 | * The touch goes to the window, and then the window disappears. | 
|  | 3422 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled | 
|  | 3423 | * for the monitor, as well. | 
|  | 3424 | * 1. foregroundWindow | 
|  | 3425 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) | 
|  | 3426 | */ | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3427 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3428 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3429 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3430 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3431 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3432 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3433 |  | 
|  | 3434 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3435 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3436 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3437 | {100, 200})) | 
|  | 3438 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 3439 |  | 
|  | 3440 | // Both the foreground window and the global monitor should receive the touch down | 
|  | 3441 | window->consumeMotionDown(); | 
|  | 3442 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3443 |  | 
|  | 3444 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3445 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3446 | ADISPLAY_ID_DEFAULT, {110, 200})) | 
|  | 3447 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 3448 |  | 
|  | 3449 | window->consumeMotionMove(); | 
|  | 3450 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 3451 |  | 
|  | 3452 | // Now the foreground window goes away | 
|  | 3453 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); | 
|  | 3454 | window->consumeMotionCancel(); | 
|  | 3455 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet | 
|  | 3456 |  | 
|  | 3457 | // If more events come in, there will be no more foreground window to send them to. This will | 
|  | 3458 | // cause a cancel for the monitor, as well. | 
|  | 3459 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
|  | 3460 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3461 | ADISPLAY_ID_DEFAULT, {120, 200})) | 
|  | 3462 | << "Injection should fail because the window was removed"; | 
|  | 3463 | window->assertNoEvents(); | 
|  | 3464 | // Global monitor now gets the cancel | 
|  | 3465 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); | 
|  | 3466 | } | 
|  | 3467 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3468 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3469 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3470 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3471 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3472 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3473 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3474 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3475 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3476 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3477 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3478 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3479 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3480 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3481 | } | 
|  | 3482 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3483 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { | 
|  | 3484 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3485 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3486 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3487 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3488 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3489 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3490 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3491 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3492 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3493 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3494 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3495 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3496 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3497 | // Pilfer pointers from the monitor. | 
|  | 3498 | // This should not do anything and the window should continue to receive events. | 
|  | 3499 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3500 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3501 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3502 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3503 | ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3504 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3505 |  | 
|  | 3506 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 3507 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3508 | } | 
|  | 3509 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3510 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3511 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3512 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3513 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3514 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3515 | window->setWindowOffset(20, 40); | 
|  | 3516 | window->setWindowTransform(0, 1, -1, 0); | 
|  | 3517 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3518 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3519 |  | 
|  | 3520 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3521 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 3522 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 3523 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3524 | MotionEvent* event = monitor.consumeMotion(); | 
|  | 3525 | // Even though window has transform, gesture monitor must not. | 
|  | 3526 | ASSERT_EQ(ui::Transform(), event->getTransform()); | 
|  | 3527 | } | 
|  | 3528 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3529 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { | 
| Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3530 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3531 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3532 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3533 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3534 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3535 | << "Injection should fail if there is a monitor, but no touchable window"; | 
|  | 3536 | monitor.assertNoEvents(); | 
| Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3537 | } | 
|  | 3538 |  | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3539 | TEST_F(InputDispatcherTest, TestMoveEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3540 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3541 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3542 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3543 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3544 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3545 |  | 
|  | 3546 | NotifyMotionArgs motionArgs = | 
|  | 3547 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3548 | ADISPLAY_ID_DEFAULT); | 
|  | 3549 |  | 
|  | 3550 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 3551 | // Window should receive motion down event. | 
|  | 3552 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3553 |  | 
|  | 3554 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3555 | motionArgs.id += 1; | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3556 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 3557 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, | 
|  | 3558 | motionArgs.pointerCoords[0].getX() - 10); | 
|  | 3559 |  | 
|  | 3560 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 3561 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, | 
|  | 3562 | 0 /*expectedFlags*/); | 
|  | 3563 | } | 
|  | 3564 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3565 | /** | 
|  | 3566 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to | 
|  | 3567 | * the device default right away. In the test scenario, we check both the default value, | 
|  | 3568 | * and the action of enabling / disabling. | 
|  | 3569 | */ | 
|  | 3570 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3571 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3572 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3573 | "Test window", ADISPLAY_ID_DEFAULT); | 
| Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 3574 | const WindowInfo& windowInfo = *window->getInfo(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3575 |  | 
|  | 3576 | // Set focused application. | 
|  | 3577 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3578 | window->setFocusable(true); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3579 |  | 
|  | 3580 | SCOPED_TRACE("Check default value of touch mode"); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3581 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3582 | setFocusedWindow(window); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3583 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 3584 |  | 
|  | 3585 | SCOPED_TRACE("Remove the window to trigger focus loss"); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3586 | window->setFocusable(false); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3587 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3588 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); | 
|  | 3589 |  | 
|  | 3590 | SCOPED_TRACE("Disable touch mode"); | 
| Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 3591 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, | 
| Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 3592 | true /*hasPermission*/, ADISPLAY_ID_DEFAULT); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3593 | window->consumeTouchModeEvent(false); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3594 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3595 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3596 | setFocusedWindow(window); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3597 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); | 
|  | 3598 |  | 
|  | 3599 | SCOPED_TRACE("Remove the window to trigger focus loss"); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3600 | window->setFocusable(false); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3601 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3602 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); | 
|  | 3603 |  | 
|  | 3604 | SCOPED_TRACE("Enable touch mode again"); | 
| Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 3605 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, | 
| Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 3606 | true /*hasPermission*/, ADISPLAY_ID_DEFAULT); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3607 | window->consumeTouchModeEvent(true); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3608 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3609 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3610 | setFocusedWindow(window); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3611 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 3612 |  | 
|  | 3613 | window->assertNoEvents(); | 
|  | 3614 | } | 
|  | 3615 |  | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3616 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3617 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3618 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3619 | "Test window", ADISPLAY_ID_DEFAULT); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3620 |  | 
|  | 3621 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3622 | window->setFocusable(true); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3623 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3624 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3625 | setFocusedWindow(window); | 
|  | 3626 |  | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3627 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 3628 |  | 
|  | 3629 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); | 
|  | 3630 | mDispatcher->notifyKey(&keyArgs); | 
|  | 3631 |  | 
|  | 3632 | InputEvent* event = window->consume(); | 
|  | 3633 | ASSERT_NE(event, nullptr); | 
|  | 3634 |  | 
|  | 3635 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); | 
|  | 3636 | ASSERT_NE(verified, nullptr); | 
|  | 3637 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); | 
|  | 3638 |  | 
|  | 3639 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); | 
|  | 3640 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); | 
|  | 3641 | ASSERT_EQ(keyArgs.source, verified->source); | 
|  | 3642 | ASSERT_EQ(keyArgs.displayId, verified->displayId); | 
|  | 3643 |  | 
|  | 3644 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); | 
|  | 3645 |  | 
|  | 3646 | ASSERT_EQ(keyArgs.action, verifiedKey.action); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3647 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); | 
| Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 3648 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3649 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); | 
|  | 3650 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); | 
|  | 3651 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); | 
|  | 3652 | ASSERT_EQ(0, verifiedKey.repeatCount); | 
|  | 3653 | } | 
|  | 3654 |  | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3655 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3656 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3657 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3658 | "Test window", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3659 |  | 
|  | 3660 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3661 |  | 
| Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3662 | ui::Transform transform; | 
|  | 3663 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); | 
|  | 3664 |  | 
|  | 3665 | gui::DisplayInfo displayInfo; | 
|  | 3666 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; | 
|  | 3667 | displayInfo.transform = transform; | 
|  | 3668 |  | 
|  | 3669 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo}); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3670 |  | 
|  | 3671 | NotifyMotionArgs motionArgs = | 
|  | 3672 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3673 | ADISPLAY_ID_DEFAULT); | 
|  | 3674 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 3675 |  | 
|  | 3676 | InputEvent* event = window->consume(); | 
|  | 3677 | ASSERT_NE(event, nullptr); | 
|  | 3678 |  | 
|  | 3679 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); | 
|  | 3680 | ASSERT_NE(verified, nullptr); | 
|  | 3681 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); | 
|  | 3682 |  | 
|  | 3683 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); | 
|  | 3684 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); | 
|  | 3685 | EXPECT_EQ(motionArgs.source, verified->source); | 
|  | 3686 | EXPECT_EQ(motionArgs.displayId, verified->displayId); | 
|  | 3687 |  | 
|  | 3688 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); | 
|  | 3689 |  | 
| Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3690 | const vec2 rawXY = | 
|  | 3691 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, | 
|  | 3692 | motionArgs.pointerCoords[0].getXYValue()); | 
|  | 3693 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); | 
|  | 3694 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3695 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3696 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); | 
| Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 3697 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3698 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); | 
|  | 3699 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); | 
|  | 3700 | } | 
|  | 3701 |  | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3702 | /** | 
|  | 3703 | * Ensure that separate calls to sign the same data are generating the same key. | 
|  | 3704 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance | 
|  | 3705 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky | 
|  | 3706 | * tests. | 
|  | 3707 | */ | 
|  | 3708 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { | 
|  | 3709 | KeyEvent event = getTestKeyEvent(); | 
|  | 3710 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); | 
|  | 3711 |  | 
|  | 3712 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); | 
|  | 3713 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); | 
|  | 3714 | ASSERT_EQ(hmac1, hmac2); | 
|  | 3715 | } | 
|  | 3716 |  | 
|  | 3717 | /** | 
|  | 3718 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. | 
|  | 3719 | */ | 
|  | 3720 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { | 
|  | 3721 | KeyEvent event = getTestKeyEvent(); | 
|  | 3722 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); | 
|  | 3723 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); | 
|  | 3724 |  | 
|  | 3725 | verifiedEvent.deviceId += 1; | 
|  | 3726 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 3727 |  | 
|  | 3728 | verifiedEvent.source += 1; | 
|  | 3729 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 3730 |  | 
|  | 3731 | verifiedEvent.eventTimeNanos += 1; | 
|  | 3732 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 3733 |  | 
|  | 3734 | verifiedEvent.displayId += 1; | 
|  | 3735 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 3736 |  | 
|  | 3737 | verifiedEvent.action += 1; | 
|  | 3738 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 3739 |  | 
|  | 3740 | verifiedEvent.downTimeNanos += 1; | 
|  | 3741 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 3742 |  | 
|  | 3743 | verifiedEvent.flags += 1; | 
|  | 3744 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 3745 |  | 
|  | 3746 | verifiedEvent.keyCode += 1; | 
|  | 3747 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 3748 |  | 
|  | 3749 | verifiedEvent.scanCode += 1; | 
|  | 3750 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 3751 |  | 
|  | 3752 | verifiedEvent.metaState += 1; | 
|  | 3753 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 3754 |  | 
|  | 3755 | verifiedEvent.repeatCount += 1; | 
|  | 3756 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 3757 | } | 
|  | 3758 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3759 | TEST_F(InputDispatcherTest, SetFocusedWindow) { | 
|  | 3760 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3761 | sp<FakeWindowHandle> windowTop = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3762 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3763 | sp<FakeWindowHandle> windowSecond = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3764 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3765 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3766 |  | 
|  | 3767 | // Top window is also focusable but is not granted focus. | 
|  | 3768 | windowTop->setFocusable(true); | 
|  | 3769 | windowSecond->setFocusable(true); | 
|  | 3770 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
|  | 3771 | setFocusedWindow(windowSecond); | 
|  | 3772 |  | 
|  | 3773 | windowSecond->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3774 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 3775 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3776 |  | 
|  | 3777 | // Focused window should receive event. | 
|  | 3778 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 3779 | windowTop->assertNoEvents(); | 
|  | 3780 | } | 
|  | 3781 |  | 
|  | 3782 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { | 
|  | 3783 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3784 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3785 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3786 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3787 |  | 
|  | 3788 | window->setFocusable(true); | 
|  | 3789 | // Release channel for window is no longer valid. | 
|  | 3790 | window->releaseChannel(); | 
|  | 3791 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3792 | setFocusedWindow(window); | 
|  | 3793 |  | 
|  | 3794 | // Test inject a key down, should timeout. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3795 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 3796 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3797 |  | 
|  | 3798 | // window channel is invalid, so it should not receive any input event. | 
|  | 3799 | window->assertNoEvents(); | 
|  | 3800 | } | 
|  | 3801 |  | 
|  | 3802 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { | 
|  | 3803 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3804 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3805 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3806 | window->setFocusable(false); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3807 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3808 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3809 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3810 | setFocusedWindow(window); | 
|  | 3811 |  | 
|  | 3812 | // Test inject a key down, should timeout. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3813 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 3814 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3815 |  | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3816 | // window is not focusable, so it should not receive any input event. | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3817 | window->assertNoEvents(); | 
|  | 3818 | } | 
|  | 3819 |  | 
|  | 3820 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { | 
|  | 3821 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3822 | sp<FakeWindowHandle> windowTop = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3823 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3824 | sp<FakeWindowHandle> windowSecond = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3825 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3826 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3827 |  | 
|  | 3828 | windowTop->setFocusable(true); | 
|  | 3829 | windowSecond->setFocusable(true); | 
|  | 3830 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
|  | 3831 | setFocusedWindow(windowTop); | 
|  | 3832 | windowTop->consumeFocusEvent(true); | 
|  | 3833 |  | 
|  | 3834 | setFocusedWindow(windowSecond, windowTop); | 
|  | 3835 | windowSecond->consumeFocusEvent(true); | 
|  | 3836 | windowTop->consumeFocusEvent(false); | 
|  | 3837 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3838 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 3839 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3840 |  | 
|  | 3841 | // Focused window should receive event. | 
|  | 3842 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 3843 | } | 
|  | 3844 |  | 
|  | 3845 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { | 
|  | 3846 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3847 | sp<FakeWindowHandle> windowTop = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3848 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3849 | sp<FakeWindowHandle> windowSecond = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3850 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3851 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3852 |  | 
|  | 3853 | windowTop->setFocusable(true); | 
|  | 3854 | windowSecond->setFocusable(true); | 
|  | 3855 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
|  | 3856 | setFocusedWindow(windowSecond, windowTop); | 
|  | 3857 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3858 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 3859 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3860 |  | 
|  | 3861 | // Event should be dropped. | 
|  | 3862 | windowTop->assertNoEvents(); | 
|  | 3863 | windowSecond->assertNoEvents(); | 
|  | 3864 | } | 
|  | 3865 |  | 
|  | 3866 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { | 
|  | 3867 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3868 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3869 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3870 | sp<FakeWindowHandle> previousFocusedWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3871 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", | 
|  | 3872 | ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3873 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3874 |  | 
|  | 3875 | window->setFocusable(true); | 
|  | 3876 | previousFocusedWindow->setFocusable(true); | 
|  | 3877 | window->setVisible(false); | 
|  | 3878 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); | 
|  | 3879 | setFocusedWindow(previousFocusedWindow); | 
|  | 3880 | previousFocusedWindow->consumeFocusEvent(true); | 
|  | 3881 |  | 
|  | 3882 | // Requesting focus on invisible window takes focus from currently focused window. | 
|  | 3883 | setFocusedWindow(window); | 
|  | 3884 | previousFocusedWindow->consumeFocusEvent(false); | 
|  | 3885 |  | 
|  | 3886 | // Injected key goes to pending queue. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3887 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3888 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3889 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3890 |  | 
|  | 3891 | // Window does not get focus event or key down. | 
|  | 3892 | window->assertNoEvents(); | 
|  | 3893 |  | 
|  | 3894 | // Window becomes visible. | 
|  | 3895 | window->setVisible(true); | 
|  | 3896 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3897 |  | 
|  | 3898 | // Window receives focus event. | 
|  | 3899 | window->consumeFocusEvent(true); | 
|  | 3900 | // Focused window receives key down. | 
|  | 3901 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 3902 | } | 
|  | 3903 |  | 
| Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 3904 | TEST_F(InputDispatcherTest, DisplayRemoved) { | 
|  | 3905 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3906 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3907 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 3908 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3909 |  | 
|  | 3910 | // window is granted focus. | 
|  | 3911 | window->setFocusable(true); | 
|  | 3912 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3913 | setFocusedWindow(window); | 
|  | 3914 | window->consumeFocusEvent(true); | 
|  | 3915 |  | 
|  | 3916 | // When a display is removed window loses focus. | 
|  | 3917 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); | 
|  | 3918 | window->consumeFocusEvent(false); | 
|  | 3919 | } | 
|  | 3920 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3921 | /** | 
|  | 3922 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, | 
|  | 3923 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top | 
|  | 3924 | * of the 'slipperyEnterWindow'. | 
|  | 3925 | * | 
|  | 3926 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such | 
|  | 3927 | * a way so that the touched location is no longer covered by the top window. | 
|  | 3928 | * | 
|  | 3929 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now | 
|  | 3930 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had | 
|  | 3931 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive | 
|  | 3932 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting | 
|  | 3933 | * with ACTION_DOWN). | 
|  | 3934 | * Thus, the touch has been transferred from the top window into the bottom window, because the top | 
|  | 3935 | * window moved itself away from the touched location and had Flag::SLIPPERY. | 
|  | 3936 | * | 
|  | 3937 | * Even though the top window moved away from the touched location, it is still obscuring the bottom | 
|  | 3938 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ | 
|  | 3939 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. | 
|  | 3940 | * | 
|  | 3941 | * In this test, we ensure that the event received by the bottom window has | 
|  | 3942 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. | 
|  | 3943 | */ | 
|  | 3944 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 3945 | constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1; | 
|  | 3946 | constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1; | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3947 |  | 
|  | 3948 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3949 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3950 |  | 
|  | 3951 | sp<FakeWindowHandle> slipperyExitWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3952 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3953 | slipperyExitWindow->setSlippery(true); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3954 | // Make sure this one overlaps the bottom window | 
|  | 3955 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); | 
|  | 3956 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom | 
|  | 3957 | // one. Windows with the same owner are not considered to be occluding each other. | 
|  | 3958 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); | 
|  | 3959 |  | 
|  | 3960 | sp<FakeWindowHandle> slipperyEnterWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3961 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3962 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 3963 |  | 
|  | 3964 | mDispatcher->setInputWindows( | 
|  | 3965 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); | 
|  | 3966 |  | 
|  | 3967 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions | 
|  | 3968 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3969 | ADISPLAY_ID_DEFAULT, {{50, 50}}); | 
|  | 3970 | mDispatcher->notifyMotion(&args); | 
|  | 3971 | slipperyExitWindow->consumeMotionDown(); | 
|  | 3972 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); | 
|  | 3973 | mDispatcher->setInputWindows( | 
|  | 3974 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); | 
|  | 3975 |  | 
|  | 3976 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3977 | ADISPLAY_ID_DEFAULT, {{51, 51}}); | 
|  | 3978 | mDispatcher->notifyMotion(&args); | 
|  | 3979 |  | 
|  | 3980 | slipperyExitWindow->consumeMotionCancel(); | 
|  | 3981 |  | 
|  | 3982 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, | 
|  | 3983 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); | 
|  | 3984 | } | 
|  | 3985 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3986 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { | 
|  | 3987 | protected: | 
|  | 3988 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms | 
|  | 3989 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000;   // 40 ms | 
|  | 3990 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3991 | std::shared_ptr<FakeApplicationHandle> mApp; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3992 | sp<FakeWindowHandle> mWindow; | 
|  | 3993 |  | 
|  | 3994 | virtual void SetUp() override { | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3995 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3996 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3997 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3998 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); | 
|  | 3999 | ASSERT_EQ(OK, mDispatcher->start()); | 
|  | 4000 |  | 
|  | 4001 | setUpWindow(); | 
|  | 4002 | } | 
|  | 4003 |  | 
|  | 4004 | void setUpWindow() { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4005 | mApp = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4006 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4007 |  | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4008 | mWindow->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4009 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4010 | setFocusedWindow(mWindow); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4011 | mWindow->consumeFocusEvent(true); | 
|  | 4012 | } | 
|  | 4013 |  | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4014 | void sendAndConsumeKeyDown(int32_t deviceId) { | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4015 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4016 | keyArgs.deviceId = deviceId; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4017 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event | 
|  | 4018 | mDispatcher->notifyKey(&keyArgs); | 
|  | 4019 |  | 
|  | 4020 | // Window should receive key down event. | 
|  | 4021 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 4022 | } | 
|  | 4023 |  | 
|  | 4024 | void expectKeyRepeatOnce(int32_t repeatCount) { | 
|  | 4025 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); | 
|  | 4026 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 4027 | ASSERT_NE(nullptr, repeatEvent); | 
|  | 4028 |  | 
|  | 4029 | uint32_t eventType = repeatEvent->getType(); | 
|  | 4030 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); | 
|  | 4031 |  | 
|  | 4032 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); | 
|  | 4033 | uint32_t eventAction = repeatKeyEvent->getAction(); | 
|  | 4034 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); | 
|  | 4035 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); | 
|  | 4036 | } | 
|  | 4037 |  | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4038 | void sendAndConsumeKeyUp(int32_t deviceId) { | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4039 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4040 | keyArgs.deviceId = deviceId; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4041 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event | 
|  | 4042 | mDispatcher->notifyKey(&keyArgs); | 
|  | 4043 |  | 
|  | 4044 | // Window should receive key down event. | 
|  | 4045 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, | 
|  | 4046 | 0 /*expectedFlags*/); | 
|  | 4047 | } | 
|  | 4048 | }; | 
|  | 4049 |  | 
|  | 4050 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4051 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 4052 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 4053 | expectKeyRepeatOnce(repeatCount); | 
|  | 4054 | } | 
|  | 4055 | } | 
|  | 4056 |  | 
|  | 4057 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { | 
|  | 4058 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 4059 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 4060 | expectKeyRepeatOnce(repeatCount); | 
|  | 4061 | } | 
|  | 4062 | sendAndConsumeKeyDown(2 /* deviceId */); | 
|  | 4063 | /* repeatCount will start from 1 for deviceId 2 */ | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4064 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 4065 | expectKeyRepeatOnce(repeatCount); | 
|  | 4066 | } | 
|  | 4067 | } | 
|  | 4068 |  | 
|  | 4069 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4070 | sendAndConsumeKeyDown(1 /* deviceId */); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4071 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4072 | sendAndConsumeKeyUp(1 /* deviceId */); | 
|  | 4073 | mWindow->assertNoEvents(); | 
|  | 4074 | } | 
|  | 4075 |  | 
|  | 4076 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { | 
|  | 4077 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 4078 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 4079 | sendAndConsumeKeyDown(2 /* deviceId */); | 
|  | 4080 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 4081 | // Stale key up from device 1. | 
|  | 4082 | sendAndConsumeKeyUp(1 /* deviceId */); | 
|  | 4083 | // Device 2 is still down, keep repeating | 
|  | 4084 | expectKeyRepeatOnce(2 /*repeatCount*/); | 
|  | 4085 | expectKeyRepeatOnce(3 /*repeatCount*/); | 
|  | 4086 | // Device 2 key up | 
|  | 4087 | sendAndConsumeKeyUp(2 /* deviceId */); | 
|  | 4088 | mWindow->assertNoEvents(); | 
|  | 4089 | } | 
|  | 4090 |  | 
|  | 4091 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { | 
|  | 4092 | sendAndConsumeKeyDown(1 /* deviceId */); | 
|  | 4093 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 4094 | sendAndConsumeKeyDown(2 /* deviceId */); | 
|  | 4095 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 4096 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. | 
|  | 4097 | sendAndConsumeKeyUp(2 /* deviceId */); | 
|  | 4098 | // Device 1 still holds key down, but the repeating was already stopped | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4099 | mWindow->assertNoEvents(); | 
|  | 4100 | } | 
|  | 4101 |  | 
| liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 4102 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { | 
|  | 4103 | sendAndConsumeKeyDown(DEVICE_ID); | 
|  | 4104 | expectKeyRepeatOnce(1 /*repeatCount*/); | 
|  | 4105 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); | 
|  | 4106 | mDispatcher->notifyDeviceReset(&args); | 
|  | 4107 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, | 
|  | 4108 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); | 
|  | 4109 | mWindow->assertNoEvents(); | 
|  | 4110 | } | 
|  | 4111 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4112 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4113 | sendAndConsumeKeyDown(1 /* deviceId */); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4114 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 4115 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 4116 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; | 
|  | 4117 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, | 
|  | 4118 | IdGenerator::getSource(repeatEvent->getId())); | 
|  | 4119 | } | 
|  | 4120 | } | 
|  | 4121 |  | 
|  | 4122 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4123 | sendAndConsumeKeyDown(1 /* deviceId */); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4124 |  | 
|  | 4125 | std::unordered_set<int32_t> idSet; | 
|  | 4126 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 4127 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 4128 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; | 
|  | 4129 | int32_t id = repeatEvent->getId(); | 
|  | 4130 | EXPECT_EQ(idSet.end(), idSet.find(id)); | 
|  | 4131 | idSet.insert(id); | 
|  | 4132 | } | 
|  | 4133 | } | 
|  | 4134 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4135 | /* Test InputDispatcher for MultiDisplay */ | 
|  | 4136 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { | 
|  | 4137 | public: | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4138 | virtual void SetUp() override { | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4139 | InputDispatcherTest::SetUp(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4140 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4141 | application1 = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4142 | windowInPrimary = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4143 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4144 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4145 | // Set focus window for primary display, but focused display would be second one. | 
|  | 4146 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4147 | windowInPrimary->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4148 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4149 | setFocusedWindow(windowInPrimary); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4150 | windowInPrimary->consumeFocusEvent(true); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4151 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4152 | application2 = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4153 | windowInSecondary = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4154 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4155 | // Set focus to second display window. | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4156 | // Set focus display to second one. | 
|  | 4157 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); | 
|  | 4158 | // Set focus window for second display. | 
|  | 4159 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4160 | windowInSecondary->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4161 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4162 | setFocusedWindow(windowInSecondary); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4163 | windowInSecondary->consumeFocusEvent(true); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4164 | } | 
|  | 4165 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4166 | virtual void TearDown() override { | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4167 | InputDispatcherTest::TearDown(); | 
|  | 4168 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4169 | application1.reset(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4170 | windowInPrimary.clear(); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4171 | application2.reset(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4172 | windowInSecondary.clear(); | 
|  | 4173 | } | 
|  | 4174 |  | 
|  | 4175 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4176 | std::shared_ptr<FakeApplicationHandle> application1; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4177 | sp<FakeWindowHandle> windowInPrimary; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4178 | std::shared_ptr<FakeApplicationHandle> application2; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4179 | sp<FakeWindowHandle> windowInSecondary; | 
|  | 4180 | }; | 
|  | 4181 |  | 
|  | 4182 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { | 
|  | 4183 | // Test touch down on primary display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4184 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 4185 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 4186 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4187 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4188 | windowInSecondary->assertNoEvents(); | 
|  | 4189 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4190 | // Test touch down on second display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4191 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 4192 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) | 
|  | 4193 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4194 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4195 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4196 | } | 
|  | 4197 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4198 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4199 | // Test inject a key down with display id specified. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4200 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 4201 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4202 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4203 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4204 | windowInSecondary->assertNoEvents(); | 
|  | 4205 |  | 
|  | 4206 | // Test inject a key down without display id specified. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4207 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4208 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4209 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4210 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4211 |  | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4212 | // Remove all windows in secondary display. | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4213 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4214 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4215 | // Old focus should receive a cancel event. | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4216 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, | 
|  | 4217 | AKEY_EVENT_FLAG_CANCELED); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4218 |  | 
|  | 4219 | // Test inject a key down, should timeout because of no target window. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4220 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4221 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4222 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4223 | windowInSecondary->consumeFocusEvent(false); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4224 | windowInSecondary->assertNoEvents(); | 
|  | 4225 | } | 
|  | 4226 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4227 | // Test per-display input monitors for motion event. | 
|  | 4228 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4229 | FakeMonitorReceiver monitorInPrimary = | 
|  | 4230 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
|  | 4231 | FakeMonitorReceiver monitorInSecondary = | 
|  | 4232 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4233 |  | 
|  | 4234 | // Test touch down on primary display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4235 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 4236 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 4237 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4238 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4239 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4240 | windowInSecondary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4241 | monitorInSecondary.assertNoEvents(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4242 |  | 
|  | 4243 | // Test touch down on second display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4244 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 4245 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) | 
|  | 4246 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4247 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4248 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4249 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4250 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4251 |  | 
|  | 4252 | // Test inject a non-pointer motion event. | 
|  | 4253 | // If specific a display, it will dispatch to the focused window of particular display, | 
|  | 4254 | // or it will dispatch to the focused window of focused display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4255 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 4256 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) | 
|  | 4257 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4258 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4259 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4260 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4261 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4262 | } | 
|  | 4263 |  | 
|  | 4264 | // Test per-display input monitors for key event. | 
|  | 4265 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4266 | // Input monitor per display. | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4267 | FakeMonitorReceiver monitorInPrimary = | 
|  | 4268 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
|  | 4269 | FakeMonitorReceiver monitorInSecondary = | 
|  | 4270 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4271 |  | 
|  | 4272 | // Test inject a key down. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4273 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 4274 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4275 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4276 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4277 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4278 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4279 | } | 
|  | 4280 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4281 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { | 
|  | 4282 | sp<FakeWindowHandle> secondWindowInPrimary = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4283 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4284 | secondWindowInPrimary->setFocusable(true); | 
|  | 4285 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); | 
|  | 4286 | setFocusedWindow(secondWindowInPrimary); | 
|  | 4287 | windowInPrimary->consumeFocusEvent(false); | 
|  | 4288 | secondWindowInPrimary->consumeFocusEvent(true); | 
|  | 4289 |  | 
|  | 4290 | // Test inject a key down. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4291 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
|  | 4292 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4293 | windowInPrimary->assertNoEvents(); | 
|  | 4294 | windowInSecondary->assertNoEvents(); | 
|  | 4295 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 4296 | } | 
|  | 4297 |  | 
| Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 4298 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { | 
|  | 4299 | FakeMonitorReceiver monitorInPrimary = | 
|  | 4300 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
|  | 4301 | FakeMonitorReceiver monitorInSecondary = | 
|  | 4302 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); | 
|  | 4303 |  | 
|  | 4304 | // Test touch down on primary display. | 
|  | 4305 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 4306 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 4307 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 4308 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 4309 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 4310 |  | 
|  | 4311 | // Test touch down on second display. | 
|  | 4312 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 4313 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) | 
|  | 4314 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 4315 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); | 
|  | 4316 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); | 
|  | 4317 |  | 
|  | 4318 | // Trigger cancel touch. | 
|  | 4319 | mDispatcher->cancelCurrentTouch(); | 
|  | 4320 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); | 
|  | 4321 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); | 
|  | 4322 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); | 
|  | 4323 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); | 
|  | 4324 |  | 
|  | 4325 | // Test inject a move motion event, no window/monitor should receive the event. | 
|  | 4326 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
|  | 4327 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4328 | ADISPLAY_ID_DEFAULT, {110, 200})) | 
|  | 4329 | << "Inject motion event should return InputEventInjectionResult::FAILED"; | 
|  | 4330 | windowInPrimary->assertNoEvents(); | 
|  | 4331 | monitorInPrimary.assertNoEvents(); | 
|  | 4332 |  | 
|  | 4333 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
|  | 4334 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4335 | SECOND_DISPLAY_ID, {110, 200})) | 
|  | 4336 | << "Inject motion event should return InputEventInjectionResult::FAILED"; | 
|  | 4337 | windowInSecondary->assertNoEvents(); | 
|  | 4338 | monitorInSecondary.assertNoEvents(); | 
|  | 4339 | } | 
|  | 4340 |  | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4341 | class InputFilterTest : public InputDispatcherTest { | 
|  | 4342 | protected: | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4343 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, | 
|  | 4344 | const ui::Transform& transform = ui::Transform()) { | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4345 | NotifyMotionArgs motionArgs; | 
|  | 4346 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4347 | motionArgs = | 
|  | 4348 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4349 | mDispatcher->notifyMotion(&motionArgs); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4350 | motionArgs = | 
|  | 4351 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4352 | mDispatcher->notifyMotion(&motionArgs); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4353 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4354 | if (expectToBeFiltered) { | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4355 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); | 
|  | 4356 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4357 | } else { | 
|  | 4358 | mFakePolicy->assertFilterInputEventWasNotCalled(); | 
|  | 4359 | } | 
|  | 4360 | } | 
|  | 4361 |  | 
|  | 4362 | void testNotifyKey(bool expectToBeFiltered) { | 
|  | 4363 | NotifyKeyArgs keyArgs; | 
|  | 4364 |  | 
|  | 4365 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); | 
|  | 4366 | mDispatcher->notifyKey(&keyArgs); | 
|  | 4367 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); | 
|  | 4368 | mDispatcher->notifyKey(&keyArgs); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4369 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4370 |  | 
|  | 4371 | if (expectToBeFiltered) { | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 4372 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4373 | } else { | 
|  | 4374 | mFakePolicy->assertFilterInputEventWasNotCalled(); | 
|  | 4375 | } | 
|  | 4376 | } | 
|  | 4377 | }; | 
|  | 4378 |  | 
|  | 4379 | // Test InputFilter for MotionEvent | 
|  | 4380 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { | 
|  | 4381 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. | 
|  | 4382 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); | 
|  | 4383 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); | 
|  | 4384 |  | 
|  | 4385 | // Enable InputFilter | 
|  | 4386 | mDispatcher->setInputFilterEnabled(true); | 
|  | 4387 | // Test touch on both primary and second display, and check if both events are filtered. | 
|  | 4388 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); | 
|  | 4389 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); | 
|  | 4390 |  | 
|  | 4391 | // Disable InputFilter | 
|  | 4392 | mDispatcher->setInputFilterEnabled(false); | 
|  | 4393 | // Test touch on both primary and second display, and check if both events aren't filtered. | 
|  | 4394 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); | 
|  | 4395 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); | 
|  | 4396 | } | 
|  | 4397 |  | 
|  | 4398 | // Test InputFilter for KeyEvent | 
|  | 4399 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { | 
|  | 4400 | // Since the InputFilter is disabled by default, check if key event aren't filtered. | 
|  | 4401 | testNotifyKey(/*expectToBeFiltered*/ false); | 
|  | 4402 |  | 
|  | 4403 | // Enable InputFilter | 
|  | 4404 | mDispatcher->setInputFilterEnabled(true); | 
|  | 4405 | // Send a key event, and check if it is filtered. | 
|  | 4406 | testNotifyKey(/*expectToBeFiltered*/ true); | 
|  | 4407 |  | 
|  | 4408 | // Disable InputFilter | 
|  | 4409 | mDispatcher->setInputFilterEnabled(false); | 
|  | 4410 | // Send a key event, and check if it isn't filtered. | 
|  | 4411 | testNotifyKey(/*expectToBeFiltered*/ false); | 
|  | 4412 | } | 
|  | 4413 |  | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4414 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the | 
|  | 4415 | // logical display coordinate space. | 
|  | 4416 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { | 
|  | 4417 | ui::Transform firstDisplayTransform; | 
|  | 4418 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); | 
|  | 4419 | ui::Transform secondDisplayTransform; | 
|  | 4420 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); | 
|  | 4421 |  | 
|  | 4422 | std::vector<gui::DisplayInfo> displayInfos(2); | 
|  | 4423 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; | 
|  | 4424 | displayInfos[0].transform = firstDisplayTransform; | 
|  | 4425 | displayInfos[1].displayId = SECOND_DISPLAY_ID; | 
|  | 4426 | displayInfos[1].transform = secondDisplayTransform; | 
|  | 4427 |  | 
|  | 4428 | mDispatcher->onWindowInfosChanged({}, displayInfos); | 
|  | 4429 |  | 
|  | 4430 | // Enable InputFilter | 
|  | 4431 | mDispatcher->setInputFilterEnabled(true); | 
|  | 4432 |  | 
|  | 4433 | // Ensure the correct transforms are used for the displays. | 
|  | 4434 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); | 
|  | 4435 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); | 
|  | 4436 | } | 
|  | 4437 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4438 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { | 
|  | 4439 | protected: | 
|  | 4440 | virtual void SetUp() override { | 
|  | 4441 | InputDispatcherTest::SetUp(); | 
|  | 4442 |  | 
|  | 4443 | /** | 
|  | 4444 | * We don't need to enable input filter to test the injected event policy, but we enabled it | 
|  | 4445 | * here to make the tests more realistic, since this policy only matters when inputfilter is | 
|  | 4446 | * on. | 
|  | 4447 | */ | 
|  | 4448 | mDispatcher->setInputFilterEnabled(true); | 
|  | 4449 |  | 
|  | 4450 | std::shared_ptr<InputApplicationHandle> application = | 
|  | 4451 | std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4452 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", | 
|  | 4453 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4454 |  | 
|  | 4455 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 4456 | mWindow->setFocusable(true); | 
|  | 4457 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 4458 | setFocusedWindow(mWindow); | 
|  | 4459 | mWindow->consumeFocusEvent(true); | 
|  | 4460 | } | 
|  | 4461 |  | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4462 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, | 
|  | 4463 | int32_t flags) { | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4464 | KeyEvent event; | 
|  | 4465 |  | 
|  | 4466 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 4467 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, | 
|  | 4468 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, | 
|  | 4469 | KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime); | 
|  | 4470 | const int32_t additionalPolicyFlags = | 
|  | 4471 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; | 
|  | 4472 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4473 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4474 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, | 
|  | 4475 | policyFlags | additionalPolicyFlags)); | 
|  | 4476 |  | 
|  | 4477 | InputEvent* received = mWindow->consume(); | 
|  | 4478 | ASSERT_NE(nullptr, received); | 
|  | 4479 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4480 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY); | 
|  | 4481 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); | 
|  | 4482 | ASSERT_EQ(flags, keyEvent.getFlags()); | 
|  | 4483 | } | 
|  | 4484 |  | 
|  | 4485 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, | 
|  | 4486 | int32_t flags) { | 
|  | 4487 | MotionEvent event; | 
|  | 4488 | PointerProperties pointerProperties[1]; | 
|  | 4489 | PointerCoords pointerCoords[1]; | 
|  | 4490 | pointerProperties[0].clear(); | 
|  | 4491 | pointerProperties[0].id = 0; | 
|  | 4492 | pointerCoords[0].clear(); | 
|  | 4493 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); | 
|  | 4494 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); | 
|  | 4495 |  | 
|  | 4496 | ui::Transform identityTransform; | 
|  | 4497 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 4498 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4499 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, | 
|  | 4500 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, | 
|  | 4501 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4502 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 4503 | eventTime, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4504 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
|  | 4505 |  | 
|  | 4506 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; | 
|  | 4507 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4508 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4509 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, | 
|  | 4510 | policyFlags | additionalPolicyFlags)); | 
|  | 4511 |  | 
|  | 4512 | InputEvent* received = mWindow->consume(); | 
|  | 4513 | ASSERT_NE(nullptr, received); | 
|  | 4514 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); | 
|  | 4515 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION); | 
|  | 4516 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); | 
|  | 4517 | ASSERT_EQ(flags, motionEvent.getFlags()); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4518 | } | 
|  | 4519 |  | 
|  | 4520 | private: | 
|  | 4521 | sp<FakeWindowHandle> mWindow; | 
|  | 4522 | }; | 
|  | 4523 |  | 
|  | 4524 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4525 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input | 
|  | 4526 | // filter. Without it, the event will no different from a regularly injected event, and the | 
|  | 4527 | // injected device id will be overwritten. | 
|  | 4528 | testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, | 
|  | 4529 | 0 /*flags*/); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4530 | } | 
|  | 4531 |  | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4532 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4533 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4534 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, | 
|  | 4535 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); | 
|  | 4536 | } | 
|  | 4537 |  | 
|  | 4538 | TEST_F(InputFilterInjectionPolicyTest, | 
|  | 4539 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { | 
|  | 4540 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, | 
|  | 4541 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, | 
|  | 4542 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4543 | } | 
|  | 4544 |  | 
|  | 4545 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { | 
|  | 4546 | testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4547 | VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4548 | } | 
|  | 4549 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4550 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4551 | virtual void SetUp() override { | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4552 | InputDispatcherTest::SetUp(); | 
|  | 4553 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4554 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 4555 | std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4556 | mUnfocusedWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4557 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4558 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4559 |  | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4560 | mFocusedWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4561 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4562 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4563 |  | 
|  | 4564 | // Set focused application. | 
|  | 4565 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4566 | mFocusedWindow->setFocusable(true); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4567 |  | 
|  | 4568 | // Expect one focus window exist in display. | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4569 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4570 | setFocusedWindow(mFocusedWindow); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4571 | mFocusedWindow->consumeFocusEvent(true); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4572 | } | 
|  | 4573 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4574 | virtual void TearDown() override { | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4575 | InputDispatcherTest::TearDown(); | 
|  | 4576 |  | 
|  | 4577 | mUnfocusedWindow.clear(); | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4578 | mFocusedWindow.clear(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4579 | } | 
|  | 4580 |  | 
|  | 4581 | protected: | 
|  | 4582 | sp<FakeWindowHandle> mUnfocusedWindow; | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4583 | sp<FakeWindowHandle> mFocusedWindow; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4584 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4585 | }; | 
|  | 4586 |  | 
|  | 4587 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action | 
|  | 4588 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received | 
|  | 4589 | // the onPointerDownOutsideFocus callback. | 
|  | 4590 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4591 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4592 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4593 | {20, 20})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4594 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4595 | mUnfocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4596 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4597 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4598 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); | 
|  | 4599 | } | 
|  | 4600 |  | 
|  | 4601 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action | 
|  | 4602 | // DOWN on the window that doesn't have focus. Ensure no window received the | 
|  | 4603 | // onPointerDownOutsideFocus callback. | 
|  | 4604 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4605 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4606 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4607 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4608 | mFocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4609 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4610 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4611 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4612 | } | 
|  | 4613 |  | 
|  | 4614 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't | 
|  | 4615 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. | 
|  | 4616 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4617 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 4618 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4619 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4620 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4621 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4622 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4623 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4624 | } | 
|  | 4625 |  | 
|  | 4626 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action | 
|  | 4627 | // DOWN on the window that already has focus. Ensure no window received the | 
|  | 4628 | // onPointerDownOutsideFocus callback. | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4629 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4630 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4631 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4632 | FOCUSED_WINDOW_TOUCH_POINT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4633 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4634 | mFocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4635 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4636 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4637 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4638 | } | 
|  | 4639 |  | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 4640 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag | 
|  | 4641 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. | 
|  | 4642 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { | 
|  | 4643 | const MotionEvent event = | 
|  | 4644 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 4645 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 4646 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20)) | 
|  | 4647 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) | 
|  | 4648 | .build(); | 
|  | 4649 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) | 
|  | 4650 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 4651 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); | 
|  | 4652 |  | 
|  | 4653 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4654 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
|  | 4655 | // Ensure that the unfocused window did not receive any FOCUS events. | 
|  | 4656 | mUnfocusedWindow->assertNoEvents(); | 
|  | 4657 | } | 
|  | 4658 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4659 | // These tests ensures we can send touch events to a single client when there are multiple input | 
|  | 4660 | // windows that point to the same client token. | 
|  | 4661 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { | 
|  | 4662 | virtual void SetUp() override { | 
|  | 4663 | InputDispatcherTest::SetUp(); | 
|  | 4664 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4665 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 4666 | std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4667 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", | 
|  | 4668 | ADISPLAY_ID_DEFAULT); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4669 | mWindow1->setFrame(Rect(0, 0, 100, 100)); | 
|  | 4670 |  | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4671 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", | 
|  | 4672 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4673 | mWindow2->setFrame(Rect(100, 100, 200, 200)); | 
|  | 4674 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4675 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4676 | } | 
|  | 4677 |  | 
|  | 4678 | protected: | 
|  | 4679 | sp<FakeWindowHandle> mWindow1; | 
|  | 4680 | sp<FakeWindowHandle> mWindow2; | 
|  | 4681 |  | 
|  | 4682 | // 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] | 4683 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4684 | vec2 vals = windowInfo->transform.transform(point.x, point.y); | 
|  | 4685 | return {vals.x, vals.y}; | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4686 | } | 
|  | 4687 |  | 
|  | 4688 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, | 
|  | 4689 | const std::vector<PointF>& points) { | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4690 | const std::string name = window->getName(); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4691 | InputEvent* event = window->consume(); | 
|  | 4692 |  | 
|  | 4693 | ASSERT_NE(nullptr, event) << name.c_str() | 
|  | 4694 | << ": consumer should have returned non-NULL event."; | 
|  | 4695 |  | 
|  | 4696 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) | 
|  | 4697 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) | 
|  | 4698 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; | 
|  | 4699 |  | 
|  | 4700 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4701 | assertMotionAction(expectedAction, motionEvent.getAction()); | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 4702 | ASSERT_EQ(points.size(), motionEvent.getPointerCount()); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4703 |  | 
|  | 4704 | for (size_t i = 0; i < points.size(); i++) { | 
|  | 4705 | float expectedX = points[i].x; | 
|  | 4706 | float expectedY = points[i].y; | 
|  | 4707 |  | 
|  | 4708 | EXPECT_EQ(expectedX, motionEvent.getX(i)) | 
|  | 4709 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() | 
|  | 4710 | << ", got " << motionEvent.getX(i); | 
|  | 4711 | EXPECT_EQ(expectedY, motionEvent.getY(i)) | 
|  | 4712 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() | 
|  | 4713 | << ", got " << motionEvent.getY(i); | 
|  | 4714 | } | 
|  | 4715 | } | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4716 |  | 
| Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 4717 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4718 | std::vector<PointF> expectedPoints) { | 
|  | 4719 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4720 | ADISPLAY_ID_DEFAULT, touchedPoints); | 
|  | 4721 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 4722 |  | 
|  | 4723 | // Always consume from window1 since it's the window that has the InputReceiver | 
|  | 4724 | consumeMotionEvent(mWindow1, action, expectedPoints); | 
|  | 4725 | } | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4726 | }; | 
|  | 4727 |  | 
|  | 4728 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { | 
|  | 4729 | // Touch Window 1 | 
|  | 4730 | PointF touchedPoint = {10, 10}; | 
|  | 4731 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4732 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4733 |  | 
|  | 4734 | // Release touch on Window 1 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4735 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4736 |  | 
|  | 4737 | // Touch Window 2 | 
|  | 4738 | touchedPoint = {150, 150}; | 
|  | 4739 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4740 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4741 | } | 
|  | 4742 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4743 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { | 
|  | 4744 | // Set scale value for window2 | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4745 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 4746 |  | 
|  | 4747 | // Touch Window 1 | 
|  | 4748 | PointF touchedPoint = {10, 10}; | 
|  | 4749 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4750 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4751 | // Release touch on Window 1 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4752 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4753 |  | 
|  | 4754 | // Touch Window 2 | 
|  | 4755 | touchedPoint = {150, 150}; | 
|  | 4756 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4757 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
|  | 4758 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4759 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4760 | // Update the transform so rotation is set | 
|  | 4761 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 4762 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
|  | 4763 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4764 | } | 
|  | 4765 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4766 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4767 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 4768 |  | 
|  | 4769 | // Touch Window 1 | 
|  | 4770 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 4771 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4772 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4773 |  | 
|  | 4774 | // Touch Window 2 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4775 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 4776 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4777 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4778 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4779 | // Release Window 2 | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4780 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4781 | expectedPoints.pop_back(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4782 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4783 | // Update the transform so rotation is set for Window 2 | 
|  | 4784 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 4785 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4786 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4787 | } | 
|  | 4788 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4789 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4790 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 4791 |  | 
|  | 4792 | // Touch Window 1 | 
|  | 4793 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 4794 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4795 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4796 |  | 
|  | 4797 | // Touch Window 2 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4798 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 4799 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4800 |  | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4801 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4802 |  | 
|  | 4803 | // Move both windows | 
|  | 4804 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 4805 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 4806 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 4807 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4808 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4809 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4810 | // Release Window 2 | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4811 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4812 | expectedPoints.pop_back(); | 
|  | 4813 |  | 
|  | 4814 | // Touch Window 2 | 
|  | 4815 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 4816 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4817 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4818 |  | 
|  | 4819 | // Move both windows | 
|  | 4820 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 4821 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 4822 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 4823 |  | 
|  | 4824 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4825 | } | 
|  | 4826 |  | 
|  | 4827 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { | 
|  | 4828 | mWindow1->setWindowScale(0.5f, 0.5f); | 
|  | 4829 |  | 
|  | 4830 | // Touch Window 1 | 
|  | 4831 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 4832 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4833 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4834 |  | 
|  | 4835 | // Touch Window 2 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4836 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 4837 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4838 |  | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4839 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4840 |  | 
|  | 4841 | // Move both windows | 
|  | 4842 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 4843 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 4844 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 4845 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4846 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4847 | } | 
|  | 4848 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4849 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { | 
|  | 4850 | virtual void SetUp() override { | 
|  | 4851 | InputDispatcherTest::SetUp(); | 
|  | 4852 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4853 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4854 | mApplication->setDispatchingTimeout(20ms); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4855 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", | 
|  | 4856 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4857 | mWindow->setFrame(Rect(0, 0, 30, 30)); | 
| Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 4858 | mWindow->setDispatchingTimeout(30ms); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4859 | mWindow->setFocusable(true); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4860 |  | 
|  | 4861 | // Set focused application. | 
|  | 4862 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); | 
|  | 4863 |  | 
|  | 4864 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4865 | setFocusedWindow(mWindow); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4866 | mWindow->consumeFocusEvent(true); | 
|  | 4867 | } | 
|  | 4868 |  | 
|  | 4869 | virtual void TearDown() override { | 
|  | 4870 | InputDispatcherTest::TearDown(); | 
|  | 4871 | mWindow.clear(); | 
|  | 4872 | } | 
|  | 4873 |  | 
|  | 4874 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4875 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4876 | sp<FakeWindowHandle> mWindow; | 
|  | 4877 | static constexpr PointF WINDOW_LOCATION = {20, 20}; | 
|  | 4878 |  | 
|  | 4879 | void tapOnWindow() { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4880 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4881 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4882 | WINDOW_LOCATION)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4883 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4884 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4885 | WINDOW_LOCATION)); | 
|  | 4886 | } | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4887 |  | 
|  | 4888 | sp<FakeWindowHandle> addSpyWindow() { | 
|  | 4889 | sp<FakeWindowHandle> spy = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4890 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4891 | spy->setTrustedOverlay(true); | 
|  | 4892 | spy->setFocusable(false); | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4893 | spy->setSpy(true); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4894 | spy->setDispatchingTimeout(30ms); | 
|  | 4895 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); | 
|  | 4896 | return spy; | 
|  | 4897 | } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4898 | }; | 
|  | 4899 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4900 | // Send a tap and respond, which should not cause an ANR. | 
|  | 4901 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { | 
|  | 4902 | tapOnWindow(); | 
|  | 4903 | mWindow->consumeMotionDown(); | 
|  | 4904 | mWindow->consumeMotionUp(); | 
|  | 4905 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4906 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4907 | } | 
|  | 4908 |  | 
|  | 4909 | // Send a regular key and respond, which should not cause an ANR. | 
|  | 4910 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4911 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4912 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 4913 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4914 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4915 | } | 
|  | 4916 |  | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4917 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { | 
|  | 4918 | mWindow->setFocusable(false); | 
|  | 4919 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 4920 | mWindow->consumeFocusEvent(false); | 
|  | 4921 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4922 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4923 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4924 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, | 
|  | 4925 | false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4926 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4927 | // Key will not go to window because we have no focused window. | 
|  | 4928 | // The 'no focused window' ANR timer should start instead. | 
|  | 4929 |  | 
|  | 4930 | // Now, the focused application goes away. | 
|  | 4931 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); | 
|  | 4932 | // The key should get dropped and there should be no ANR. | 
|  | 4933 |  | 
|  | 4934 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4935 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4936 | } | 
|  | 4937 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4938 | // 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] | 4939 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. | 
|  | 4940 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4941 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4942 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4943 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4944 | WINDOW_LOCATION)); | 
|  | 4945 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4946 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN | 
|  | 4947 | ASSERT_TRUE(sequenceNum); | 
|  | 4948 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4949 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4950 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4951 | mWindow->finishEvent(*sequenceNum); | 
|  | 4952 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, | 
|  | 4953 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4954 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4955 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4956 | } | 
|  | 4957 |  | 
|  | 4958 | // Send a key to the app and have the app not respond right away. | 
|  | 4959 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { | 
|  | 4960 | // Inject a key, and don't respond - expect that ANR is called. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4961 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4962 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); | 
|  | 4963 | ASSERT_TRUE(sequenceNum); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4964 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 4965 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4966 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4967 | } | 
|  | 4968 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4969 | // We have a focused application, but no focused window | 
|  | 4970 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4971 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4972 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 4973 | mWindow->consumeFocusEvent(false); | 
|  | 4974 |  | 
|  | 4975 | // taps on the window work as normal | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4976 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4977 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4978 | WINDOW_LOCATION)); | 
|  | 4979 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); | 
|  | 4980 | mDispatcher->waitForIdle(); | 
|  | 4981 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 4982 |  | 
|  | 4983 | // Once a focused event arrives, we get an ANR for this application | 
|  | 4984 | // We specify the injection timeout to be smaller than the application timeout, to ensure that | 
|  | 4985 | // injection times out (instead of failing). | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4986 | const InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4987 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4988 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4989 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4990 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 4991 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4992 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 4993 | } | 
|  | 4994 |  | 
| Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 4995 | /** | 
|  | 4996 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, | 
|  | 4997 | * there will not be an ANR. | 
|  | 4998 | */ | 
|  | 4999 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { | 
|  | 5000 | mWindow->setFocusable(false); | 
|  | 5001 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 5002 | mWindow->consumeFocusEvent(false); | 
|  | 5003 |  | 
|  | 5004 | KeyEvent event; | 
|  | 5005 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - | 
|  | 5006 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); | 
|  | 5007 |  | 
|  | 5008 | // Define a valid key down event that is stale (too old). | 
|  | 5009 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 5010 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, | 
|  | 5011 | AMETA_NONE, 1 /*repeatCount*/, eventTime, eventTime); | 
|  | 5012 |  | 
|  | 5013 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; | 
|  | 5014 |  | 
|  | 5015 | InputEventInjectionResult result = | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 5016 | mDispatcher->injectInputEvent(&event, {} /* targetUid */, | 
| Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 5017 | InputEventInjectionSync::WAIT_FOR_RESULT, | 
|  | 5018 | INJECT_EVENT_TIMEOUT, policyFlags); | 
|  | 5019 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) | 
|  | 5020 | << "Injection should fail because the event is stale"; | 
|  | 5021 |  | 
|  | 5022 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 5023 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 5024 | mWindow->assertNoEvents(); | 
|  | 5025 | } | 
|  | 5026 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5027 | // We have a focused application, but no focused window | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5028 | // Make sure that we don't notify policy twice about the same ANR. | 
|  | 5029 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5030 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5031 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 5032 | mWindow->consumeFocusEvent(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5033 |  | 
|  | 5034 | // Once a focused event arrives, we get an ANR for this application | 
|  | 5035 | // We specify the injection timeout to be smaller than the application timeout, to ensure that | 
|  | 5036 | // injection times out (instead of failing). | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5037 | const InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5038 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5039 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5040 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5041 | const std::chrono::duration appTimeout = | 
|  | 5042 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
|  | 5043 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5044 |  | 
| Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5045 | std::this_thread::sleep_for(appTimeout); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5046 | // ANR should not be raised again. It is up to policy to do that if it desires. | 
|  | 5047 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5048 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5049 | // If we now get a focused window, the ANR should stop, but the policy handles that via | 
|  | 5050 | // '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] | 5051 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 5052 | } | 
|  | 5053 |  | 
|  | 5054 | // We have a focused application, but no focused window | 
|  | 5055 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5056 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5057 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 5058 | mWindow->consumeFocusEvent(false); | 
|  | 5059 |  | 
|  | 5060 | // Once a focused event arrives, we get an ANR for this application | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5061 | const InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5062 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5063 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); | 
|  | 5064 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5065 |  | 
| Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5066 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
|  | 5067 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5068 |  | 
|  | 5069 | // Future focused events get dropped right away | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5070 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5071 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 5072 | mWindow->assertNoEvents(); | 
|  | 5073 | } | 
|  | 5074 |  | 
|  | 5075 | /** | 
|  | 5076 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the | 
|  | 5077 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. | 
|  | 5078 | * If we process 1 of the events, but ANR on the second event with the same timestamp, | 
|  | 5079 | * the ANR mechanism should still work. | 
|  | 5080 | * | 
|  | 5081 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the | 
|  | 5082 | * DOWN event, while not responding on the second one. | 
|  | 5083 | */ | 
|  | 5084 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { | 
|  | 5085 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 5086 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5087 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, | 
|  | 5088 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 5089 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5090 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5091 |  | 
|  | 5092 | // Now send ACTION_UP, with identical timestamp | 
|  | 5093 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5094 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, | 
|  | 5095 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 5096 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5097 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5098 |  | 
|  | 5099 | // We have now sent down and up. Let's consume first event and then ANR on the second. | 
|  | 5100 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5101 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5102 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5103 | } | 
|  | 5104 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5105 | // A spy window can receive an ANR | 
|  | 5106 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { | 
|  | 5107 | sp<FakeWindowHandle> spy = addSpyWindow(); | 
|  | 5108 |  | 
|  | 5109 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5110 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5111 | WINDOW_LOCATION)); | 
|  | 5112 | mWindow->consumeMotionDown(); | 
|  | 5113 |  | 
|  | 5114 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN | 
|  | 5115 | ASSERT_TRUE(sequenceNum); | 
|  | 5116 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5117 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5118 |  | 
|  | 5119 | spy->finishEvent(*sequenceNum); | 
|  | 5120 | spy->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, | 
|  | 5121 | 0 /*flags*/); | 
|  | 5122 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5123 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5124 | } | 
|  | 5125 |  | 
|  | 5126 | // 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] | 5127 | // new motion events | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5128 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { | 
|  | 5129 | sp<FakeWindowHandle> spy = addSpyWindow(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5130 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5131 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5132 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5133 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5134 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5135 |  | 
|  | 5136 | // Stuck on the ACTION_UP | 
|  | 5137 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5138 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5139 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5140 | // 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] | 5141 | tapOnWindow(); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5142 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5143 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5144 |  | 
|  | 5145 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion | 
|  | 5146 | mDispatcher->waitForIdle(); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5147 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5148 | mWindow->assertNoEvents(); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5149 | spy->assertNoEvents(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5150 | } | 
|  | 5151 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5152 | // 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] | 5153 | // new motion events | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5154 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { | 
|  | 5155 | sp<FakeWindowHandle> spy = addSpyWindow(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5156 |  | 
|  | 5157 | tapOnWindow(); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5158 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5159 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5160 |  | 
|  | 5161 | mWindow->consumeMotionDown(); | 
|  | 5162 | // Stuck on the ACTION_UP | 
|  | 5163 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5164 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5165 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5166 | // 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] | 5167 | tapOnWindow(); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5168 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5169 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5170 |  | 
|  | 5171 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion | 
|  | 5172 | mDispatcher->waitForIdle(); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5173 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5174 | mWindow->assertNoEvents(); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5175 | spy->assertNoEvents(); | 
|  | 5176 | } | 
|  | 5177 |  | 
|  | 5178 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { | 
|  | 5179 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); | 
|  | 5180 |  | 
|  | 5181 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
|  | 5182 |  | 
|  | 5183 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5184 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5185 | WINDOW_LOCATION)); | 
|  | 5186 |  | 
|  | 5187 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5188 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); | 
|  | 5189 | ASSERT_TRUE(consumeSeq); | 
|  | 5190 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5191 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5192 |  | 
|  | 5193 | monitor.finishEvent(*consumeSeq); | 
|  | 5194 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); | 
|  | 5195 |  | 
|  | 5196 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5197 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5198 | } | 
|  | 5199 |  | 
|  | 5200 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to | 
|  | 5201 | // process events, you don't get an anr. When the window later becomes unresponsive again, you | 
|  | 5202 | // get an ANR again. | 
|  | 5203 | // 1. tap -> block on ACTION_UP -> receive ANR | 
|  | 5204 | // 2. consume all pending events (= queue becomes healthy again) | 
|  | 5205 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time | 
|  | 5206 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { | 
|  | 5207 | tapOnWindow(); | 
|  | 5208 |  | 
|  | 5209 | mWindow->consumeMotionDown(); | 
|  | 5210 | // Block on ACTION_UP | 
|  | 5211 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5212 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5213 | mWindow->consumeMotionUp(); // Now the connection should be healthy again | 
|  | 5214 | mDispatcher->waitForIdle(); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5215 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5216 | mWindow->assertNoEvents(); | 
|  | 5217 |  | 
|  | 5218 | tapOnWindow(); | 
|  | 5219 | mWindow->consumeMotionDown(); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5220 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5221 | mWindow->consumeMotionUp(); | 
|  | 5222 |  | 
|  | 5223 | mDispatcher->waitForIdle(); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5224 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5225 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5226 | mWindow->assertNoEvents(); | 
|  | 5227 | } | 
|  | 5228 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5229 | // If a connection remains unresponsive for a while, make sure policy is only notified once about | 
|  | 5230 | // it. | 
|  | 5231 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5232 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5233 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5234 | WINDOW_LOCATION)); | 
|  | 5235 |  | 
|  | 5236 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5237 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5238 | std::this_thread::sleep_for(windowTimeout); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5239 | // 'notifyConnectionUnresponsive' should only be called once per connection | 
|  | 5240 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 5241 | // 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] | 5242 | mWindow->consumeMotionDown(); | 
|  | 5243 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, | 
|  | 5244 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 5245 | mWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5246 | mDispatcher->waitForIdle(); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5247 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5248 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5249 | } | 
|  | 5250 |  | 
|  | 5251 | /** | 
|  | 5252 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 5253 | * not to to the focused window until the motion is processed. | 
|  | 5254 | * | 
|  | 5255 | * Warning!!! | 
|  | 5256 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT | 
|  | 5257 | * and the injection timeout that we specify when injecting the key. | 
|  | 5258 | * We must have the injection timeout (10ms) be smaller than | 
|  | 5259 | *  KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). | 
|  | 5260 | * | 
|  | 5261 | * If that value changes, this test should also change. | 
|  | 5262 | */ | 
|  | 5263 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { | 
|  | 5264 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering | 
|  | 5265 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 5266 |  | 
|  | 5267 | tapOnWindow(); | 
|  | 5268 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); | 
|  | 5269 | ASSERT_TRUE(downSequenceNum); | 
|  | 5270 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); | 
|  | 5271 | ASSERT_TRUE(upSequenceNum); | 
|  | 5272 | // Don't finish the events yet, and send a key | 
|  | 5273 | // Injection will "succeed" because we will eventually give up and send the key to the focused | 
|  | 5274 | // window even if motions are still being processed. But because the injection timeout is short, | 
|  | 5275 | // we will receive INJECTION_TIMED_OUT as the result. | 
|  | 5276 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5277 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5278 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5279 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); | 
|  | 5280 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5281 | // Key will not be sent to the window, yet, because the window is still processing events | 
|  | 5282 | // and the key remains pending, waiting for the touch events to be processed | 
|  | 5283 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); | 
|  | 5284 | ASSERT_FALSE(keySequenceNum); | 
|  | 5285 |  | 
|  | 5286 | std::this_thread::sleep_for(500ms); | 
|  | 5287 | // if we wait long enough though, dispatcher will give up, and still send the key | 
|  | 5288 | // to the focused window, even though we have not yet finished the motion event | 
|  | 5289 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 5290 | mWindow->finishEvent(*downSequenceNum); | 
|  | 5291 | mWindow->finishEvent(*upSequenceNum); | 
|  | 5292 | } | 
|  | 5293 |  | 
|  | 5294 | /** | 
|  | 5295 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 5296 | * not go to the focused window until the motion is processed. | 
|  | 5297 | * If then a new motion comes in, then the pending key event should be going to the currently | 
|  | 5298 | * focused window right away. | 
|  | 5299 | */ | 
|  | 5300 | TEST_F(InputDispatcherSingleWindowAnr, | 
|  | 5301 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { | 
|  | 5302 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering | 
|  | 5303 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 5304 |  | 
|  | 5305 | tapOnWindow(); | 
|  | 5306 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); | 
|  | 5307 | ASSERT_TRUE(downSequenceNum); | 
|  | 5308 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); | 
|  | 5309 | ASSERT_TRUE(upSequenceNum); | 
|  | 5310 | // Don't finish the events yet, and send a key | 
|  | 5311 | // Injection is async, so it will succeed | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5312 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5313 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5314 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5315 | // At this point, key is still pending, and should not be sent to the application yet. | 
|  | 5316 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); | 
|  | 5317 | ASSERT_FALSE(keySequenceNum); | 
|  | 5318 |  | 
|  | 5319 | // Now tap down again. It should cause the pending key to go to the focused window right away. | 
|  | 5320 | tapOnWindow(); | 
|  | 5321 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd | 
|  | 5322 | // the other events yet. We can finish events in any order. | 
|  | 5323 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN | 
|  | 5324 | mWindow->finishEvent(*upSequenceNum);   // first tap's ACTION_UP | 
|  | 5325 | mWindow->consumeMotionDown(); | 
|  | 5326 | mWindow->consumeMotionUp(); | 
|  | 5327 | mWindow->assertNoEvents(); | 
|  | 5328 | } | 
|  | 5329 |  | 
|  | 5330 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { | 
|  | 5331 | virtual void SetUp() override { | 
|  | 5332 | InputDispatcherTest::SetUp(); | 
|  | 5333 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5334 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5335 | mApplication->setDispatchingTimeout(10ms); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5336 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", | 
|  | 5337 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5338 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5339 | // 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] | 5340 | mUnfocusedWindow->setWatchOutsideTouch(true); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5341 |  | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5342 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", | 
|  | 5343 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 5344 | mFocusedWindow->setDispatchingTimeout(30ms); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5345 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5346 |  | 
|  | 5347 | // Set focused application. | 
|  | 5348 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5349 | mFocusedWindow->setFocusable(true); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5350 |  | 
|  | 5351 | // Expect one focus window exist in display. | 
|  | 5352 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5353 | setFocusedWindow(mFocusedWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5354 | mFocusedWindow->consumeFocusEvent(true); | 
|  | 5355 | } | 
|  | 5356 |  | 
|  | 5357 | virtual void TearDown() override { | 
|  | 5358 | InputDispatcherTest::TearDown(); | 
|  | 5359 |  | 
|  | 5360 | mUnfocusedWindow.clear(); | 
|  | 5361 | mFocusedWindow.clear(); | 
|  | 5362 | } | 
|  | 5363 |  | 
|  | 5364 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5365 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5366 | sp<FakeWindowHandle> mUnfocusedWindow; | 
|  | 5367 | sp<FakeWindowHandle> mFocusedWindow; | 
|  | 5368 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; | 
|  | 5369 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; | 
|  | 5370 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; | 
|  | 5371 |  | 
|  | 5372 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } | 
|  | 5373 |  | 
|  | 5374 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } | 
|  | 5375 |  | 
|  | 5376 | private: | 
|  | 5377 | void tap(const PointF& location) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5378 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5379 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5380 | location)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5381 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5382 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5383 | location)); | 
|  | 5384 | } | 
|  | 5385 | }; | 
|  | 5386 |  | 
|  | 5387 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout | 
|  | 5388 | // should be ANR'd first. | 
|  | 5389 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { | 
| 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 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5392 | FOCUSED_WINDOW_LOCATION)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5393 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5394 | mFocusedWindow->consumeMotionDown(); | 
|  | 5395 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
|  | 5396 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 5397 | // We consumed all events, so no ANR | 
|  | 5398 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 5399 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 5400 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5401 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5402 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5403 | FOCUSED_WINDOW_LOCATION)); | 
|  | 5404 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 5405 | ASSERT_TRUE(unfocusedSequenceNum); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5406 |  | 
|  | 5407 | const std::chrono::duration timeout = | 
|  | 5408 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5409 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5410 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event | 
|  | 5411 | // sequence to make it consistent | 
|  | 5412 | mFocusedWindow->consumeMotionCancel(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5413 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5414 | mFocusedWindow->consumeMotionDown(); | 
|  | 5415 | // This cancel is generated because the connection was unresponsive | 
|  | 5416 | mFocusedWindow->consumeMotionCancel(); | 
|  | 5417 | mFocusedWindow->assertNoEvents(); | 
|  | 5418 | mUnfocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5419 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5420 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), | 
|  | 5421 | mFocusedWindow->getPid()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5422 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5423 | } | 
|  | 5424 |  | 
|  | 5425 | // If we have 2 windows with identical timeouts that are both unresponsive, | 
|  | 5426 | // it doesn't matter which order they should have ANR. | 
|  | 5427 | // But we should receive ANR for both. | 
|  | 5428 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { | 
|  | 5429 | // Set the timeout for unfocused window to match the focused window | 
|  | 5430 | mUnfocusedWindow->setDispatchingTimeout(10ms); | 
|  | 5431 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
|  | 5432 |  | 
|  | 5433 | tapOnFocusedWindow(); | 
|  | 5434 | // 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] | 5435 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; | 
|  | 5436 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); | 
|  | 5437 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5438 |  | 
|  | 5439 | // 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] | 5440 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || | 
|  | 5441 | mFocusedWindow->getToken() == anrConnectionToken2); | 
|  | 5442 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || | 
|  | 5443 | mUnfocusedWindow->getToken() == anrConnectionToken2); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5444 |  | 
|  | 5445 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 5446 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5447 |  | 
|  | 5448 | mFocusedWindow->consumeMotionDown(); | 
|  | 5449 | mFocusedWindow->consumeMotionUp(); | 
|  | 5450 | mUnfocusedWindow->consumeMotionOutside(); | 
|  | 5451 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5452 | sp<IBinder> responsiveToken1, responsiveToken2; | 
|  | 5453 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); | 
|  | 5454 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5455 |  | 
|  | 5456 | // Both applications should be marked as responsive, in any order | 
|  | 5457 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || | 
|  | 5458 | mFocusedWindow->getToken() == responsiveToken2); | 
|  | 5459 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || | 
|  | 5460 | mUnfocusedWindow->getToken() == responsiveToken2); | 
|  | 5461 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5462 | } | 
|  | 5463 |  | 
|  | 5464 | // If a window is already not responding, the second tap on the same window should be ignored. | 
|  | 5465 | // We should also log an error to account for the dropped event (not tested here). | 
|  | 5466 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. | 
|  | 5467 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { | 
|  | 5468 | tapOnFocusedWindow(); | 
|  | 5469 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
|  | 5470 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 5471 | // Receive the events, but don't respond | 
|  | 5472 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN | 
|  | 5473 | ASSERT_TRUE(downEventSequenceNum); | 
|  | 5474 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP | 
|  | 5475 | ASSERT_TRUE(upEventSequenceNum); | 
|  | 5476 | const std::chrono::duration timeout = | 
|  | 5477 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5478 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5479 |  | 
|  | 5480 | // Tap once again | 
|  | 5481 | // 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] | 5482 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5483 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5484 | FOCUSED_WINDOW_LOCATION)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5485 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5486 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5487 | FOCUSED_WINDOW_LOCATION)); | 
|  | 5488 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a | 
|  | 5489 | // valid touch target | 
|  | 5490 | mUnfocusedWindow->assertNoEvents(); | 
|  | 5491 |  | 
|  | 5492 | // Consume the first tap | 
|  | 5493 | mFocusedWindow->finishEvent(*downEventSequenceNum); | 
|  | 5494 | mFocusedWindow->finishEvent(*upEventSequenceNum); | 
|  | 5495 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 5496 | // The second tap did not go to the focused window | 
|  | 5497 | mFocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5498 | // Since all events are finished, connection should be deemed healthy again | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5499 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), | 
|  | 5500 | mFocusedWindow->getPid()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5501 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 5502 | } | 
|  | 5503 |  | 
|  | 5504 | // If you tap outside of all windows, there will not be ANR | 
|  | 5505 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5506 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5507 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5508 | LOCATION_OUTSIDE_ALL_WINDOWS)); | 
|  | 5509 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 5510 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 5511 | } | 
|  | 5512 |  | 
|  | 5513 | // Since the focused window is paused, tapping on it should not produce any events | 
|  | 5514 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { | 
|  | 5515 | mFocusedWindow->setPaused(true); | 
|  | 5516 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
|  | 5517 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5518 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5519 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5520 | FOCUSED_WINDOW_LOCATION)); | 
|  | 5521 |  | 
|  | 5522 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); | 
|  | 5523 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 5524 | // Should not ANR because the window is paused, and touches shouldn't go to it | 
|  | 5525 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 5526 |  | 
|  | 5527 | mFocusedWindow->assertNoEvents(); | 
|  | 5528 | mUnfocusedWindow->assertNoEvents(); | 
|  | 5529 | } | 
|  | 5530 |  | 
|  | 5531 | /** | 
|  | 5532 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 5533 | * not to to the focused window until the motion is processed. | 
|  | 5534 | * If a different window becomes focused at this time, the key should go to that window instead. | 
|  | 5535 | * | 
|  | 5536 | * Warning!!! | 
|  | 5537 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT | 
|  | 5538 | * and the injection timeout that we specify when injecting the key. | 
|  | 5539 | * We must have the injection timeout (10ms) be smaller than | 
|  | 5540 | *  KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). | 
|  | 5541 | * | 
|  | 5542 | * If that value changes, this test should also change. | 
|  | 5543 | */ | 
|  | 5544 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { | 
|  | 5545 | // Set a long ANR timeout to prevent it from triggering | 
|  | 5546 | mFocusedWindow->setDispatchingTimeout(2s); | 
|  | 5547 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 5548 |  | 
|  | 5549 | tapOnUnfocusedWindow(); | 
|  | 5550 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 5551 | ASSERT_TRUE(downSequenceNum); | 
|  | 5552 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 5553 | ASSERT_TRUE(upSequenceNum); | 
|  | 5554 | // Don't finish the events yet, and send a key | 
|  | 5555 | // Injection will succeed because we will eventually give up and send the key to the focused | 
|  | 5556 | // window even if motions are still being processed. | 
|  | 5557 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5558 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5559 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5560 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); | 
|  | 5561 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5562 | // Key will not be sent to the window, yet, because the window is still processing events | 
|  | 5563 | // and the key remains pending, waiting for the touch events to be processed | 
|  | 5564 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); | 
|  | 5565 | ASSERT_FALSE(keySequenceNum); | 
|  | 5566 |  | 
|  | 5567 | // 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] | 5568 | mFocusedWindow->setFocusable(false); | 
|  | 5569 | mUnfocusedWindow->setFocusable(true); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5570 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5571 | setFocusedWindow(mUnfocusedWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5572 |  | 
|  | 5573 | // Focus events should precede the key events | 
|  | 5574 | mUnfocusedWindow->consumeFocusEvent(true); | 
|  | 5575 | mFocusedWindow->consumeFocusEvent(false); | 
|  | 5576 |  | 
|  | 5577 | // Finish the tap events, which should unblock dispatcher | 
|  | 5578 | mUnfocusedWindow->finishEvent(*downSequenceNum); | 
|  | 5579 | mUnfocusedWindow->finishEvent(*upSequenceNum); | 
|  | 5580 |  | 
|  | 5581 | // Now that all queues are cleared and no backlog in the connections, the key event | 
|  | 5582 | // can finally go to the newly focused "mUnfocusedWindow". | 
|  | 5583 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 5584 | mFocusedWindow->assertNoEvents(); | 
|  | 5585 | mUnfocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5586 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5587 | } | 
|  | 5588 |  | 
|  | 5589 | // When the touch stream is split across 2 windows, and one of them does not respond, | 
|  | 5590 | // then ANR should be raised and the touch should be canceled for the unresponsive window. | 
|  | 5591 | // The other window should not be affected by that. | 
|  | 5592 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { | 
|  | 5593 | // Touch Window 1 | 
|  | 5594 | NotifyMotionArgs motionArgs = | 
|  | 5595 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5596 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); | 
|  | 5597 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 5598 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
|  | 5599 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); | 
|  | 5600 |  | 
|  | 5601 | // Touch Window 2 | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5602 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5603 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5604 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 5605 |  | 
|  | 5606 | const std::chrono::duration timeout = | 
|  | 5607 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5608 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5609 |  | 
|  | 5610 | mUnfocusedWindow->consumeMotionDown(); | 
|  | 5611 | mFocusedWindow->consumeMotionDown(); | 
|  | 5612 | // Focused window may or may not receive ACTION_MOVE | 
|  | 5613 | // But it should definitely receive ACTION_CANCEL due to the ANR | 
|  | 5614 | InputEvent* event; | 
|  | 5615 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); | 
|  | 5616 | ASSERT_TRUE(moveOrCancelSequenceNum); | 
|  | 5617 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); | 
|  | 5618 | ASSERT_NE(nullptr, event); | 
|  | 5619 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); | 
|  | 5620 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 5621 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { | 
|  | 5622 | mFocusedWindow->consumeMotionCancel(); | 
|  | 5623 | } else { | 
|  | 5624 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); | 
|  | 5625 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5626 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5627 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), | 
|  | 5628 | mFocusedWindow->getPid()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5629 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5630 | mUnfocusedWindow->assertNoEvents(); | 
|  | 5631 | mFocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5632 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5633 | } | 
|  | 5634 |  | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5635 | /** | 
|  | 5636 | * If we have no focused window, and a key comes in, we start the ANR timer. | 
|  | 5637 | * The focused application should add a focused window before the timer runs out to prevent ANR. | 
|  | 5638 | * | 
|  | 5639 | * If the user touches another application during this time, the key should be dropped. | 
|  | 5640 | * Next, if a new focused window comes in, without toggling the focused application, | 
|  | 5641 | * then no ANR should occur. | 
|  | 5642 | * | 
|  | 5643 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', | 
|  | 5644 | * but in some cases the policy may not update the focused application. | 
|  | 5645 | */ | 
|  | 5646 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { | 
|  | 5647 | std::shared_ptr<FakeApplicationHandle> focusedApplication = | 
|  | 5648 | std::make_shared<FakeApplicationHandle>(); | 
|  | 5649 | focusedApplication->setDispatchingTimeout(60ms); | 
|  | 5650 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); | 
|  | 5651 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. | 
|  | 5652 | mFocusedWindow->setFocusable(false); | 
|  | 5653 |  | 
|  | 5654 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 5655 | mFocusedWindow->consumeFocusEvent(false); | 
|  | 5656 |  | 
|  | 5657 | // Send a key. The ANR timer should start because there is no focused window. | 
|  | 5658 | // 'focusedApplication' will get blamed if this timer completes. | 
|  | 5659 | // 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] | 5660 | InputEventInjectionResult result = | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5661 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5662 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, | 
|  | 5663 | false /* allowKeyRepeat */); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5664 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5665 |  | 
|  | 5666 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, | 
|  | 5667 | // then the injected touches won't cause the focused event to get dropped. | 
|  | 5668 | // The dispatcher only checks for whether the queue should be pruned upon queueing. | 
|  | 5669 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would | 
|  | 5670 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. | 
|  | 5671 | // For this test, it means that the key would get delivered to the window once it becomes | 
|  | 5672 | // focused. | 
|  | 5673 | std::this_thread::sleep_for(10ms); | 
|  | 5674 |  | 
|  | 5675 | // Touch unfocused window. This should force the pending key to get dropped. | 
|  | 5676 | NotifyMotionArgs motionArgs = | 
|  | 5677 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5678 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); | 
|  | 5679 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 5680 |  | 
|  | 5681 | // We do not consume the motion right away, because that would require dispatcher to first | 
|  | 5682 | // process (== drop) the key event, and by that time, ANR will be raised. | 
|  | 5683 | // Set the focused window first. | 
|  | 5684 | mFocusedWindow->setFocusable(true); | 
|  | 5685 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 5686 | setFocusedWindow(mFocusedWindow); | 
|  | 5687 | mFocusedWindow->consumeFocusEvent(true); | 
|  | 5688 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs | 
|  | 5689 | // to another application. This could be a bug / behaviour in the policy. | 
|  | 5690 |  | 
|  | 5691 | mUnfocusedWindow->consumeMotionDown(); | 
|  | 5692 |  | 
|  | 5693 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 5694 | // Should not ANR because we actually have a focused window. It was just added too slowly. | 
|  | 5695 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); | 
|  | 5696 | } | 
|  | 5697 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5698 | // These tests ensure we cannot send touch events to a window that's positioned behind a window | 
|  | 5699 | // that has feature NO_INPUT_CHANNEL. | 
|  | 5700 | // Layout: | 
|  | 5701 | //   Top (closest to user) | 
|  | 5702 | //       mNoInputWindow (above all windows) | 
|  | 5703 | //       mBottomWindow | 
|  | 5704 | //   Bottom (furthest from user) | 
|  | 5705 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { | 
|  | 5706 | virtual void SetUp() override { | 
|  | 5707 | InputDispatcherTest::SetUp(); | 
|  | 5708 |  | 
|  | 5709 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5710 | mNoInputWindow = | 
|  | 5711 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, | 
|  | 5712 | "Window without input channel", ADISPLAY_ID_DEFAULT, | 
|  | 5713 | std::make_optional<sp<IBinder>>(nullptr) /*token*/); | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5714 | mNoInputWindow->setNoInputChannel(true); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5715 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 5716 | // It's perfectly valid for this window to not have an associated input channel | 
|  | 5717 |  | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5718 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", | 
|  | 5719 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5720 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 5721 |  | 
|  | 5722 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); | 
|  | 5723 | } | 
|  | 5724 |  | 
|  | 5725 | protected: | 
|  | 5726 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
|  | 5727 | sp<FakeWindowHandle> mNoInputWindow; | 
|  | 5728 | sp<FakeWindowHandle> mBottomWindow; | 
|  | 5729 | }; | 
|  | 5730 |  | 
|  | 5731 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { | 
|  | 5732 | PointF touchedPoint = {10, 10}; | 
|  | 5733 |  | 
|  | 5734 | NotifyMotionArgs motionArgs = | 
|  | 5735 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5736 | ADISPLAY_ID_DEFAULT, {touchedPoint}); | 
|  | 5737 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 5738 |  | 
|  | 5739 | mNoInputWindow->assertNoEvents(); | 
|  | 5740 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have | 
|  | 5741 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, | 
|  | 5742 | // and therefore should prevent mBottomWindow from receiving touches | 
|  | 5743 | mBottomWindow->assertNoEvents(); | 
|  | 5744 | } | 
|  | 5745 |  | 
|  | 5746 | /** | 
|  | 5747 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, | 
|  | 5748 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. | 
|  | 5749 | */ | 
|  | 5750 | TEST_F(InputDispatcherMultiWindowOcclusionTests, | 
|  | 5751 | NoInputChannelFeature_DropsTouchesWithValidChannel) { | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5752 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, | 
|  | 5753 | "Window with input channel and NO_INPUT_CHANNEL", | 
|  | 5754 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5755 |  | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5756 | mNoInputWindow->setNoInputChannel(true); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5757 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 5758 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); | 
|  | 5759 |  | 
|  | 5760 | PointF touchedPoint = {10, 10}; | 
|  | 5761 |  | 
|  | 5762 | NotifyMotionArgs motionArgs = | 
|  | 5763 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5764 | ADISPLAY_ID_DEFAULT, {touchedPoint}); | 
|  | 5765 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 5766 |  | 
|  | 5767 | mNoInputWindow->assertNoEvents(); | 
|  | 5768 | mBottomWindow->assertNoEvents(); | 
|  | 5769 | } | 
|  | 5770 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5771 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { | 
|  | 5772 | protected: | 
|  | 5773 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 5774 | sp<FakeWindowHandle> mWindow; | 
|  | 5775 | sp<FakeWindowHandle> mMirror; | 
|  | 5776 |  | 
|  | 5777 | virtual void SetUp() override { | 
|  | 5778 | InputDispatcherTest::SetUp(); | 
|  | 5779 | mApp = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5780 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 5781 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", | 
|  | 5782 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5783 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
|  | 5784 | mWindow->setFocusable(true); | 
|  | 5785 | mMirror->setFocusable(true); | 
|  | 5786 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 5787 | } | 
|  | 5788 | }; | 
|  | 5789 |  | 
|  | 5790 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { | 
|  | 5791 | // Request focus on a mirrored window | 
|  | 5792 | setFocusedWindow(mMirror); | 
|  | 5793 |  | 
|  | 5794 | // window gets focused | 
|  | 5795 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5796 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 5797 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5798 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 5799 | } | 
|  | 5800 |  | 
|  | 5801 | // A focused & mirrored window remains focused only if the window and its mirror are both | 
|  | 5802 | // focusable. | 
|  | 5803 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { | 
|  | 5804 | setFocusedWindow(mMirror); | 
|  | 5805 |  | 
|  | 5806 | // window gets focused | 
|  | 5807 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5808 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 5809 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5810 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5811 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 5812 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5813 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 5814 |  | 
|  | 5815 | mMirror->setFocusable(false); | 
|  | 5816 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 5817 |  | 
|  | 5818 | // window loses focus since one of the windows associated with the token in not focusable | 
|  | 5819 | mWindow->consumeFocusEvent(false); | 
|  | 5820 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5821 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 5822 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5823 | mWindow->assertNoEvents(); | 
|  | 5824 | } | 
|  | 5825 |  | 
|  | 5826 | // A focused & mirrored window remains focused until the window and its mirror both become | 
|  | 5827 | // invisible. | 
|  | 5828 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { | 
|  | 5829 | setFocusedWindow(mMirror); | 
|  | 5830 |  | 
|  | 5831 | // window gets focused | 
|  | 5832 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5833 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 5834 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5835 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5836 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 5837 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5838 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 5839 |  | 
|  | 5840 | mMirror->setVisible(false); | 
|  | 5841 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 5842 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5843 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 5844 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5845 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5846 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 5847 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5848 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 5849 |  | 
|  | 5850 | mWindow->setVisible(false); | 
|  | 5851 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 5852 |  | 
|  | 5853 | // window loses focus only after all windows associated with the token become invisible. | 
|  | 5854 | mWindow->consumeFocusEvent(false); | 
|  | 5855 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5856 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 5857 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5858 | mWindow->assertNoEvents(); | 
|  | 5859 | } | 
|  | 5860 |  | 
|  | 5861 | // A focused & mirrored window remains focused until both windows are removed. | 
|  | 5862 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { | 
|  | 5863 | setFocusedWindow(mMirror); | 
|  | 5864 |  | 
|  | 5865 | // window gets focused | 
|  | 5866 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5867 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 5868 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5869 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5870 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 5871 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5872 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 5873 |  | 
|  | 5874 | // single window is removed but the window token remains focused | 
|  | 5875 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); | 
|  | 5876 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5877 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 5878 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5879 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5880 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 5881 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5882 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 5883 |  | 
|  | 5884 | // Both windows are removed | 
|  | 5885 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); | 
|  | 5886 | mWindow->consumeFocusEvent(false); | 
|  | 5887 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5888 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 5889 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5890 | mWindow->assertNoEvents(); | 
|  | 5891 | } | 
|  | 5892 |  | 
|  | 5893 | // Focus request can be pending until one window becomes visible. | 
|  | 5894 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { | 
|  | 5895 | // Request focus on an invisible mirror. | 
|  | 5896 | mWindow->setVisible(false); | 
|  | 5897 | mMirror->setVisible(false); | 
|  | 5898 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 5899 | setFocusedWindow(mMirror); | 
|  | 5900 |  | 
|  | 5901 | // Injected key goes to pending queue. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5902 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5903 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5904 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5905 |  | 
|  | 5906 | mMirror->setVisible(true); | 
|  | 5907 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 5908 |  | 
|  | 5909 | // window gets focused | 
|  | 5910 | mWindow->consumeFocusEvent(true); | 
|  | 5911 | // window gets the pending key event | 
|  | 5912 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 5913 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5914 |  | 
|  | 5915 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { | 
|  | 5916 | protected: | 
|  | 5917 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 5918 | sp<FakeWindowHandle> mWindow; | 
|  | 5919 | sp<FakeWindowHandle> mSecondWindow; | 
|  | 5920 |  | 
|  | 5921 | void SetUp() override { | 
|  | 5922 | InputDispatcherTest::SetUp(); | 
|  | 5923 | mApp = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5924 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5925 | mWindow->setFocusable(true); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5926 | mSecondWindow = | 
|  | 5927 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5928 | mSecondWindow->setFocusable(true); | 
|  | 5929 |  | 
|  | 5930 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
|  | 5931 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); | 
|  | 5932 |  | 
|  | 5933 | setFocusedWindow(mWindow); | 
|  | 5934 | mWindow->consumeFocusEvent(true); | 
|  | 5935 | } | 
|  | 5936 |  | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5937 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { | 
|  | 5938 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5939 | mDispatcher->notifyPointerCaptureChanged(&args); | 
|  | 5940 | } | 
|  | 5941 |  | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5942 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, | 
|  | 5943 | bool enabled) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5944 | mDispatcher->requestPointerCapture(window->getToken(), enabled); | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5945 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); | 
|  | 5946 | notifyPointerCaptureChanged(request); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5947 | window->consumeCaptureEvent(enabled); | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5948 | return request; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5949 | } | 
|  | 5950 | }; | 
|  | 5951 |  | 
|  | 5952 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { | 
|  | 5953 | // Ensure that capture cannot be obtained for unfocused windows. | 
|  | 5954 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); | 
|  | 5955 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 5956 | mSecondWindow->assertNoEvents(); | 
|  | 5957 |  | 
|  | 5958 | // Ensure that capture can be enabled from the focus window. | 
|  | 5959 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 5960 |  | 
|  | 5961 | // Ensure that capture cannot be disabled from a window that does not have capture. | 
|  | 5962 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); | 
|  | 5963 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 5964 |  | 
|  | 5965 | // Ensure that capture can be disabled from the window with capture. | 
|  | 5966 | requestAndVerifyPointerCapture(mWindow, false); | 
|  | 5967 | } | 
|  | 5968 |  | 
|  | 5969 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5970 | auto request = requestAndVerifyPointerCapture(mWindow, true); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5971 |  | 
|  | 5972 | setFocusedWindow(mSecondWindow); | 
|  | 5973 |  | 
|  | 5974 | // Ensure that the capture disabled event was sent first. | 
|  | 5975 | mWindow->consumeCaptureEvent(false); | 
|  | 5976 | mWindow->consumeFocusEvent(false); | 
|  | 5977 | mSecondWindow->consumeFocusEvent(true); | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5978 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5979 |  | 
|  | 5980 | // 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] | 5981 | notifyPointerCaptureChanged({}); | 
|  | 5982 | notifyPointerCaptureChanged(request); | 
|  | 5983 | notifyPointerCaptureChanged({}); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5984 | mWindow->assertNoEvents(); | 
|  | 5985 | mSecondWindow->assertNoEvents(); | 
|  | 5986 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 5987 | } | 
|  | 5988 |  | 
|  | 5989 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5990 | auto request = requestAndVerifyPointerCapture(mWindow, true); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5991 |  | 
|  | 5992 | // InputReader unexpectedly disables and enables pointer capture. | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5993 | notifyPointerCaptureChanged({}); | 
|  | 5994 | notifyPointerCaptureChanged(request); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5995 |  | 
|  | 5996 | // Ensure that Pointer Capture is disabled. | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5997 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5998 | mWindow->consumeCaptureEvent(false); | 
|  | 5999 | mWindow->assertNoEvents(); | 
|  | 6000 | } | 
|  | 6001 |  | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6002 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { | 
|  | 6003 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 6004 |  | 
|  | 6005 | // The first window loses focus. | 
|  | 6006 | setFocusedWindow(mSecondWindow); | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6007 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6008 | mWindow->consumeCaptureEvent(false); | 
|  | 6009 |  | 
|  | 6010 | // Request Pointer Capture from the second window before the notification from InputReader | 
|  | 6011 | // arrives. | 
|  | 6012 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6013 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6014 |  | 
|  | 6015 | // InputReader notifies Pointer Capture was disabled (because of the focus change). | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6016 | notifyPointerCaptureChanged({}); | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6017 |  | 
|  | 6018 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6019 | notifyPointerCaptureChanged(request); | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6020 |  | 
|  | 6021 | mSecondWindow->consumeFocusEvent(true); | 
|  | 6022 | mSecondWindow->consumeCaptureEvent(true); | 
|  | 6023 | } | 
|  | 6024 |  | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6025 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { | 
|  | 6026 | // App repeatedly enables and disables capture. | 
|  | 6027 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); | 
|  | 6028 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); | 
|  | 6029 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); | 
|  | 6030 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
|  | 6031 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); | 
|  | 6032 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); | 
|  | 6033 |  | 
|  | 6034 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the | 
|  | 6035 | // first request is now stale, this should do nothing. | 
|  | 6036 | notifyPointerCaptureChanged(firstRequest); | 
|  | 6037 | mWindow->assertNoEvents(); | 
|  | 6038 |  | 
|  | 6039 | // InputReader notifies that the second request was enabled. | 
|  | 6040 | notifyPointerCaptureChanged(secondRequest); | 
|  | 6041 | mWindow->consumeCaptureEvent(true); | 
|  | 6042 | } | 
|  | 6043 |  | 
| Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 6044 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { | 
|  | 6045 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 6046 |  | 
|  | 6047 | // App toggles pointer capture off and on. | 
|  | 6048 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); | 
|  | 6049 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
|  | 6050 |  | 
|  | 6051 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); | 
|  | 6052 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); | 
|  | 6053 |  | 
|  | 6054 | // InputReader notifies that the latest "enable" request was processed, while skipping over the | 
|  | 6055 | // preceding "disable" request. | 
|  | 6056 | notifyPointerCaptureChanged(enableRequest); | 
|  | 6057 |  | 
|  | 6058 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive | 
|  | 6059 | // any notifications. | 
|  | 6060 | mWindow->assertNoEvents(); | 
|  | 6061 | } | 
|  | 6062 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6063 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { | 
|  | 6064 | protected: | 
|  | 6065 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6066 |  | 
|  | 6067 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; | 
|  | 6068 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); | 
|  | 6069 |  | 
|  | 6070 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; | 
|  | 6071 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); | 
|  | 6072 |  | 
|  | 6073 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold | 
|  | 6074 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; | 
|  | 6075 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); | 
|  | 6076 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < | 
|  | 6077 | MAXIMUM_OBSCURING_OPACITY); | 
|  | 6078 |  | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6079 | static const int32_t TOUCHED_APP_UID = 10001; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6080 | static const int32_t APP_B_UID = 10002; | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6081 | static const int32_t APP_C_UID = 10003; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6082 |  | 
|  | 6083 | sp<FakeWindowHandle> mTouchWindow; | 
|  | 6084 |  | 
|  | 6085 | virtual void SetUp() override { | 
|  | 6086 | InputDispatcherTest::SetUp(); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6087 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6088 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); | 
|  | 6089 | } | 
|  | 6090 |  | 
|  | 6091 | virtual void TearDown() override { | 
|  | 6092 | InputDispatcherTest::TearDown(); | 
|  | 6093 | mTouchWindow.clear(); | 
|  | 6094 | } | 
|  | 6095 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6096 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, | 
|  | 6097 | float alpha = 1.0f) { | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6098 | sp<FakeWindowHandle> window = getWindow(uid, name); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6099 | window->setTouchable(false); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6100 | window->setTouchOcclusionMode(mode); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6101 | window->setAlpha(alpha); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6102 | return window; | 
|  | 6103 | } | 
|  | 6104 |  | 
|  | 6105 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { | 
|  | 6106 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); | 
|  | 6107 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6108 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6109 | // Generate an arbitrary PID based on the UID | 
|  | 6110 | window->setOwnerInfo(1777 + (uid % 10000), uid); | 
|  | 6111 | return window; | 
|  | 6112 | } | 
|  | 6113 |  | 
|  | 6114 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { | 
|  | 6115 | NotifyMotionArgs args = | 
|  | 6116 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6117 | ADISPLAY_ID_DEFAULT, points); | 
|  | 6118 | mDispatcher->notifyMotion(&args); | 
|  | 6119 | } | 
|  | 6120 | }; | 
|  | 6121 |  | 
|  | 6122 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6123 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6124 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6125 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6126 |  | 
|  | 6127 | touch(); | 
|  | 6128 |  | 
|  | 6129 | mTouchWindow->assertNoEvents(); | 
|  | 6130 | } | 
|  | 6131 |  | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6132 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6133 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { | 
|  | 6134 | const sp<FakeWindowHandle>& w = | 
|  | 6135 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); | 
|  | 6136 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 6137 |  | 
|  | 6138 | touch(); | 
|  | 6139 |  | 
|  | 6140 | mTouchWindow->assertNoEvents(); | 
|  | 6141 | } | 
|  | 6142 |  | 
|  | 6143 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6144 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { | 
|  | 6145 | const sp<FakeWindowHandle>& w = | 
|  | 6146 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 6147 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 6148 |  | 
|  | 6149 | touch(); | 
|  | 6150 |  | 
|  | 6151 | w->assertNoEvents(); | 
|  | 6152 | } | 
|  | 6153 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6154 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6155 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); | 
|  | 6156 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6157 |  | 
|  | 6158 | touch(); | 
|  | 6159 |  | 
|  | 6160 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 6161 | } | 
|  | 6162 |  | 
|  | 6163 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6164 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6165 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6166 | w->setFrame(Rect(0, 0, 50, 50)); | 
|  | 6167 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6168 |  | 
|  | 6169 | touch({PointF{100, 100}}); | 
|  | 6170 |  | 
|  | 6171 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 6172 | } | 
|  | 6173 |  | 
|  | 6174 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6175 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6176 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6177 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 6178 |  | 
|  | 6179 | touch(); | 
|  | 6180 |  | 
|  | 6181 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 6182 | } | 
|  | 6183 |  | 
|  | 6184 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { | 
|  | 6185 | const sp<FakeWindowHandle>& w = | 
|  | 6186 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
|  | 6187 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6188 |  | 
|  | 6189 | touch(); | 
|  | 6190 |  | 
|  | 6191 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 6192 | } | 
|  | 6193 |  | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6194 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { | 
|  | 6195 | const sp<FakeWindowHandle>& w = | 
|  | 6196 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
|  | 6197 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 6198 |  | 
|  | 6199 | touch(); | 
|  | 6200 |  | 
|  | 6201 | w->assertNoEvents(); | 
|  | 6202 | } | 
|  | 6203 |  | 
|  | 6204 | /** | 
|  | 6205 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs | 
|  | 6206 | * inside) while letting them pass-through. Note that even though touch passes through the occluding | 
|  | 6207 | * window, the occluding window will still receive ACTION_OUTSIDE event. | 
|  | 6208 | */ | 
|  | 6209 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 6210 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { | 
|  | 6211 | const sp<FakeWindowHandle>& w = | 
|  | 6212 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6213 | w->setWatchOutsideTouch(true); | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6214 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 6215 |  | 
|  | 6216 | touch(); | 
|  | 6217 |  | 
|  | 6218 | w->consumeMotionOutside(); | 
|  | 6219 | } | 
|  | 6220 |  | 
|  | 6221 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { | 
|  | 6222 | const sp<FakeWindowHandle>& w = | 
|  | 6223 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6224 | w->setWatchOutsideTouch(true); | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6225 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 6226 |  | 
|  | 6227 | touch(); | 
|  | 6228 |  | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6229 | w->consumeMotionOutsideWithZeroedCoords(); | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6230 | } | 
|  | 6231 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6232 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6233 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6234 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 6235 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6236 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 6237 |  | 
|  | 6238 | touch(); | 
|  | 6239 |  | 
|  | 6240 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 6241 | } | 
|  | 6242 |  | 
|  | 6243 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { | 
|  | 6244 | const sp<FakeWindowHandle>& w = | 
|  | 6245 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 6246 | MAXIMUM_OBSCURING_OPACITY); | 
|  | 6247 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6248 |  | 
|  | 6249 | touch(); | 
|  | 6250 |  | 
|  | 6251 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 6252 | } | 
|  | 6253 |  | 
|  | 6254 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6255 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6256 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 6257 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6258 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 6259 |  | 
|  | 6260 | touch(); | 
|  | 6261 |  | 
|  | 6262 | mTouchWindow->assertNoEvents(); | 
|  | 6263 | } | 
|  | 6264 |  | 
|  | 6265 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { | 
|  | 6266 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 | 
|  | 6267 | const sp<FakeWindowHandle>& w1 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6268 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, | 
|  | 6269 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6270 | const sp<FakeWindowHandle>& w2 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6271 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, | 
|  | 6272 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6273 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 6274 |  | 
|  | 6275 | touch(); | 
|  | 6276 |  | 
|  | 6277 | mTouchWindow->assertNoEvents(); | 
|  | 6278 | } | 
|  | 6279 |  | 
|  | 6280 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { | 
|  | 6281 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 | 
|  | 6282 | const sp<FakeWindowHandle>& w1 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6283 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, | 
|  | 6284 | OPACITY_FAR_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6285 | const sp<FakeWindowHandle>& w2 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6286 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, | 
|  | 6287 | OPACITY_FAR_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6288 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 6289 |  | 
|  | 6290 | touch(); | 
|  | 6291 |  | 
|  | 6292 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 6293 | } | 
|  | 6294 |  | 
|  | 6295 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 6296 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { | 
|  | 6297 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6298 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 6299 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6300 | const sp<FakeWindowHandle>& wC = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6301 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 6302 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6303 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
|  | 6304 |  | 
|  | 6305 | touch(); | 
|  | 6306 |  | 
|  | 6307 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 6308 | } | 
|  | 6309 |  | 
|  | 6310 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { | 
|  | 6311 | const sp<FakeWindowHandle>& wB = | 
| 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 | const sp<FakeWindowHandle>& wC = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6315 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 6316 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6317 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6318 |  | 
|  | 6319 | touch(); | 
|  | 6320 |  | 
|  | 6321 | mTouchWindow->assertNoEvents(); | 
|  | 6322 | } | 
|  | 6323 |  | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6324 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 6325 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { | 
|  | 6326 | const sp<FakeWindowHandle>& wA = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6327 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 6328 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6329 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6330 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 6331 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6332 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); | 
|  | 6333 |  | 
|  | 6334 | touch(); | 
|  | 6335 |  | 
|  | 6336 | mTouchWindow->assertNoEvents(); | 
|  | 6337 | } | 
|  | 6338 |  | 
|  | 6339 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 6340 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { | 
|  | 6341 | const sp<FakeWindowHandle>& wA = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6342 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 6343 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6344 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6345 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 6346 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6347 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); | 
|  | 6348 |  | 
|  | 6349 | touch(); | 
|  | 6350 |  | 
|  | 6351 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 6352 | } | 
|  | 6353 |  | 
|  | 6354 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { | 
|  | 6355 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6356 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 6357 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6358 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 6359 |  | 
|  | 6360 | touch(); | 
|  | 6361 |  | 
|  | 6362 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 6363 | } | 
|  | 6364 |  | 
|  | 6365 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { | 
|  | 6366 | const sp<FakeWindowHandle>& w = | 
|  | 6367 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 6368 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 6369 |  | 
|  | 6370 | touch(); | 
|  | 6371 |  | 
|  | 6372 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 6373 | } | 
|  | 6374 |  | 
| Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 6375 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 6376 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { | 
|  | 6377 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); | 
|  | 6378 | const sp<FakeWindowHandle>& w = | 
|  | 6379 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); | 
|  | 6380 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 6381 |  | 
|  | 6382 | touch(); | 
|  | 6383 |  | 
|  | 6384 | mTouchWindow->assertNoEvents(); | 
|  | 6385 | } | 
|  | 6386 |  | 
|  | 6387 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { | 
|  | 6388 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); | 
|  | 6389 | const sp<FakeWindowHandle>& w = | 
|  | 6390 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); | 
|  | 6391 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 6392 |  | 
|  | 6393 | touch(); | 
|  | 6394 |  | 
|  | 6395 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 6396 | } | 
|  | 6397 |  | 
|  | 6398 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 6399 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { | 
|  | 6400 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); | 
|  | 6401 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6402 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 6403 | OPACITY_ABOVE_THRESHOLD); | 
|  | 6404 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 6405 |  | 
|  | 6406 | touch(); | 
|  | 6407 |  | 
|  | 6408 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 6409 | } | 
|  | 6410 |  | 
|  | 6411 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 6412 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { | 
|  | 6413 | const sp<FakeWindowHandle>& w1 = | 
|  | 6414 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, | 
|  | 6415 | OPACITY_BELOW_THRESHOLD); | 
|  | 6416 | const sp<FakeWindowHandle>& w2 = | 
|  | 6417 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 6418 | OPACITY_BELOW_THRESHOLD); | 
|  | 6419 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 6420 |  | 
|  | 6421 | touch(); | 
|  | 6422 |  | 
|  | 6423 | mTouchWindow->assertNoEvents(); | 
|  | 6424 | } | 
|  | 6425 |  | 
|  | 6426 | /** | 
|  | 6427 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the | 
|  | 6428 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold | 
|  | 6429 | * (which alone would result in allowing touches) does not affect the blocking behavior. | 
|  | 6430 | */ | 
|  | 6431 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 6432 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { | 
|  | 6433 | const sp<FakeWindowHandle>& wB = | 
|  | 6434 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, | 
|  | 6435 | OPACITY_BELOW_THRESHOLD); | 
|  | 6436 | const sp<FakeWindowHandle>& wC = | 
|  | 6437 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 6438 | OPACITY_BELOW_THRESHOLD); | 
|  | 6439 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
|  | 6440 |  | 
|  | 6441 | touch(); | 
|  | 6442 |  | 
|  | 6443 | mTouchWindow->assertNoEvents(); | 
|  | 6444 | } | 
|  | 6445 |  | 
|  | 6446 | /** | 
|  | 6447 | * This test is testing that a window from a different UID but with same application token doesn't | 
|  | 6448 | * block the touch. Apps can share the application token for close UI collaboration for example. | 
|  | 6449 | */ | 
|  | 6450 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 6451 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { | 
|  | 6452 | const sp<FakeWindowHandle>& w = | 
|  | 6453 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 6454 | w->setApplicationToken(mTouchWindow->getApplicationToken()); | 
| Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 6455 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 6456 |  | 
|  | 6457 | touch(); | 
|  | 6458 |  | 
|  | 6459 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 6460 | } | 
|  | 6461 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6462 | class InputDispatcherDragTests : public InputDispatcherTest { | 
|  | 6463 | protected: | 
|  | 6464 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 6465 | sp<FakeWindowHandle> mWindow; | 
|  | 6466 | sp<FakeWindowHandle> mSecondWindow; | 
|  | 6467 | sp<FakeWindowHandle> mDragWindow; | 
| Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6468 | sp<FakeWindowHandle> mSpyWindow; | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6469 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. | 
|  | 6470 | static constexpr int32_t MOUSE_POINTER_ID = 1; | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6471 |  | 
|  | 6472 | void SetUp() override { | 
|  | 6473 | InputDispatcherTest::SetUp(); | 
|  | 6474 | mApp = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6475 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6476 | mWindow->setFrame(Rect(0, 0, 100, 100)); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6477 |  | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6478 | mSecondWindow = | 
|  | 6479 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6480 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6481 |  | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6482 | mSpyWindow = | 
|  | 6483 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); | 
| Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6484 | mSpyWindow->setSpy(true); | 
|  | 6485 | mSpyWindow->setTrustedOverlay(true); | 
|  | 6486 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); | 
|  | 6487 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6488 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
| Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6489 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}}); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6490 | } | 
|  | 6491 |  | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6492 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { | 
|  | 6493 | switch (fromSource) { | 
|  | 6494 | case AINPUT_SOURCE_TOUCHSCREEN: | 
|  | 6495 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6496 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6497 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 6498 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6499 | break; | 
|  | 6500 | case AINPUT_SOURCE_STYLUS: | 
|  | 6501 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6502 | injectMotionEvent( | 
|  | 6503 | mDispatcher, | 
|  | 6504 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, | 
|  | 6505 | AINPUT_SOURCE_STYLUS) | 
|  | 6506 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) | 
|  | 6507 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 6508 | .x(50) | 
|  | 6509 | .y(50)) | 
|  | 6510 | .build())); | 
|  | 6511 | break; | 
|  | 6512 | case AINPUT_SOURCE_MOUSE: | 
|  | 6513 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6514 | injectMotionEvent( | 
|  | 6515 | mDispatcher, | 
|  | 6516 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 6517 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 6518 | .pointer(PointerBuilder(MOUSE_POINTER_ID, | 
|  | 6519 | AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 6520 | .x(50) | 
|  | 6521 | .y(50)) | 
|  | 6522 | .build())); | 
|  | 6523 | break; | 
|  | 6524 | default: | 
|  | 6525 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; | 
|  | 6526 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6527 |  | 
|  | 6528 | // Window should receive motion event. | 
|  | 6529 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6530 | // Spy window should also receive motion event | 
|  | 6531 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6532 | } | 
|  | 6533 |  | 
|  | 6534 | // Start performing drag, we will create a drag window and transfer touch to it. | 
|  | 6535 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. | 
|  | 6536 | // Returns true on success. | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6537 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6538 | if (sendDown) { | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6539 | injectDown(fromSource); | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6540 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6541 |  | 
|  | 6542 | // The drag window covers the entire display | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6543 | mDragWindow = | 
|  | 6544 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); | 
| Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6545 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6546 | mDispatcher->setInputWindows( | 
| Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6547 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}}); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6548 |  | 
|  | 6549 | // Transfer touch focus to the drag window | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6550 | bool transferred = | 
|  | 6551 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), | 
|  | 6552 | true /* isDragDrop */); | 
|  | 6553 | if (transferred) { | 
|  | 6554 | mWindow->consumeMotionCancel(); | 
|  | 6555 | mDragWindow->consumeMotionDown(); | 
|  | 6556 | } | 
|  | 6557 | return transferred; | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6558 | } | 
|  | 6559 | }; | 
|  | 6560 |  | 
|  | 6561 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6562 | startDrag(); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6563 |  | 
|  | 6564 | // Move on window. | 
|  | 6565 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6566 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6567 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 6568 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6569 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 6570 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 6571 | mSecondWindow->assertNoEvents(); | 
|  | 6572 |  | 
|  | 6573 | // Move to another window. | 
|  | 6574 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6575 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6576 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 6577 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6578 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 6579 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 6580 | mSecondWindow->consumeDragEvent(false, 50, 50); | 
|  | 6581 |  | 
|  | 6582 | // Move back to original window. | 
|  | 6583 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6584 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6585 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 6586 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6587 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 6588 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 6589 | mSecondWindow->consumeDragEvent(true, -50, 50); | 
|  | 6590 |  | 
|  | 6591 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6592 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 6593 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6594 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 6595 | mWindow->assertNoEvents(); | 
|  | 6596 | mSecondWindow->assertNoEvents(); | 
|  | 6597 | } | 
|  | 6598 |  | 
| Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6599 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6600 | startDrag(); | 
| Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6601 |  | 
|  | 6602 | // No cancel event after drag start | 
|  | 6603 | mSpyWindow->assertNoEvents(); | 
|  | 6604 |  | 
|  | 6605 | const MotionEvent secondFingerDownEvent = | 
|  | 6606 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 6607 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 6608 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) | 
|  | 6609 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(60).y(60)) | 
|  | 6610 | .build(); | 
|  | 6611 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6612 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 6613 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 6614 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6615 |  | 
|  | 6616 | // Receives cancel for first pointer after next pointer down | 
|  | 6617 | mSpyWindow->consumeMotionCancel(); | 
|  | 6618 | mSpyWindow->consumeMotionDown(); | 
|  | 6619 |  | 
|  | 6620 | mSpyWindow->assertNoEvents(); | 
|  | 6621 | } | 
|  | 6622 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 6623 | TEST_F(InputDispatcherDragTests, DragAndDrop) { | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6624 | startDrag(); | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 6625 |  | 
|  | 6626 | // Move on window. | 
|  | 6627 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6628 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6629 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 6630 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6631 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 6632 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 6633 | mSecondWindow->assertNoEvents(); | 
|  | 6634 |  | 
|  | 6635 | // Move to another window. | 
|  | 6636 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6637 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6638 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 6639 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6640 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 6641 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 6642 | mSecondWindow->consumeDragEvent(false, 50, 50); | 
|  | 6643 |  | 
|  | 6644 | // drop to another window. | 
|  | 6645 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6646 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 6647 | {150, 50})) | 
|  | 6648 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6649 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 6650 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); | 
|  | 6651 | mWindow->assertNoEvents(); | 
|  | 6652 | mSecondWindow->assertNoEvents(); | 
|  | 6653 | } | 
|  | 6654 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 6655 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6656 | startDrag(true, AINPUT_SOURCE_STYLUS); | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 6657 |  | 
|  | 6658 | // Move on window and keep button pressed. | 
|  | 6659 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6660 | injectMotionEvent(mDispatcher, | 
|  | 6661 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) | 
|  | 6662 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) | 
|  | 6663 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 6664 | .x(50) | 
|  | 6665 | .y(50)) | 
|  | 6666 | .build())) | 
|  | 6667 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6668 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 6669 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 6670 | mSecondWindow->assertNoEvents(); | 
|  | 6671 |  | 
|  | 6672 | // Move to another window and release button, expect to drop item. | 
|  | 6673 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6674 | injectMotionEvent(mDispatcher, | 
|  | 6675 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) | 
|  | 6676 | .buttonState(0) | 
|  | 6677 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 6678 | .x(150) | 
|  | 6679 | .y(50)) | 
|  | 6680 | .build())) | 
|  | 6681 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6682 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 6683 | mWindow->assertNoEvents(); | 
|  | 6684 | mSecondWindow->assertNoEvents(); | 
|  | 6685 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); | 
|  | 6686 |  | 
|  | 6687 | // nothing to the window. | 
|  | 6688 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6689 | injectMotionEvent(mDispatcher, | 
|  | 6690 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) | 
|  | 6691 | .buttonState(0) | 
|  | 6692 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) | 
|  | 6693 | .x(150) | 
|  | 6694 | .y(50)) | 
|  | 6695 | .build())) | 
|  | 6696 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6697 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 6698 | mWindow->assertNoEvents(); | 
|  | 6699 | mSecondWindow->assertNoEvents(); | 
|  | 6700 | } | 
|  | 6701 |  | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6702 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6703 | startDrag(); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 6704 |  | 
|  | 6705 | // Set second window invisible. | 
|  | 6706 | mSecondWindow->setVisible(false); | 
|  | 6707 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); | 
|  | 6708 |  | 
|  | 6709 | // Move on window. | 
|  | 6710 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6711 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6712 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 6713 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6714 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 6715 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 6716 | mSecondWindow->assertNoEvents(); | 
|  | 6717 |  | 
|  | 6718 | // Move to another window. | 
|  | 6719 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6720 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6721 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 6722 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6723 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 6724 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 6725 | mSecondWindow->assertNoEvents(); | 
|  | 6726 |  | 
|  | 6727 | // drop to another window. | 
|  | 6728 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6729 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 6730 | {150, 50})) | 
|  | 6731 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6732 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 6733 | mFakePolicy->assertDropTargetEquals(nullptr); | 
|  | 6734 | mWindow->assertNoEvents(); | 
|  | 6735 | mSecondWindow->assertNoEvents(); | 
|  | 6736 | } | 
|  | 6737 |  | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6738 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6739 | // Ensure window could track pointerIds if it didn't support split touch. | 
|  | 6740 | mWindow->setPreventSplitting(true); | 
|  | 6741 |  | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6742 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6743 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 6744 | {50, 50})) | 
|  | 6745 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6746 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 6747 |  | 
|  | 6748 | const MotionEvent secondFingerDownEvent = | 
|  | 6749 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 6750 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 6751 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 6752 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) | 
|  | 6753 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(75).y(50)) | 
|  | 6754 | .build(); | 
|  | 6755 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6756 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 6757 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 6758 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6759 | mWindow->consumeMotionPointerDown(1 /* pointerIndex */); | 
|  | 6760 |  | 
|  | 6761 | // Should not perform drag and drop when window has multi fingers. | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6762 | ASSERT_FALSE(startDrag(false)); | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6763 | } | 
|  | 6764 |  | 
|  | 6765 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { | 
|  | 6766 | // First down on second window. | 
|  | 6767 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6768 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 6769 | {150, 50})) | 
|  | 6770 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6771 |  | 
|  | 6772 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 6773 |  | 
|  | 6774 | // Second down on first window. | 
|  | 6775 | const MotionEvent secondFingerDownEvent = | 
|  | 6776 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 6777 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 6778 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 6779 | .pointer( | 
|  | 6780 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) | 
|  | 6781 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) | 
|  | 6782 | .build(); | 
|  | 6783 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6784 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 6785 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 6786 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6787 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 6788 |  | 
|  | 6789 | // Perform drag and drop from first window. | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6790 | ASSERT_TRUE(startDrag(false)); | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6791 |  | 
|  | 6792 | // Move on window. | 
|  | 6793 | const MotionEvent secondFingerMoveEvent = | 
|  | 6794 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 6795 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 6796 | .pointer( | 
|  | 6797 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) | 
|  | 6798 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) | 
|  | 6799 | .build(); | 
|  | 6800 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6801 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, | 
|  | 6802 | InputEventInjectionSync::WAIT_FOR_RESULT)); | 
|  | 6803 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 6804 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 6805 | mSecondWindow->consumeMotionMove(); | 
|  | 6806 |  | 
|  | 6807 | // Release the drag pointer should perform drop. | 
|  | 6808 | const MotionEvent secondFingerUpEvent = | 
|  | 6809 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 6810 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 6811 | .pointer( | 
|  | 6812 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) | 
|  | 6813 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) | 
|  | 6814 | .build(); | 
|  | 6815 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6816 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, | 
|  | 6817 | InputEventInjectionSync::WAIT_FOR_RESULT)); | 
|  | 6818 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 6819 | mFakePolicy->assertDropTargetEquals(mWindow->getToken()); | 
|  | 6820 | mWindow->assertNoEvents(); | 
|  | 6821 | mSecondWindow->consumeMotionMove(); | 
|  | 6822 | } | 
|  | 6823 |  | 
| Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 6824 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6825 | startDrag(); | 
| Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 6826 |  | 
|  | 6827 | // Update window of second display. | 
|  | 6828 | sp<FakeWindowHandle> windowInSecondary = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6829 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 6830 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); | 
|  | 6831 |  | 
|  | 6832 | // Let second display has a touch state. | 
|  | 6833 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6834 | injectMotionEvent(mDispatcher, | 
|  | 6835 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, | 
|  | 6836 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 6837 | .displayId(SECOND_DISPLAY_ID) | 
|  | 6838 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 6839 | .x(100) | 
|  | 6840 | .y(100)) | 
|  | 6841 | .build())); | 
|  | 6842 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, | 
|  | 6843 | SECOND_DISPLAY_ID, 0 /* expectedFlag */); | 
|  | 6844 | // Update window again. | 
|  | 6845 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); | 
|  | 6846 |  | 
|  | 6847 | // Move on window. | 
|  | 6848 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6849 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6850 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 6851 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6852 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 6853 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 6854 | mSecondWindow->assertNoEvents(); | 
|  | 6855 |  | 
|  | 6856 | // Move to another window. | 
|  | 6857 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6858 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6859 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 6860 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6861 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 6862 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 6863 | mSecondWindow->consumeDragEvent(false, 50, 50); | 
|  | 6864 |  | 
|  | 6865 | // drop to another window. | 
|  | 6866 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6867 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 6868 | {150, 50})) | 
|  | 6869 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6870 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 6871 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); | 
|  | 6872 | mWindow->assertNoEvents(); | 
|  | 6873 | mSecondWindow->assertNoEvents(); | 
|  | 6874 | } | 
|  | 6875 |  | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6876 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { | 
|  | 6877 | startDrag(true, AINPUT_SOURCE_MOUSE); | 
|  | 6878 | // Move on window. | 
|  | 6879 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6880 | injectMotionEvent(mDispatcher, | 
|  | 6881 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) | 
|  | 6882 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 6883 | .pointer(PointerBuilder(MOUSE_POINTER_ID, | 
|  | 6884 | AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 6885 | .x(50) | 
|  | 6886 | .y(50)) | 
|  | 6887 | .build())) | 
|  | 6888 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6889 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 6890 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 6891 | mSecondWindow->assertNoEvents(); | 
|  | 6892 |  | 
|  | 6893 | // Move to another window. | 
|  | 6894 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6895 | injectMotionEvent(mDispatcher, | 
|  | 6896 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) | 
|  | 6897 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 6898 | .pointer(PointerBuilder(MOUSE_POINTER_ID, | 
|  | 6899 | AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 6900 | .x(150) | 
|  | 6901 | .y(50)) | 
|  | 6902 | .build())) | 
|  | 6903 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6904 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 6905 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 6906 | mSecondWindow->consumeDragEvent(false, 50, 50); | 
|  | 6907 |  | 
|  | 6908 | // drop to another window. | 
|  | 6909 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6910 | injectMotionEvent(mDispatcher, | 
|  | 6911 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) | 
|  | 6912 | .buttonState(0) | 
|  | 6913 | .pointer(PointerBuilder(MOUSE_POINTER_ID, | 
|  | 6914 | AMOTION_EVENT_TOOL_TYPE_MOUSE) | 
|  | 6915 | .x(150) | 
|  | 6916 | .y(50)) | 
|  | 6917 | .build())) | 
|  | 6918 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6919 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 6920 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); | 
|  | 6921 | mWindow->assertNoEvents(); | 
|  | 6922 | mSecondWindow->assertNoEvents(); | 
|  | 6923 | } | 
|  | 6924 |  | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6925 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; | 
|  | 6926 |  | 
|  | 6927 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { | 
|  | 6928 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6929 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 6930 | "Test window", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6931 | window->setDropInput(true); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6932 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 6933 | window->setFocusable(true); | 
|  | 6934 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 6935 | setFocusedWindow(window); | 
|  | 6936 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 6937 |  | 
|  | 6938 | // With the flag set, window should not get any input | 
|  | 6939 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 6940 | mDispatcher->notifyKey(&keyArgs); | 
|  | 6941 | window->assertNoEvents(); | 
|  | 6942 |  | 
|  | 6943 | NotifyMotionArgs motionArgs = | 
|  | 6944 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6945 | ADISPLAY_ID_DEFAULT); | 
|  | 6946 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 6947 | window->assertNoEvents(); | 
|  | 6948 |  | 
|  | 6949 | // With the flag cleared, the window should get input | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6950 | window->setDropInput(false); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6951 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 6952 |  | 
|  | 6953 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); | 
|  | 6954 | mDispatcher->notifyKey(&keyArgs); | 
|  | 6955 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); | 
|  | 6956 |  | 
|  | 6957 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6958 | ADISPLAY_ID_DEFAULT); | 
|  | 6959 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 6960 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 6961 | window->assertNoEvents(); | 
|  | 6962 | } | 
|  | 6963 |  | 
|  | 6964 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { | 
|  | 6965 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = | 
|  | 6966 | std::make_shared<FakeApplicationHandle>(); | 
|  | 6967 | sp<FakeWindowHandle> obscuringWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6968 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", | 
|  | 6969 | ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6970 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); | 
|  | 6971 | obscuringWindow->setOwnerInfo(111, 111); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6972 | obscuringWindow->setTouchable(false); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6973 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6974 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 6975 | "Test window", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6976 | window->setDropInputIfObscured(true); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6977 | window->setOwnerInfo(222, 222); | 
|  | 6978 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 6979 | window->setFocusable(true); | 
|  | 6980 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); | 
|  | 6981 | setFocusedWindow(window); | 
|  | 6982 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 6983 |  | 
|  | 6984 | // With the flag set, window should not get any input | 
|  | 6985 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 6986 | mDispatcher->notifyKey(&keyArgs); | 
|  | 6987 | window->assertNoEvents(); | 
|  | 6988 |  | 
|  | 6989 | NotifyMotionArgs motionArgs = | 
|  | 6990 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6991 | ADISPLAY_ID_DEFAULT); | 
|  | 6992 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 6993 | window->assertNoEvents(); | 
|  | 6994 |  | 
|  | 6995 | // With the flag cleared, the window should get input | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6996 | window->setDropInputIfObscured(false); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6997 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); | 
|  | 6998 |  | 
|  | 6999 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); | 
|  | 7000 | mDispatcher->notifyKey(&keyArgs); | 
|  | 7001 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); | 
|  | 7002 |  | 
|  | 7003 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 7004 | ADISPLAY_ID_DEFAULT); | 
|  | 7005 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 7006 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); | 
|  | 7007 | window->assertNoEvents(); | 
|  | 7008 | } | 
|  | 7009 |  | 
|  | 7010 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { | 
|  | 7011 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = | 
|  | 7012 | std::make_shared<FakeApplicationHandle>(); | 
|  | 7013 | sp<FakeWindowHandle> obscuringWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7014 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", | 
|  | 7015 | ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7016 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); | 
|  | 7017 | obscuringWindow->setOwnerInfo(111, 111); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7018 | obscuringWindow->setTouchable(false); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7019 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7020 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 7021 | "Test window", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7022 | window->setDropInputIfObscured(true); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7023 | window->setOwnerInfo(222, 222); | 
|  | 7024 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 7025 | window->setFocusable(true); | 
|  | 7026 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); | 
|  | 7027 | setFocusedWindow(window); | 
|  | 7028 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 7029 |  | 
|  | 7030 | // With the flag set, window should not get any input | 
|  | 7031 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 7032 | mDispatcher->notifyKey(&keyArgs); | 
|  | 7033 | window->assertNoEvents(); | 
|  | 7034 |  | 
|  | 7035 | NotifyMotionArgs motionArgs = | 
|  | 7036 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 7037 | ADISPLAY_ID_DEFAULT); | 
|  | 7038 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 7039 | window->assertNoEvents(); | 
|  | 7040 |  | 
|  | 7041 | // When the window is no longer obscured because it went on top, it should get input | 
|  | 7042 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); | 
|  | 7043 |  | 
|  | 7044 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); | 
|  | 7045 | mDispatcher->notifyKey(&keyArgs); | 
|  | 7046 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); | 
|  | 7047 |  | 
|  | 7048 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 7049 | ADISPLAY_ID_DEFAULT); | 
|  | 7050 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 7051 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 7052 | window->assertNoEvents(); | 
|  | 7053 | } | 
|  | 7054 |  | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7055 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { | 
|  | 7056 | protected: | 
|  | 7057 | std::shared_ptr<FakeApplicationHandle> mApp; | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7058 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7059 | sp<FakeWindowHandle> mWindow; | 
|  | 7060 | sp<FakeWindowHandle> mSecondWindow; | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7061 | sp<FakeWindowHandle> mThirdWindow; | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7062 |  | 
|  | 7063 | void SetUp() override { | 
|  | 7064 | InputDispatcherTest::SetUp(); | 
|  | 7065 |  | 
|  | 7066 | mApp = std::make_shared<FakeApplicationHandle>(); | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7067 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7068 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7069 | mWindow->setFocusable(true); | 
| Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7070 | setFocusedWindow(mWindow); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7071 | mSecondWindow = | 
|  | 7072 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7073 | mSecondWindow->setFocusable(true); | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7074 | mThirdWindow = | 
|  | 7075 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, | 
|  | 7076 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); | 
|  | 7077 | mThirdWindow->setFocusable(true); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7078 |  | 
|  | 7079 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7080 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}, | 
|  | 7081 | {SECOND_DISPLAY_ID, {mThirdWindow}}}); | 
|  | 7082 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7083 | mWindow->consumeFocusEvent(true); | 
| Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7084 |  | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7085 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 7086 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7087 | WINDOW_UID, true /* hasPermission */, | 
|  | 7088 | ADISPLAY_ID_DEFAULT)) { | 
| Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7089 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); | 
|  | 7090 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7091 | mThirdWindow->assertNoEvents(); | 
|  | 7092 | } | 
|  | 7093 |  | 
|  | 7094 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. | 
|  | 7095 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, | 
|  | 7096 | SECONDARY_WINDOW_UID, true /* hasPermission */, | 
|  | 7097 | SECOND_DISPLAY_ID)) { | 
|  | 7098 | mWindow->assertNoEvents(); | 
|  | 7099 | mSecondWindow->assertNoEvents(); | 
|  | 7100 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); | 
| Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7101 | } | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7102 | } | 
|  | 7103 |  | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7104 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid, | 
|  | 7105 | bool hasPermission) { | 
| Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7106 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, | 
|  | 7107 | ADISPLAY_ID_DEFAULT)); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7108 | mWindow->consumeTouchModeEvent(inTouchMode); | 
|  | 7109 | mSecondWindow->consumeTouchModeEvent(inTouchMode); | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7110 | mThirdWindow->assertNoEvents(); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7111 | } | 
|  | 7112 | }; | 
|  | 7113 |  | 
| Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7114 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { | 
| Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7115 | const WindowInfo& windowInfo = *mWindow->getInfo(); | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7116 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, | 
|  | 7117 | windowInfo.ownerPid, windowInfo.ownerUid, | 
|  | 7118 | false /*  hasPermission */); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7119 | } | 
|  | 7120 |  | 
| Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7121 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { | 
|  | 7122 | const WindowInfo& windowInfo = *mWindow->getInfo(); | 
|  | 7123 | int32_t ownerPid = windowInfo.ownerPid; | 
|  | 7124 | int32_t ownerUid = windowInfo.ownerUid; | 
|  | 7125 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); | 
|  | 7126 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, | 
| Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7127 | ownerUid, false /*hasPermission*/, | 
|  | 7128 | ADISPLAY_ID_DEFAULT)); | 
| Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7129 | mWindow->assertNoEvents(); | 
|  | 7130 | mSecondWindow->assertNoEvents(); | 
|  | 7131 | } | 
|  | 7132 |  | 
|  | 7133 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { | 
|  | 7134 | const WindowInfo& windowInfo = *mWindow->getInfo(); | 
|  | 7135 | int32_t ownerPid = windowInfo.ownerPid; | 
|  | 7136 | int32_t ownerUid = windowInfo.ownerUid; | 
|  | 7137 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7138 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, | 
|  | 7139 | ownerUid, true /*hasPermission*/); | 
| Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7140 | } | 
|  | 7141 |  | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7142 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { | 
| Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7143 | const WindowInfo& windowInfo = *mWindow->getInfo(); | 
| Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7144 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, | 
|  | 7145 | windowInfo.ownerPid, windowInfo.ownerUid, | 
| Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7146 | true /*hasPermission*/, ADISPLAY_ID_DEFAULT)); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7147 | mWindow->assertNoEvents(); | 
|  | 7148 | mSecondWindow->assertNoEvents(); | 
|  | 7149 | } | 
|  | 7150 |  | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7151 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { | 
|  | 7152 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); | 
|  | 7153 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, | 
|  | 7154 | windowInfo.ownerPid, windowInfo.ownerUid, | 
|  | 7155 | true /*hasPermission*/, SECOND_DISPLAY_ID)); | 
|  | 7156 | mWindow->assertNoEvents(); | 
|  | 7157 | mSecondWindow->assertNoEvents(); | 
|  | 7158 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); | 
|  | 7159 | } | 
|  | 7160 |  | 
| Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7161 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { | 
|  | 7162 | // Interact with the window first. | 
|  | 7163 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
|  | 7164 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7165 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 7166 |  | 
|  | 7167 | // Then remove focus. | 
|  | 7168 | mWindow->setFocusable(false); | 
|  | 7169 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 7170 |  | 
|  | 7171 | // Assert that caller can switch touch mode by owning one of the last interacted window. | 
|  | 7172 | const WindowInfo& windowInfo = *mWindow->getInfo(); | 
|  | 7173 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, | 
|  | 7174 | windowInfo.ownerPid, windowInfo.ownerUid, | 
| Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7175 | false /*hasPermission*/, ADISPLAY_ID_DEFAULT)); | 
| Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7176 | } | 
|  | 7177 |  | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7178 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { | 
|  | 7179 | public: | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7180 | sp<FakeWindowHandle> createSpy() { | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7181 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 7182 | std::make_shared<FakeApplicationHandle>(); | 
|  | 7183 | std::string name = "Fake Spy "; | 
|  | 7184 | name += std::to_string(mSpyCount++); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7185 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 7186 | name.c_str(), ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7187 | spy->setSpy(true); | 
| Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7188 | spy->setTrustedOverlay(true); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7189 | return spy; | 
|  | 7190 | } | 
|  | 7191 |  | 
|  | 7192 | sp<FakeWindowHandle> createForeground() { | 
|  | 7193 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 7194 | std::make_shared<FakeApplicationHandle>(); | 
|  | 7195 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7196 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", | 
|  | 7197 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7198 | window->setFocusable(true); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7199 | return window; | 
|  | 7200 | } | 
|  | 7201 |  | 
|  | 7202 | private: | 
|  | 7203 | int mSpyCount{0}; | 
|  | 7204 | }; | 
|  | 7205 |  | 
| Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7206 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7207 | /** | 
| Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7208 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. | 
|  | 7209 | */ | 
| Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7210 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { | 
|  | 7211 | ScopedSilentDeath _silentDeath; | 
|  | 7212 |  | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7213 | auto spy = createSpy(); | 
| Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7214 | spy->setTrustedOverlay(false); | 
|  | 7215 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), | 
|  | 7216 | ".* not a trusted overlay"); | 
|  | 7217 | } | 
|  | 7218 |  | 
|  | 7219 | /** | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7220 | * Input injection into a display with a spy window but no foreground windows should succeed. | 
|  | 7221 | */ | 
|  | 7222 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7223 | auto spy = createSpy(); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7224 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); | 
|  | 7225 |  | 
|  | 7226 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7227 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 7228 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7229 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 7230 | } | 
|  | 7231 |  | 
|  | 7232 | /** | 
|  | 7233 | * Verify the order in which different input windows receive events. The touched foreground window | 
|  | 7234 | * (if there is one) should always receive the event first. When there are multiple spy windows, the | 
|  | 7235 | * spy windows will receive the event according to their Z-order, where the top-most spy window will | 
|  | 7236 | * receive events before ones belows it. | 
|  | 7237 | * | 
|  | 7238 | * Here, we set up a scenario with four windows in the following Z order from the top: | 
|  | 7239 | *    spy1, spy2, window, spy3. | 
|  | 7240 | * We then inject an event and verify that the foreground "window" receives it first, followed by | 
|  | 7241 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground | 
|  | 7242 | * window. | 
|  | 7243 | */ | 
|  | 7244 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { | 
|  | 7245 | auto window = createForeground(); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7246 | auto spy1 = createSpy(); | 
|  | 7247 | auto spy2 = createSpy(); | 
|  | 7248 | auto spy3 = createSpy(); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7249 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); | 
|  | 7250 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; | 
|  | 7251 | const size_t numChannels = channels.size(); | 
|  | 7252 |  | 
| Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 7253 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7254 | if (!epollFd.ok()) { | 
|  | 7255 | FAIL() << "Failed to create epoll fd"; | 
|  | 7256 | } | 
|  | 7257 |  | 
|  | 7258 | for (size_t i = 0; i < numChannels; i++) { | 
|  | 7259 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; | 
|  | 7260 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { | 
|  | 7261 | FAIL() << "Failed to add fd to epoll"; | 
|  | 7262 | } | 
|  | 7263 | } | 
|  | 7264 |  | 
|  | 7265 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7266 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 7267 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7268 |  | 
|  | 7269 | std::vector<size_t> eventOrder; | 
|  | 7270 | std::vector<struct epoll_event> events(numChannels); | 
|  | 7271 | for (;;) { | 
|  | 7272 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), | 
|  | 7273 | (100ms).count()); | 
|  | 7274 | if (nFds < 0) { | 
|  | 7275 | FAIL() << "Failed to call epoll_wait"; | 
|  | 7276 | } | 
|  | 7277 | if (nFds == 0) { | 
|  | 7278 | break; // epoll_wait timed out | 
|  | 7279 | } | 
|  | 7280 | for (int i = 0; i < nFds; i++) { | 
| Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 7281 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); | 
| Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 7282 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7283 | channels[i]->consumeMotionDown(); | 
|  | 7284 | } | 
|  | 7285 | } | 
|  | 7286 |  | 
|  | 7287 | // Verify the order in which the events were received. | 
|  | 7288 | EXPECT_EQ(3u, eventOrder.size()); | 
|  | 7289 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window | 
|  | 7290 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 | 
|  | 7291 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 | 
|  | 7292 | } | 
|  | 7293 |  | 
|  | 7294 | /** | 
|  | 7295 | * A spy window using the NOT_TOUCHABLE flag does not receive events. | 
|  | 7296 | */ | 
|  | 7297 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { | 
|  | 7298 | auto window = createForeground(); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7299 | auto spy = createSpy(); | 
|  | 7300 | spy->setTouchable(false); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7301 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 7302 |  | 
|  | 7303 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7304 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 7305 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7306 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 7307 | spy->assertNoEvents(); | 
|  | 7308 | } | 
|  | 7309 |  | 
|  | 7310 | /** | 
|  | 7311 | * A spy window will only receive gestures that originate within its touchable region. Gestures that | 
|  | 7312 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched | 
|  | 7313 | * to the window. | 
|  | 7314 | */ | 
|  | 7315 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { | 
|  | 7316 | auto window = createForeground(); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7317 | auto spy = createSpy(); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7318 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); | 
|  | 7319 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 7320 |  | 
|  | 7321 | // Inject an event outside the spy window's touchable region. | 
|  | 7322 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7323 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 7324 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7325 | window->consumeMotionDown(); | 
|  | 7326 | spy->assertNoEvents(); | 
|  | 7327 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7328 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 7329 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7330 | window->consumeMotionUp(); | 
|  | 7331 | spy->assertNoEvents(); | 
|  | 7332 |  | 
|  | 7333 | // Inject an event inside the spy window's touchable region. | 
|  | 7334 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7335 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7336 | {5, 10})) | 
|  | 7337 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7338 | window->consumeMotionDown(); | 
|  | 7339 | spy->consumeMotionDown(); | 
|  | 7340 | } | 
|  | 7341 |  | 
|  | 7342 | /** | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7343 | * 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] | 7344 | * 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] | 7345 | */ | 
|  | 7346 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { | 
|  | 7347 | auto window = createForeground(); | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7348 | window->setOwnerInfo(12, 34); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7349 | auto spy = createSpy(); | 
|  | 7350 | spy->setWatchOutsideTouch(true); | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7351 | spy->setOwnerInfo(56, 78); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7352 | spy->setFrame(Rect{0, 0, 20, 20}); | 
|  | 7353 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 7354 |  | 
|  | 7355 | // Inject an event outside the spy window's frame and touchable region. | 
|  | 7356 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7357 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7358 | {100, 200})) | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7359 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7360 | window->consumeMotionDown(); | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7361 | spy->consumeMotionOutsideWithZeroedCoords(); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7362 | } | 
|  | 7363 |  | 
|  | 7364 | /** | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7365 | * Even when a spy window spans over multiple foreground windows, the spy should receive all | 
|  | 7366 | * pointers that are down within its bounds. | 
|  | 7367 | */ | 
|  | 7368 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { | 
|  | 7369 | auto windowLeft = createForeground(); | 
|  | 7370 | windowLeft->setFrame({0, 0, 100, 200}); | 
|  | 7371 | auto windowRight = createForeground(); | 
|  | 7372 | windowRight->setFrame({100, 0, 200, 200}); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7373 | auto spy = createSpy(); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7374 | spy->setFrame({0, 0, 200, 200}); | 
|  | 7375 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); | 
|  | 7376 |  | 
|  | 7377 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7378 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7379 | {50, 50})) | 
|  | 7380 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7381 | windowLeft->consumeMotionDown(); | 
|  | 7382 | spy->consumeMotionDown(); | 
|  | 7383 |  | 
|  | 7384 | const MotionEvent secondFingerDownEvent = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7385 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7386 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 7387 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) | 
|  | 7388 | .pointer( | 
|  | 7389 | PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) | 
|  | 7390 | .build(); | 
|  | 7391 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7392 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 7393 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 7394 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7395 | windowRight->consumeMotionDown(); | 
|  | 7396 | spy->consumeMotionPointerDown(1 /*pointerIndex*/); | 
|  | 7397 | } | 
|  | 7398 |  | 
|  | 7399 | /** | 
|  | 7400 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the | 
|  | 7401 | * the spy should receive the second pointer with ACTION_DOWN. | 
|  | 7402 | */ | 
|  | 7403 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { | 
|  | 7404 | auto window = createForeground(); | 
|  | 7405 | window->setFrame({0, 0, 200, 200}); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7406 | auto spyRight = createSpy(); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7407 | spyRight->setFrame({100, 0, 200, 200}); | 
|  | 7408 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); | 
|  | 7409 |  | 
|  | 7410 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7411 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7412 | {50, 50})) | 
|  | 7413 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7414 | window->consumeMotionDown(); | 
|  | 7415 | spyRight->assertNoEvents(); | 
|  | 7416 |  | 
|  | 7417 | const MotionEvent secondFingerDownEvent = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7418 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7419 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 7420 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) | 
|  | 7421 | .pointer( | 
|  | 7422 | PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) | 
|  | 7423 | .build(); | 
|  | 7424 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7425 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 7426 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 7427 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7428 | window->consumeMotionPointerDown(1 /*pointerIndex*/); | 
|  | 7429 | spyRight->consumeMotionDown(); | 
|  | 7430 | } | 
|  | 7431 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7432 | /** | 
|  | 7433 | * The spy window should not be able to affect whether or not touches are split. Only the foreground | 
|  | 7434 | * windows should be allowed to control split touch. | 
|  | 7435 | */ | 
|  | 7436 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7437 | // 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] | 7438 | // because a foreground window has not disabled splitting. | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7439 | auto spy = createSpy(); | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7440 | spy->setPreventSplitting(true); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7441 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7442 | auto window = createForeground(); | 
|  | 7443 | window->setFrame(Rect(0, 0, 100, 100)); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7444 |  | 
|  | 7445 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 7446 |  | 
|  | 7447 | // First finger down, no window touched. | 
|  | 7448 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7449 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7450 | {100, 200})) | 
|  | 7451 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7452 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 7453 | window->assertNoEvents(); | 
|  | 7454 |  | 
|  | 7455 | // Second finger down on window, the window should receive touch down. | 
|  | 7456 | const MotionEvent secondFingerDownEvent = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7457 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7458 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 7459 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 7460 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 7461 | .x(100) | 
|  | 7462 | .y(200)) | 
|  | 7463 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) | 
|  | 7464 | .build(); | 
|  | 7465 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7466 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 7467 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 7468 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7469 |  | 
|  | 7470 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 7471 | spy->consumeMotionPointerDown(1 /* pointerIndex */); | 
|  | 7472 | } | 
|  | 7473 |  | 
|  | 7474 | /** | 
|  | 7475 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows | 
|  | 7476 | * do not receive key events. | 
|  | 7477 | */ | 
|  | 7478 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7479 | auto spy = createSpy(); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7480 | spy->setFocusable(false); | 
|  | 7481 |  | 
|  | 7482 | auto window = createForeground(); | 
|  | 7483 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 7484 | setFocusedWindow(window); | 
|  | 7485 | window->consumeFocusEvent(true); | 
|  | 7486 |  | 
|  | 7487 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 7488 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7489 | window->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 7490 |  | 
|  | 7491 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 7492 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7493 | window->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 7494 |  | 
|  | 7495 | spy->assertNoEvents(); | 
|  | 7496 | } | 
|  | 7497 |  | 
| Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 7498 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; | 
|  | 7499 |  | 
|  | 7500 | /** | 
|  | 7501 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that | 
|  | 7502 | * are currently sent to any other windows - including other spy windows - will also be cancelled. | 
|  | 7503 | */ | 
|  | 7504 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { | 
|  | 7505 | auto window = createForeground(); | 
|  | 7506 | auto spy1 = createSpy(); | 
|  | 7507 | auto spy2 = createSpy(); | 
|  | 7508 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); | 
|  | 7509 |  | 
|  | 7510 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7511 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 7512 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7513 | window->consumeMotionDown(); | 
|  | 7514 | spy1->consumeMotionDown(); | 
|  | 7515 | spy2->consumeMotionDown(); | 
|  | 7516 |  | 
|  | 7517 | // Pilfer pointers from the second spy window. | 
|  | 7518 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); | 
|  | 7519 | spy2->assertNoEvents(); | 
|  | 7520 | spy1->consumeMotionCancel(); | 
|  | 7521 | window->consumeMotionCancel(); | 
|  | 7522 |  | 
|  | 7523 | // The rest of the gesture should only be sent to the second spy window. | 
|  | 7524 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7525 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 7526 | ADISPLAY_ID_DEFAULT)) | 
|  | 7527 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7528 | spy2->consumeMotionMove(); | 
|  | 7529 | spy1->assertNoEvents(); | 
|  | 7530 | window->assertNoEvents(); | 
|  | 7531 | } | 
|  | 7532 |  | 
|  | 7533 | /** | 
|  | 7534 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed | 
|  | 7535 | * in the middle of the gesture. | 
|  | 7536 | */ | 
|  | 7537 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { | 
|  | 7538 | auto window = createForeground(); | 
|  | 7539 | auto spy = createSpy(); | 
|  | 7540 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 7541 |  | 
|  | 7542 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7543 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 7544 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7545 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 7546 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 7547 |  | 
|  | 7548 | window->releaseChannel(); | 
|  | 7549 |  | 
|  | 7550 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); | 
|  | 7551 |  | 
|  | 7552 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7553 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 7554 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7555 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 7556 | } | 
|  | 7557 |  | 
|  | 7558 | /** | 
|  | 7559 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to | 
|  | 7560 | * the spy, but not to any other windows. | 
|  | 7561 | */ | 
|  | 7562 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { | 
|  | 7563 | auto spy = createSpy(); | 
|  | 7564 | auto window = createForeground(); | 
|  | 7565 |  | 
|  | 7566 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 7567 |  | 
|  | 7568 | // First finger down on the window and the spy. | 
|  | 7569 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7570 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7571 | {100, 200})) | 
|  | 7572 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7573 | spy->consumeMotionDown(); | 
|  | 7574 | window->consumeMotionDown(); | 
|  | 7575 |  | 
|  | 7576 | // Spy window pilfers the pointers. | 
|  | 7577 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); | 
|  | 7578 | window->consumeMotionCancel(); | 
|  | 7579 |  | 
|  | 7580 | // Second finger down on the window and spy, but the window should not receive the pointer down. | 
|  | 7581 | const MotionEvent secondFingerDownEvent = | 
|  | 7582 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 7583 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 7584 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 7585 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 7586 | .x(100) | 
|  | 7587 | .y(200)) | 
|  | 7588 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) | 
|  | 7589 | .build(); | 
|  | 7590 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7591 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 7592 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 7593 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7594 |  | 
|  | 7595 | spy->consumeMotionPointerDown(1 /*pointerIndex*/); | 
|  | 7596 |  | 
|  | 7597 | // Third finger goes down outside all windows, so injection should fail. | 
|  | 7598 | const MotionEvent thirdFingerDownEvent = | 
|  | 7599 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 7600 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 7601 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 7602 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 7603 | .x(100) | 
|  | 7604 | .y(200)) | 
|  | 7605 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) | 
|  | 7606 | .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-5).y(-5)) | 
|  | 7607 | .build(); | 
|  | 7608 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
|  | 7609 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 7610 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 7611 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7612 |  | 
|  | 7613 | spy->assertNoEvents(); | 
|  | 7614 | window->assertNoEvents(); | 
|  | 7615 | } | 
|  | 7616 |  | 
|  | 7617 | /** | 
|  | 7618 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled | 
|  | 7619 | */ | 
|  | 7620 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { | 
|  | 7621 | auto spy = createSpy(); | 
|  | 7622 | spy->setFrame(Rect(0, 0, 100, 100)); | 
|  | 7623 | auto window = createForeground(); | 
|  | 7624 | window->setFrame(Rect(0, 0, 200, 200)); | 
|  | 7625 |  | 
|  | 7626 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 7627 |  | 
|  | 7628 | // First finger down on the window only | 
|  | 7629 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7630 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7631 | {150, 150})) | 
|  | 7632 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7633 | window->consumeMotionDown(); | 
|  | 7634 |  | 
|  | 7635 | // Second finger down on the spy and window | 
|  | 7636 | const MotionEvent secondFingerDownEvent = | 
|  | 7637 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 7638 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 7639 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 7640 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 7641 | .x(150) | 
|  | 7642 | .y(150)) | 
|  | 7643 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) | 
|  | 7644 | .build(); | 
|  | 7645 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7646 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 7647 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 7648 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7649 | spy->consumeMotionDown(); | 
|  | 7650 | window->consumeMotionPointerDown(1); | 
|  | 7651 |  | 
|  | 7652 | // Third finger down on the spy and window | 
|  | 7653 | const MotionEvent thirdFingerDownEvent = | 
|  | 7654 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 7655 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 7656 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 7657 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 7658 | .x(150) | 
|  | 7659 | .y(150)) | 
|  | 7660 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) | 
|  | 7661 | .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) | 
|  | 7662 | .build(); | 
|  | 7663 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7664 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 7665 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 7666 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7667 | spy->consumeMotionPointerDown(1); | 
|  | 7668 | window->consumeMotionPointerDown(2); | 
|  | 7669 |  | 
|  | 7670 | // Spy window pilfers the pointers. | 
|  | 7671 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); | 
|  | 7672 | window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); | 
|  | 7673 | window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); | 
|  | 7674 |  | 
|  | 7675 | spy->assertNoEvents(); | 
|  | 7676 | window->assertNoEvents(); | 
|  | 7677 | } | 
|  | 7678 |  | 
|  | 7679 | /** | 
|  | 7680 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to | 
|  | 7681 | * other windows should be canceled. If this results in the cancellation of all pointers for some | 
|  | 7682 | * window, then that window should receive ACTION_CANCEL. | 
|  | 7683 | */ | 
|  | 7684 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { | 
|  | 7685 | auto spy = createSpy(); | 
|  | 7686 | spy->setFrame(Rect(0, 0, 100, 100)); | 
|  | 7687 | auto window = createForeground(); | 
|  | 7688 | window->setFrame(Rect(0, 0, 200, 200)); | 
|  | 7689 |  | 
|  | 7690 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 7691 |  | 
|  | 7692 | // First finger down on both spy and window | 
|  | 7693 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7694 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7695 | {10, 10})) | 
|  | 7696 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7697 | window->consumeMotionDown(); | 
|  | 7698 | spy->consumeMotionDown(); | 
|  | 7699 |  | 
|  | 7700 | // Second finger down on the spy and window | 
|  | 7701 | const MotionEvent secondFingerDownEvent = | 
|  | 7702 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 7703 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 7704 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 7705 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) | 
|  | 7706 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) | 
|  | 7707 | .build(); | 
|  | 7708 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7709 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 7710 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 7711 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7712 | spy->consumeMotionPointerDown(1); | 
|  | 7713 | window->consumeMotionPointerDown(1); | 
|  | 7714 |  | 
|  | 7715 | // Spy window pilfers the pointers. | 
|  | 7716 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); | 
|  | 7717 | window->consumeMotionCancel(); | 
|  | 7718 |  | 
|  | 7719 | spy->assertNoEvents(); | 
|  | 7720 | window->assertNoEvents(); | 
|  | 7721 | } | 
|  | 7722 |  | 
|  | 7723 | /** | 
|  | 7724 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still | 
|  | 7725 | * be sent to other windows | 
|  | 7726 | */ | 
|  | 7727 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { | 
|  | 7728 | auto spy = createSpy(); | 
|  | 7729 | spy->setFrame(Rect(0, 0, 100, 100)); | 
|  | 7730 | auto window = createForeground(); | 
|  | 7731 | window->setFrame(Rect(0, 0, 200, 200)); | 
|  | 7732 |  | 
|  | 7733 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 7734 |  | 
|  | 7735 | // First finger down on both window and spy | 
|  | 7736 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7737 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7738 | {10, 10})) | 
|  | 7739 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7740 | window->consumeMotionDown(); | 
|  | 7741 | spy->consumeMotionDown(); | 
|  | 7742 |  | 
|  | 7743 | // Spy window pilfers the pointers. | 
|  | 7744 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); | 
|  | 7745 | window->consumeMotionCancel(); | 
|  | 7746 |  | 
|  | 7747 | // Second finger down on the window only | 
|  | 7748 | const MotionEvent secondFingerDownEvent = | 
|  | 7749 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 7750 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 7751 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 7752 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) | 
|  | 7753 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) | 
|  | 7754 | .x(150) | 
|  | 7755 | .y(150)) | 
|  | 7756 | .build(); | 
|  | 7757 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7758 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 7759 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 7760 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 7761 | window->consumeMotionDown(); | 
|  | 7762 | window->assertNoEvents(); | 
|  | 7763 |  | 
|  | 7764 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line | 
|  | 7765 | spy->consumeMotionMove(); | 
|  | 7766 | spy->assertNoEvents(); | 
|  | 7767 | } | 
|  | 7768 |  | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7769 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { | 
|  | 7770 | public: | 
|  | 7771 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { | 
|  | 7772 | std::shared_ptr<FakeApplicationHandle> overlayApplication = | 
|  | 7773 | std::make_shared<FakeApplicationHandle>(); | 
|  | 7774 | sp<FakeWindowHandle> overlay = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7775 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, | 
|  | 7776 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7777 | overlay->setFocusable(false); | 
|  | 7778 | overlay->setOwnerInfo(111, 111); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7779 | overlay->setTouchable(false); | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7780 | overlay->setInterceptsStylus(true); | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7781 | overlay->setTrustedOverlay(true); | 
|  | 7782 |  | 
|  | 7783 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 7784 | std::make_shared<FakeApplicationHandle>(); | 
|  | 7785 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7786 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", | 
|  | 7787 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7788 | window->setFocusable(true); | 
|  | 7789 | window->setOwnerInfo(222, 222); | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7790 |  | 
|  | 7791 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 7792 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); | 
|  | 7793 | setFocusedWindow(window); | 
|  | 7794 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); | 
|  | 7795 | return {std::move(overlay), std::move(window)}; | 
|  | 7796 | } | 
|  | 7797 |  | 
|  | 7798 | void sendFingerEvent(int32_t action) { | 
|  | 7799 | NotifyMotionArgs motionArgs = | 
|  | 7800 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, | 
|  | 7801 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}}); | 
|  | 7802 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 7803 | } | 
|  | 7804 |  | 
|  | 7805 | void sendStylusEvent(int32_t action) { | 
|  | 7806 | NotifyMotionArgs motionArgs = | 
|  | 7807 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, | 
|  | 7808 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); | 
|  | 7809 | motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS; | 
|  | 7810 | mDispatcher->notifyMotion(&motionArgs); | 
|  | 7811 | } | 
|  | 7812 | }; | 
|  | 7813 |  | 
| Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7814 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; | 
|  | 7815 |  | 
|  | 7816 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { | 
|  | 7817 | ScopedSilentDeath _silentDeath; | 
|  | 7818 |  | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7819 | auto [overlay, window] = setupStylusOverlayScenario(); | 
|  | 7820 | overlay->setTrustedOverlay(false); | 
|  | 7821 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. | 
|  | 7822 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), | 
|  | 7823 | ".* not a trusted overlay"); | 
|  | 7824 | } | 
|  | 7825 |  | 
|  | 7826 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { | 
|  | 7827 | auto [overlay, window] = setupStylusOverlayScenario(); | 
|  | 7828 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); | 
|  | 7829 |  | 
|  | 7830 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); | 
|  | 7831 | overlay->consumeMotionDown(); | 
|  | 7832 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); | 
|  | 7833 | overlay->consumeMotionUp(); | 
|  | 7834 |  | 
|  | 7835 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); | 
|  | 7836 | window->consumeMotionDown(); | 
|  | 7837 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); | 
|  | 7838 | window->consumeMotionUp(); | 
|  | 7839 |  | 
|  | 7840 | overlay->assertNoEvents(); | 
|  | 7841 | window->assertNoEvents(); | 
|  | 7842 | } | 
|  | 7843 |  | 
|  | 7844 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { | 
|  | 7845 | auto [overlay, window] = setupStylusOverlayScenario(); | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7846 | overlay->setSpy(true); | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7847 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); | 
|  | 7848 |  | 
|  | 7849 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); | 
|  | 7850 | overlay->consumeMotionDown(); | 
|  | 7851 | window->consumeMotionDown(); | 
|  | 7852 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); | 
|  | 7853 | overlay->consumeMotionUp(); | 
|  | 7854 | window->consumeMotionUp(); | 
|  | 7855 |  | 
|  | 7856 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); | 
|  | 7857 | window->consumeMotionDown(); | 
|  | 7858 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); | 
|  | 7859 | window->consumeMotionUp(); | 
|  | 7860 |  | 
|  | 7861 | overlay->assertNoEvents(); | 
|  | 7862 | window->assertNoEvents(); | 
|  | 7863 | } | 
|  | 7864 |  | 
| Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 7865 | /** | 
|  | 7866 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. | 
|  | 7867 | * The scenario is as follows: | 
|  | 7868 | *   - The stylus interceptor overlay is configured as a spy window. | 
|  | 7869 | *   - The stylus interceptor spy receives the start of a new stylus gesture. | 
|  | 7870 | *   - It pilfers pointers and then configures itself to no longer be a spy. | 
|  | 7871 | *   - The stylus interceptor continues to receive the rest of the gesture. | 
|  | 7872 | */ | 
|  | 7873 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { | 
|  | 7874 | auto [overlay, window] = setupStylusOverlayScenario(); | 
|  | 7875 | overlay->setSpy(true); | 
|  | 7876 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); | 
|  | 7877 |  | 
|  | 7878 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); | 
|  | 7879 | overlay->consumeMotionDown(); | 
|  | 7880 | window->consumeMotionDown(); | 
|  | 7881 |  | 
|  | 7882 | // The interceptor pilfers the pointers. | 
|  | 7883 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); | 
|  | 7884 | window->consumeMotionCancel(); | 
|  | 7885 |  | 
|  | 7886 | // The interceptor configures itself so that it is no longer a spy. | 
|  | 7887 | overlay->setSpy(false); | 
|  | 7888 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); | 
|  | 7889 |  | 
|  | 7890 | // It continues to receive the rest of the stylus gesture. | 
|  | 7891 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); | 
|  | 7892 | overlay->consumeMotionMove(); | 
|  | 7893 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); | 
|  | 7894 | overlay->consumeMotionUp(); | 
|  | 7895 |  | 
|  | 7896 | window->assertNoEvents(); | 
|  | 7897 | } | 
|  | 7898 |  | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 7899 | struct User { | 
|  | 7900 | int32_t mPid; | 
|  | 7901 | int32_t mUid; | 
|  | 7902 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; | 
|  | 7903 | std::unique_ptr<InputDispatcher>& mDispatcher; | 
|  | 7904 |  | 
|  | 7905 | User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid) | 
|  | 7906 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} | 
|  | 7907 |  | 
|  | 7908 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { | 
|  | 7909 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 7910 | ADISPLAY_ID_DEFAULT, {100, 200}, | 
|  | 7911 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 7912 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
|  | 7913 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, | 
|  | 7914 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); | 
|  | 7915 | } | 
|  | 7916 |  | 
|  | 7917 | InputEventInjectionResult injectTargetedKey(int32_t action) const { | 
|  | 7918 | return inputdispatcher::injectKey(mDispatcher, action, 0 /* repeatCount*/, ADISPLAY_ID_NONE, | 
|  | 7919 | InputEventInjectionSync::WAIT_FOR_RESULT, | 
|  | 7920 | INJECT_EVENT_TIMEOUT, false /*allowKeyRepeat*/, {mUid}, | 
|  | 7921 | mPolicyFlags); | 
|  | 7922 | } | 
|  | 7923 |  | 
|  | 7924 | sp<FakeWindowHandle> createWindow() const { | 
|  | 7925 | std::shared_ptr<FakeApplicationHandle> overlayApplication = | 
|  | 7926 | std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7927 | sp<FakeWindowHandle> window = | 
|  | 7928 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window", | 
|  | 7929 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 7930 | window->setOwnerInfo(mPid, mUid); | 
|  | 7931 | return window; | 
|  | 7932 | } | 
|  | 7933 | }; | 
|  | 7934 |  | 
|  | 7935 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; | 
|  | 7936 |  | 
|  | 7937 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { | 
|  | 7938 | auto owner = User(mDispatcher, 10, 11); | 
|  | 7939 | auto window = owner.createWindow(); | 
|  | 7940 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 7941 |  | 
|  | 7942 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7943 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 7944 | window->consumeMotionDown(); | 
|  | 7945 |  | 
|  | 7946 | setFocusedWindow(window); | 
|  | 7947 | window->consumeFocusEvent(true); | 
|  | 7948 |  | 
|  | 7949 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7950 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); | 
|  | 7951 | window->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 7952 | } | 
|  | 7953 |  | 
|  | 7954 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { | 
|  | 7955 | auto owner = User(mDispatcher, 10, 11); | 
|  | 7956 | auto window = owner.createWindow(); | 
|  | 7957 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 7958 |  | 
|  | 7959 | auto rando = User(mDispatcher, 20, 21); | 
|  | 7960 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, | 
|  | 7961 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 7962 |  | 
|  | 7963 | setFocusedWindow(window); | 
|  | 7964 | window->consumeFocusEvent(true); | 
|  | 7965 |  | 
|  | 7966 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, | 
|  | 7967 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); | 
|  | 7968 | window->assertNoEvents(); | 
|  | 7969 | } | 
|  | 7970 |  | 
|  | 7971 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { | 
|  | 7972 | auto owner = User(mDispatcher, 10, 11); | 
|  | 7973 | auto window = owner.createWindow(); | 
|  | 7974 | auto spy = owner.createWindow(); | 
|  | 7975 | spy->setSpy(true); | 
|  | 7976 | spy->setTrustedOverlay(true); | 
|  | 7977 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 7978 |  | 
|  | 7979 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7980 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 7981 | spy->consumeMotionDown(); | 
|  | 7982 | window->consumeMotionDown(); | 
|  | 7983 | } | 
|  | 7984 |  | 
|  | 7985 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { | 
|  | 7986 | auto owner = User(mDispatcher, 10, 11); | 
|  | 7987 | auto window = owner.createWindow(); | 
|  | 7988 |  | 
|  | 7989 | auto rando = User(mDispatcher, 20, 21); | 
|  | 7990 | auto randosSpy = rando.createWindow(); | 
|  | 7991 | randosSpy->setSpy(true); | 
|  | 7992 | randosSpy->setTrustedOverlay(true); | 
|  | 7993 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); | 
|  | 7994 |  | 
|  | 7995 | // The event is targeted at owner's window, so injection should succeed, but the spy should | 
|  | 7996 | // not receive the event. | 
|  | 7997 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 7998 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 7999 | randosSpy->assertNoEvents(); | 
|  | 8000 | window->consumeMotionDown(); | 
|  | 8001 | } | 
|  | 8002 |  | 
|  | 8003 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { | 
|  | 8004 | auto owner = User(mDispatcher, 10, 11); | 
|  | 8005 | auto window = owner.createWindow(); | 
|  | 8006 |  | 
|  | 8007 | auto rando = User(mDispatcher, 20, 21); | 
|  | 8008 | auto randosSpy = rando.createWindow(); | 
|  | 8009 | randosSpy->setSpy(true); | 
|  | 8010 | randosSpy->setTrustedOverlay(true); | 
|  | 8011 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); | 
|  | 8012 |  | 
|  | 8013 | // A user that has injection permission can inject into any window. | 
|  | 8014 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8015 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 8016 | ADISPLAY_ID_DEFAULT)); | 
|  | 8017 | randosSpy->consumeMotionDown(); | 
|  | 8018 | window->consumeMotionDown(); | 
|  | 8019 |  | 
|  | 8020 | setFocusedWindow(randosSpy); | 
|  | 8021 | randosSpy->consumeFocusEvent(true); | 
|  | 8022 |  | 
|  | 8023 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); | 
|  | 8024 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 8025 | window->assertNoEvents(); | 
|  | 8026 | } | 
|  | 8027 |  | 
|  | 8028 | TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) { | 
|  | 8029 | auto owner = User(mDispatcher, 10, 11); | 
|  | 8030 | auto window = owner.createWindow(); | 
|  | 8031 |  | 
|  | 8032 | auto rando = User(mDispatcher, 20, 21); | 
|  | 8033 | auto randosWindow = rando.createWindow(); | 
|  | 8034 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); | 
|  | 8035 | randosWindow->setWatchOutsideTouch(true); | 
|  | 8036 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); | 
|  | 8037 |  | 
|  | 8038 | // We allow generation of ACTION_OUTSIDE events into windows owned by different uids. | 
|  | 8039 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8040 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 8041 | window->consumeMotionDown(); | 
|  | 8042 | randosWindow->consumeMotionOutside(); | 
|  | 8043 | } | 
|  | 8044 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 8045 | } // namespace android::inputdispatcher |